Coding agents

Putting it all together

View as Markdown

Congratulations, you've learned how to work with coding agents.

You now know how to use agents to understand codebases, create features quickly, search through your code and find bugs, review code more efficiently, and customize agents to your needs. This chapter shows an example of putting all of those pieces together.

An end-to-end example

You've joined a team that runs an ecommerce platform. You need to add discount codes to the checkout flow. The codebase is unfamiliar, and the feature needs to ship by end of week.

This is a realistic situation. You need to explore before you build, plan before you code, test before you ship, and review before you merge.

Step 1: Understand the codebase

Before touching anything, explore the checkout flow. Use the agent to build a mental map.

Ask mode example: Explore the checkout flow
I need to add discount codes to our checkout flow. Before I make any changes, walk me through how checkout works. What components, API routes, and services are involved? Show me the data flow from the cart to payment confirmation.
See the agent's response
Searched codebase checkout flow components and services
Read src/pages/Checkout.tsx
Read src/services/OrderService.ts
Read src/services/PricingService.ts
Checkout flow: cart to payment confirmation
1.Cart page (src/pages/Cart.tsx) — User reviews items, clicks "Checkout"
2.Checkout page (src/pages/Checkout.tsx) — Collects shipping address and payment method
3.Order summary (src/components/OrderSummary.tsx) — Displays line items, subtotal, tax, and total. Calls PricingService.calculateTotal()
4.Payment — Checkout.tsx:handleSubmit() calls OrderService.createOrder(), which calls PaymentService.charge()
5.Confirmation — On success, redirects to /orders/[id]/confirmation
Where discounts would fit: PricingService.calculateTotal() at line 34 computes the subtotal from line items. A discount step would go between subtotal calculation and tax calculation (line 41). The OrderSummary component would need a new row to show the discount amount.

Ask follow-up questions to fill gaps:

Ask mode example: Find where discounts fit
How does our pricing calculation work in src/services/PricingService.ts? Where do we compute the order total, and where would a discount be applied in that flow?
See the agent's response
Read src/services/PricingService.ts
Grepped calculateTotal
Pricing calculation flow:
calculateTotal(items) → {
  subtotal = sum(items.map(i => i.price * i.quantity))   // line 34
  tax = subtotal * taxRate                               // line 41
  total = subtotal + tax + shippingCost                  // line 45
}
Discount insertion point: Between lines 34 and 41. Apply the discount to subtotal before tax calculation so tax is computed on the discounted amount.
Important: PricingService is also called by InvoiceService and QuoteService. Any discount logic needs to handle cases where no discount is applied (default path should be unchanged).

This combines the search techniques from understanding your codebase to trace the checkout flow, then summarizes what it finds.

Step 2: Plan the feature

Now that you understand the codebase, create a plan. Switch to Plan Mode (Shift+Tab) and outline the work:

Plansdiscount-codes.md
Discount codes
Overview

Add discount code support to checkout. Users enter a code, the system validates it, and the discount is applied to the order total before tax.

Approach
•Add DiscountService with validation logic (expiry, usage limits)
•Modify PricingService.calculateTotal() to accept optional discount
•Add discount code input to OrderSummary component
•Add /api/discount/validate endpoint
6 Tasks
Create discount_codes table migration
Create DiscountService with applyDiscount()
Add discount step to PricingService.calculateTotal()
Add code input UI to OrderSummary component
Add /api/discount/validate route
Write tests for all discount types and edge cases

Review the plan. Remove steps that are too ambitious for the first iteration. Focus on the core flow: apply a code, calculate the discount, show it in the summary. Then click Build to create the first version of this feature.

Step 3: Debug a failing edge case

After the build, one test keeps failing: stacking a percentage discount with a fixed-amount discount produces the wrong total. The same set of discounts gives different results depending on the order they arrive.

Use Debug Mode or ask the agent to investigate the root cause:

Agent example: Debug the root cause
The discount stacking test is failing. The same discounts produce different totals depending on application order. Find out why and fix the ordering of discount application in src/services/DiscountService.ts. Show me the before and after values at each step.
See the agent's response
Read src/services/DiscountService.ts
Thought for 3s
The stacking test applies a 50% discount and a $30 fixed discount to a $40 item. Current code applies them in arrival order: $40 - $30 = $10, then $10 * 0.5 = $5. But the test expects percentage first: $40 * 0.5 = $20, then $20 - $30 = -$10 → $0 (floored at zero). The result changes depending on which discount type is applied first. Percentage discounts should always apply first so the math is predictable.
EditedDiscountService.ts+8-4
  // Apply discounts in order received
  for (const discount of discounts) {
  // Always apply percentage discounts before fixed-amount
  const sorted = [...discounts].sort((a, b) =>
    a.type === 'percentage' ? -1 : 1
  )
  for (const discount of sorted) {
Root cause: Discounts were applied in arrival order, which made the result depend on the order they were received. Applying fixed-amount first ($40 - $30 = $10, then 50% off = $5) gives a different result than percentage first ($40 * 0.5 = $20, then -$30 = -$10 → $0 after floor).
Fix: Sort discounts so percentage discounts always apply before fixed-amount discounts. This ensures percentages calculate against the full price, and the existing $0 floor handles cases where discounts exceed the total.
Before: $40 → -$30 = $10 → *0.5 = $5 (order-dependent)
After: $40 → *0.5 = $20 → -$30 → floor at $0 (consistent)
All discount tests now pass.

This applies the debugging principles from the Finding and Fixing Bugs chapter: don't fix symptoms, find the cause.

Step 4: Review and test

Before pushing, run a self-review:

Ask mode example: Self-review before pushing
Review all the changes I've made for the discount code feature. Check for bugs, missing error handling, edge cases, and consistency with existing patterns. What would a code reviewer flag?
See the agent's response
Ran git diff --staged
Searched codebase discount and pricing patterns
Review summary:
•4 files changed, 142 insertions, 3 deletions
•New: DiscountService.ts, discountCode.test.ts, api/discount/validate/route.ts
•Modified: PricingService.ts (added discount step in calculateTotal)
Issues found:
1.No rate limiting on /api/discount/validate — add rateLimit(10, '1m') to match other validation endpoints
2.Missing index — discount_codes table queries by code column but no index exists. Will be slow at scale.
Looks good: Test coverage, error handling patterns, discount ordering logic, price floor at $0.

If you have cloud agents set up, launch a few to test edge cases you might have missed: unicode characters in discount codes, large discount values, or concurrent code usage. Cloud agents report back with the cases they covered and areas that need more testing, so you can fill gaps before merging.

Step 5: Write a rule

You learned something during this feature: discount calculations should always apply percentage discounts before fixed-amount discounts so results are consistent regardless of input order. Capture this in a rule so the agent knows for next time:

Text

# Pricing rules

- Apply percentage discounts before fixed-amount discounts
- Order total must never go below $0
- See src/services/PricingService.ts for the canonical calculation pattern

This completes the loop. You explored, planned, built, tested, debugged, reviewed, and captured knowledge for the future.

You've finished the discount code feature. A teammate asks you to add gift cards next week. What's your first step?

Keep building

The best way to learn is to take what you've learned here and apply it.

The tools and models are improving rapidly and the specific practices will evolve over time. But this core skill, learning how to work collaboratively with coding agents, will keep compounding. It's becoming one of the most important skills in software engineering.

By pushing the limits of what coding agents can do, you'll get the most out of the latest tools and models as they ship. Good luck and stay curious.

You've completed this chapter

Back to the course outline

Last updated: November 23, 2026