Build your own coding agent · Chapter 14 of 20
Sub-agents
By the end of this chapter you can hand one piece of a big task to a sub-agent: a fresh run of Kaarigar's loop with only that piece as its task, a cheaper model, its own step limit and its own copy of the repository; you can bring its changes back as one patch, catch a clash when two copies changed the same line, and say what this saves in tokens and rupees. 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
- Python
- In the all labs plan
The first 2 chapters of Build your own coding agent are free. This one comes with the all labs plan.
Why you need this
The RTO's December revision is close, and Sneha has stopped sending Kaarigar one issue at a time. Her message this morning:
Fix these issues:
- MD-105: Mumbai's tariff is out of date
- MD-108: The first bag should ride free
- MD-109: Receipt polish
Kaarigar can do this today, one issue after the other in one conversation. It fixes all three, in 12 requests. But look at what it sent:
| One context, three issues | |
|---|---|
| requests | 12 |
| prompt tokens, all requests added up | 52,855 |
| the biggest single request | 6,596 of the 8,192-token window |
cost on lab-chat | ₹11.82 |
Every request carries everything before it: MD-105's whole tariff file is still being sent while Kaarigar works on the receipt, which has nothing to do with tariffs. The cost grows with the square of the list, and the biggest request is already at 80% of the window. A fourth or fifth issue with a long file in it, and the next request is a 400.
Kedar has seen this in every coding agent he has used: one conversation is the wrong shape for several independent jobs. Jaya adds her worry: if three jobs did run at the same time in one folder, each would see the others' half-made changes, and run the tests on them.
What Kaarigar has so far cannot meet either need. Its loop has one conversation and one folder.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
worktree.py: a copy for each sub-agent
Write worktree(root) -> Worktree (given: path, diff, remove). path is a folder "tree" in a new temp folder (tempfile.mkdtemp(prefix="kaarigar-wt-")), made by running git worktree add --detach PATH HEAD in root (raise RuntimeError with git's message if it fails). diff() runs git add -A, then returns git diff --cached --binary, both in the worktree: every change since HEAD, new files too ("" when none). remove() runs git worktree remove --force PATH in root, then deletes the temp folder.
- Task 2
delegate.py: the delegate tool
Write delegate_tool(make, reports=None) -> Tool, a tool named delegate with one required string parameter, task. Its run makes a worktree of ctx.root, runs run_agent(wt.path, task, make(wt.path)), takes wt.diff() and, when it is not empty, applies it in ctx.root with git apply (the patch as input). It appends a Report(task, answer, steps, tokens (prompt + completion), applied (git apply's return code was 0), files (the patch's +++ b/ paths)) and returns the sub-agent's answer and a line like (sub-agent: 6 steps; changed meterdown/a.py), saying when the patch was NOT applied, with git's first error line. The worktree is removed in a finally, whatever happens.
- Task 3 · interview question
Interview: fan_out.py, one context or sub-agents?
Give Kaarigar MD-101, MD-102 and MD-104 in one task (Fix these issues:, then a line "- " + the first line of each issue_text), twice, each time on a fresh checkout of all three: "one context" with Agent(SYSTEM, tools()), and "sub-agents" with a parent that has read_file and delegate_tool(make), each sub-agent Agent(SYSTEM, tools(), model="lab-chat-mini", max_steps=12). After each, print:
LABEL: P prompt tokens, R requests, F/3 fixed
(P the run's usage.prompt, R its steps, F the issues grade calls fixed)
What you will learn
- What a sub-agent is: the same loop, a new conversation with only its task, and its own limits.
- A git worktree per sub-agent: a separate folder from the same repository, so no sub-agent sees another's work in progress.
- A patch back: each sub-agent's changes, applied to the repository with git apply, all or nothing.
- Clashes: two patches that change one line, caught and reported, never half applied.
- What it saves, measured: the parent's tokens, the window, and rupees.
More in Scale and trust
Build "Sub-agents" 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 are free. This one comes with the all labs plan.