Agents from scratch (TypeScript) · Chapter 15 of 18
Cost and latency
By the end of this chapter you can measure an agent run in requests, tokens, rupees and seconds, say why each step costs more than the one before, and make a run cheaper three ways: smaller results, a cheaper model for routine steps, and a prompt whose beginning stays the same. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Farooq has the first month's bill for the lab model in one hand and the bookings report in the other. "One booking: how many rupees? And 1,000 a day?" Nobody on the team could answer without guessing. Anjali asks the other half of the question: why does the bot take three seconds to book a plumber, when GharFix's own booking system answers in a millisecond?
Chapter 6 gave the loop a budget so a run could not spend without limit. That stops the worst runs; it does not tell you where the money goes in an ordinary one, or how to spend less. For that you measure every step, then change one thing at a time and measure again.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
run-cost.ts: what a run costs
In run-cost.ts, export loadPrices(), async: each model's prices from the lab's model list, as an object from the model's id to { input, cached_input, output } (rupees per million tokens, the lab's own price_inr_per_million field).
Export a class RunCost: new RunCost(prices); add(response, seconds) takes one response and how long its request took, and adds a step to steps, an array: an object with model (the response's own), promptTokens, cachedTokens (from the usage's prompt token details, 0 when there are none), completionTokens, rupees and seconds. A step's rupees: its cached prompt tokens at its model's cached_input price, its other prompt tokens at the input price, its completion tokens at the output price, all per million tokens.
requests, promptTokens, cachedTokens, completionTokens, rupees and seconds are the totals over every step, and summary() returns one line like 3 requests, 3387 prompt tokens (0 cached), 212 completion, ₹0.8470, 3.23 s (rupees with 4 decimals, seconds with 2).
Then export runMeasured(question, model = 'lab-chat'), async: chapter 3's loop with that model, a system message saying today is 2026-10-22, the question and TOOLS, at most 10 requests, timing each request and adding it to a RunCost. Return { answer, cost }. Importing the file sends no request.
- Task 2
trimmed.ts: the same job, with smaller results
Every result is resent with every later request. In trimmed.ts, export trim(name, result): for find_technicians, each row keeps only technician_id, name, rating and free_slots; for check_slots, each row keeps technician_id and slot; for book_slot, the result keeps booking_id, date, slot, technician, amount and status. A key the result does not have is left out. Every other tool's result, and any result with an error, is returned as it is.
Then export run(question, trimmed = true), async: chapter 3's loop with lab-chat, a system message saying today is 2026-10-22, the question and TOOLS, at most 10 requests, sending each result trimmed when trimmed is true and whole when it is false. Return { answer, promptTokens }: the final answer, and the prompt tokens of every request added up.
The check runs a booking with no slot both ways.
- Task 3
two-models.ts: a cheaper model for the routine steps
In two-models.ts, export run(question), async: chapter 3's loop with a system message saying today is 2026-10-22, the question and TOOLS, at most 10 requests. Every request goes to lab-chat-mini until a call that is not one of the four lookups (find_technicians, check_slots, get_quote, get_booking) has run; from then on, requests go to lab-chat.
Measure the run with RunCost and loadPrices from ./run-cost.ts and return { answer, cost }.
The check runs a booking, a cancellation and a quote, and compares the cost with lab-chat on every step.
- Task 4 · interview question
Interview: per-day.ts, Farooq's question
Farooq needs the cost of 1,000 bookings a day. per-day.ts runs three sample bookings and estimates it, and the bill for the first week of real bookings came to several times its number. Find what the estimate gets wrong and fix it, so that run(question, model) returns what one run really cost, and estimate(questions, perDay, model) the cost of perDay bookings a day from the mean cost of the sample runs.
Keep run, estimate, rupees, loadPrices and SAMPLE, exported, and what each returns. The check runs a booking, then the estimate with lab-chat and with lab-chat-mini, and adds up the usage the lab model reported.
What you will learn
- Measuring a run: requests, prompt, cached and completion tokens, rupees and seconds, step by step.
- Why each step costs more: what is resent every time.
- Three savings: trimming tool results, a cheaper model for routine steps, and caching a prompt's unchanged beginning.
- Latency: where an agent's seconds go.
More in Production
Build "Cost and latency" yourself
GharFix, a Bengaluru home-services company, needs an assistant that books, reschedules and refunds safely. You build it in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Agents from scratch (TypeScript) are free. This one comes with the all labs plan.