Sub topic 2
Why Long Tasks Strain a Single Context Window
This chapter assumes you have used an AI coding agent in a terminal or editor, and that you know roughly what a context window and a tool call are. It explains the problem that subagents exist to solve.
The loop that fills the window
A single coding agent runs a loop. It reads the prompt, decides on a tool call, runs the tool, and reads the result as an observation. Then it decides again. Every step appends to one shared transcript.
The context window is the maximum amount of text (measured in tokens) the model can read at once. The transcript has to fit inside it. Everything the agent has seen stays in the transcript until something removes it.
What grows the context
Most of the growth comes from observations, not from the agent's own reasoning. Typical sources:
- Whole files read to check one function.
- Test runs that print hundreds of lines of output.
- Search results with many irrelevant matches.
- Failed attempts, including the errors and the edits that caused them.
A task such as "update the docs, add tests, and rename a module" can touch dozens of files. The agent carries all of that output into every later step.
Three ways it goes wrong
| Problem | What you see |
|---|---|
| Hard limit | The agent stops, or older content is dropped or summarized automatically. |
| Dilution | Relevant facts sit among thousands of irrelevant lines, and the model uses them less reliably. |
| Cost and speed | Each step re-reads the whole transcript, so later steps cost more and take longer. |
Dilution is the least obvious. The agent can still fit everything, yet it forgets a constraint you stated early, or repeats a fix it already tried. This often appears well before the hard limit.
Compaction (summarizing old turns to free space) helps, but it is lossy. The summary may drop a detail that matters later, and you cannot always tell which one.
Unrelated work shares one transcript
Consider a task with two independent parts: updating the README and adding tests for parse_args. In a single agent, the README edits and the test output land in the same context. While writing tests, the agent still carries the README drafts. Neither part needs the other, but both pay for it.
This is the core problem. The work is separable, but the context is not. A subagent addresses this by giving a piece of work its own fresh context, so only a short result returns to the parent. The next chapters cover how that works.
A rough way to estimate growth
You can estimate the cost of a long run with simple arithmetic. If each step adds about 3,000 tokens of observations, then after 20 steps the transcript holds about 60,000 tokens. Step 20 reads all of that again.
def transcript_tokens(steps: int, tokens_per_step: int, prompt: int = 2000) -> int:
"""Tokens in the transcript after a given number of steps."""
return prompt + steps * tokens_per_step
total_read = sum(transcript_tokens(s, 3000) for s in range(1, 21))
print(transcript_tokens(20, 3000)) # 62000
print(total_read) # 1,282,000 tokens read across 20 stepsThe context at the last step is 62,000 tokens, but the total read across all steps is more than 1.2 million. Total cost grows roughly with the square of the number of steps. Splitting work into smaller contexts flattens that growth.
Exercise
A single agent does two unrelated jobs in one run. Name two ways the shared context hurts, even when everything still fits in the window.
Show answer
Dilution: facts from one job crowd the other, so the model may miss constraints or repeat fixes. Cost and latency: every step re-reads the full transcript, including the unrelated job's output.
Exercise
Using transcript_tokens with 3,000 tokens per step, how many tokens are in the transcript after 10 steps?
Show answer
2,000 + 10 × 3,000 = 32,000 tokens.