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, know roughly what a context window and a tool call are, and can read short Python. It explains the pressure that makes subagents useful. The next chapters cover the subagents themselves.
The context window is the agent's whole working memory
A model does not remember anything between requests. Each time the agent loop calls the model, it sends everything the model should know, and the model replies from that input alone. That input is the context window. It usually holds:
- The system prompt and tool definitions.
- Your task description.
- Every earlier model message.
- Every tool call and its result.
The window has a fixed size, measured in tokens. A token is a chunk of text, roughly three to four characters of English. Code often uses more tokens per line than prose because of symbols and indentation.
Two limits follow from this. The window has a hard ceiling, and a request that exceeds it fails or forces the agent to drop content. The window also has a cost, because you pay for and wait on every token you send on every turn.
How the context grows during a task
In the single-agent loop, the agent reads a file, runs a command, reads the output, edits a file, and runs the tests. Each step appends to the history. Nothing is removed unless the agent actively trims it.
Tool results cause most of the growth. A few examples of typical sizes:
| Action | Typical size added |
|---|---|
| Reading a 400-line source file | 4,000 to 6,000 tokens |
| Running a test suite that prints failures | 2,000 to 20,000 tokens |
| A repository-wide search with many matches | 1,000 to 10,000 tokens |
| An edit and its confirmation | 200 to 1,000 tokens |
The sizes above are rough estimates and vary by language and tooling. The pattern matters more than the numbers: exploration is expensive, and most of what the agent reads is needed only briefly.
The next script models the growth. It assumes every step adds the same number of tokens and that the whole history is sent on each request.
CONTEXT_LIMIT = 200_000 # tokens the model can accept per request
BASE_CONTEXT = 6_000 # system prompt, tool definitions, task text
def simulate(tokens_per_step: int) -> tuple[int, int]:
"""Return (steps completed before the limit, total input tokens sent)."""
history = BASE_CONTEXT
total_sent = 0
steps_done = 0
while True:
history += tokens_per_step
if history > CONTEXT_LIMIT:
return steps_done, total_sent
total_sent += history # the whole history is resent on every request
steps_done += 1
if __name__ == "__main__":
steps, total = simulate(tokens_per_step=4_000)
print(f"Steps that fit in the window: {steps}")
print(f"Total input tokens sent across those steps: {total:,}")With 4,000 tokens per step, 48 steps fit and the 49th overflows. The 48 requests together send 4,992,000 input tokens. The window holds only about 200,000 tokens, but the cumulative cost is about 25 times that. Prompt caching, which many providers offer, can lower the price of the repeated prefix. It does not remove the ceiling, and it does not make the model attend better to a long history.
Failures that appear before the limit is hit
An agent rarely fails by reaching the exact ceiling. Quality usually degrades earlier, in these ways.
Attention dilution
Models handle long inputs less reliably than short ones. Information in the middle of a very long context is more likely to be overlooked than information near the start or end. A constraint you stated at the beginning of the session ("do not touch the public API") competes with tens of thousands of tokens of file contents and logs.
Stale and contradictory content
The history keeps old versions of things. The agent read config.py, edited it, and read it again, so both versions sit in the window. The agent may later act on the older one. Failed approaches stay in the history as well, and they can pull the model back toward them.
Distraction by irrelevant detail
Reading ten files to find one function leaves nine files of unrelated code in the window. The agent has no way to forget them. They add noise for every later decision.
Drift from the original goal
Over many steps, the agent works on the most recent problem it saw. A test failure leads to a fix, which exposes another failure, which leads to a refactor. The original task statement is now a small part of a large history, and the agent can wander from it.
Loss from compaction
Many agent tools handle a full window by summarizing or truncating older content. This keeps the session running, but a summary discards detail. If a later step needs an exact error message or a line number from the dropped portion, the agent must find it again or guess.
Latency and cost grow with the history
Every request carries the full history, so each step is slower and more expensive than the one before it. This is why the script above shows a cumulative total far larger than the window size. A task that takes 100 steps costs much more than ten tasks of 10 steps each, because the early steps of the long task are cheap and the late steps are expensive. The relationship between steps and cost is closer to quadratic than linear, unless caching absorbs part of it.
Why parallel work makes this worse
Consider a task with three independent parts: update documentation, add tests, and fix a bug in the parser. A single agent does them in sequence. By the time it reaches the third part, its window holds all the exploration and edits from the first two. None of that is useful for the third part, but the agent still pays for it and still has to work around it.
Sequential work also takes the sum of the durations. Three 10-minute pieces take 30 minutes, even though nothing about them depends on each other.
Ways to reduce the strain without more agents
Before reaching for multiple agents, there are cheaper options. Use them where they fit.
- Start a fresh session per task. Do not carry one session across unrelated work.
- Write progress to a file. A notes file lets a new session resume without the full history.
- Read narrowly. Search first, then read only the relevant line ranges instead of whole files.
- Limit command output. Pipe long output through
tailorgrep, or ask for a failing test only. - Compact deliberately. If you must compact, do it at a natural boundary and state what must be kept.
These help, but they all depend on discipline within a single window. They do not solve the underlying problem that the exploration for one piece of work occupies the same space as the next piece.
What this motivates
The pressure described here points to a design goal: let part of the work happen in a separate context, and bring back only the result. The exploration stays outside the main window, and the main agent receives a short summary. That is the idea behind a subagent, which the next chapter defines.
Exercise
A single agent has a 6,000-token base context and each step adds 5,000 tokens. The window limit is 100,000 tokens. How many steps complete before the next step would exceed the limit? Name one failure, other than hitting the limit, that you would expect before that point.
Show answer
The history after n steps is 6,000 + 5,000n. For n = 18 it is 96,000, which fits. For n = 19 it is 101,000, which exceeds 100,000. So 18 steps complete. Before the limit, you might see attention dilution (an early constraint being overlooked), stale file contents contradicting newer ones, or drift from the original goal.
Exercise
Why does a 100-step task cost much more than ten separate 10-step tasks, even if each step adds the same number of tokens?
Show answer
Each request resends the whole history. In the 100-step task, the late steps carry up to 100 steps of accumulated content, so the total tokens sent grow roughly with the square of the step count. Each 10-step task starts from a small context and never carries more than 10 steps of history, so the total stays much smaller.