Managing Context Accumulation with Retrieval and Injection
This lesson explains how every tool call, retrieved passage and message adds tokens to an agent's context, and how retrieval and injection keep only what matters in view before the context exceeds the window, becomes slow and expensive, or buries the instructions.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how context accumulates in an agent loop.
- Keep a compact working set and retrieve details on demand.
- Inject context predictably during hand-offs between agents.
How context accumulates
In an agent loop, each iteration appends the model's tool call and the tool's result to the conversation, and the next model call processes all of it again. A run with ten tool calls that each return a few thousand tokens can reach tens of thousands of tokens, and multi-turn conversations add more. Costs and latency grow with every call because the whole context is reprocessed, and very long contexts make it more likely that the model overlooks instructions or facts in the middle.
The following diagram shows the tokens sent on each call of an agent loop.

How fast context grows
Retrieval instead of retention
Instead of keeping everything in context, an agent can keep a compact working set and retrieve details on demand. Large tool results are stored outside the context (for example in workflow state or a file) with a short reference and summary in the context; the agent calls a tool to read the full result if needed. Earlier conversation turns and long-term memories are retrieved by relevance to the current step. This pattern keeps context size roughly constant regardless of run length.
Keeping a compact working set
Injection and hand-offs between agents
Injection places retrieved or computed context into the prompt in a predictable position, delimited and labelled, as described in the prompt engineering module. Between agents, the orchestrator decides what each agent receives. The options are compared in the following table.
| Hand-off content | Advantages | Disadvantages |
|---|---|---|
| Full conversation | Nothing lost, no extra code | High token cost, exposes unrelated data |
| Previous agent's response only | Small, focused | Loses earlier facts unless stored elsewhere |
| Structured state plus task message | Small, precise, least privilege | Requires a defined schema |
Agent Framework's sequential orchestration, for example, passes the full conversation by default and can be configured to pass only the previous agent's response; custom executors can pass structured state.
What should the hand-off carry?
A context window error
A due-diligence agent reads company filings with a document tool that returns full text. After about 15 tool calls, runs fail with a context length error.
The tool should return a summary and a reference for each filing, with a second tool to read a specific section on demand, and the agent should record findings in structured state. A model with a larger context window would postpone the failure and increase cost, but would not change the unbounded accumulation.
Why are large tool results expensive in an agent run?
Show answer
Every tool result stays in the context and is reprocessed on every later call. Storing large results outside the context with a reference avoids this.
Exam Tips
Remember for the exam
- Every tool result is reprocessed on every later call in the run.
- Store large results outside the context with a reference; retrieve on demand.
- Hand off structured state rather than full transcripts.