Build your own coding agent (TypeScript) · Chapter 15 of 20
Budgets and loops
By the end of this chapter you can say what a run costs in tokens and rupees, stop a run at a limit you choose (steps, tokens, rupees, seconds), catch an agent that asks for the same thing again and again, and end a run that cannot succeed with a note a person can pick up. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Scale and trust
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Build your own coding agent (TypeScript) are free. This one comes with the all labs plan.
Why you need this
MD-111 is the odd one in the tracker. A rider says trip T-4471 cost ₹1 more than the tariff card. Nobody could reproduce it, and T-4471 is not in the October log. There may be nothing wrong in the code at all: a meter misread, a typo on a receipt, a rider who misremembered.
Kaarigar does not know that. Given MD-111, it searches for T-4471, reads src/fare.ts, runs the tests (all 44 pass), searches for roundFare, reads src/money.ts, lists the files, and then starts again from the top. It never decides to stop: in this lab the model stops by itself only after 60 calls, and real models are no better at admitting "I cannot find it". Every turn round the circle sends the whole conversation again, so each request is bigger than the last, until request 33 is refused:
400 This model's maximum context length is 8192 tokens. However, your
messages resulted in 8500 tokens. Please reduce the length of the messages.
Thirty-two requests were paid for, and the run ends in an error that tells Sneha nothing: not what was tried, not what was ruled out, not what to do next. Kaarigar so far has one limit, maxSteps, and it is a crude one: it says nothing about money, it does not notice the agent going round in circles, and when it fires, the run just ends.
Real coding agents have the same problem and the same answers: a limit on turns, a limit on spend, a check for repeated actions, and, when the agent stops, a summary for the person. This chapter builds them.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
prices.ts: what a run costs
Write and export prices(): fetch $OPENAI_BASE_URL/models and return { [model id]: { input, output } } for every model with a price_inr_per_million. Write and export rupees(usage, price): (usage.prompt × price.input + usage.completion × price.output) / 1,000,000. Under if (import.meta.main), run Kaarigar on a checkout of MD-108 (runAgent with issueText('MD-108'), SYSTEM and tools()) and print:
MD-108: N steps, P prompt + C completion tokens
lab-chat: ₹X (rupees at lab-chat's price, toFixed(4))
lab-chat-mini: ₹X (the same usage at lab-chat-mini's price)
- Task 2
budget.ts: limits, and a loop detector
Write and export budget(b, price = { input: 200, output: 800 }), returning { hook, why }. hook(run, reply) is an afterStep hook: for a reply with no tool calls return null; count each tool call by its name and arguments text, and return 'loop' when one is asked for the b.repeats-th time (3 if not set); return 'budget' when run.steps reaches b.steps, prompt + completion tokens reach b.tokens, rupees(run.usage, price) reaches b.rupees, or the seconds since budget() was called reach b.seconds (a limit left out is no limit); else null. why() says in a few words what stopped the run ('' until then).
- Task 3 · interview question
Interview: handoff.ts, an issue with no fix
Write and export handoff(run, client = defaultClient()): one more request to 'lab-chat' with run.messages plus a user message HANDOFF and no tools; add its usage to run.usage and return its text. Under if (import.meta.main): run MD-111 with no budget (maxSteps 60) and print no budget: STOP at step N, ₹X. Then run it in a fresh checkout with afterStep: budget({ steps: 20, repeats: 3 }).hook, get the note with handoff(run), and print with a budget: STOP at step N (WHY), ₹X, then the note. (₹X: lab-chat's price, toFixed(2), of run.usage; for the second run after the note.)
What you will learn
- What a run costs: usage per request, prices per million tokens, rupees.
- Budgets: steps, tokens, rupees and seconds, checked after every reply.
- Loop detection: the same call with the same arguments, again.
- The window and the bill are different limits, and both matter.
- The hand-off: one last request, with no tools, for a note a person can use.
More in Scale and trust
Build "Budgets and loops" yourself
MeterDown, a Pune app for auto-rickshaw fares, has eleven open issues. You build Kaarigar, an agent that fixes them without breaking tests, running risky commands or touching files outside the repository. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Build your own coding agent (TypeScript) are free. This one comes with the all labs plan.