Lesson 1 of 4 Context Management and Agent Memory

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 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 everything again. After ten tool calls that each return a few thousand tokens, the context reaches tens of thousands of tokens, so cost and latency grow with every call, and the model is more likely to overlook instructions or facts in the middle.
Growth is illustrative.
Fill in the number

How fast context grows

Exam skills 2.2.1

Work out how context grows in an agent loop where every result stays in the conversation.

Log in to try this activity Interactive activities are free with an account.

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.

True or false

Keeping a compact working set

Exam skills 2.2.1

Check how retrieval keeps context size under control.

Log in to try this activity Interactive activities are free with an account.

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 contentAdvantagesDisadvantages
Full conversationNothing lost, no extra codeHigh token cost, exposes unrelated data
Previous agent's response onlySmall, focusedLoses earlier facts unless stored elsewhere
Structured state plus task messageSmall, precise, least privilegeRequires 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.

Sort into groups

What should the hand-off carry?

Exam skills 2.2.1

Decide which kind of hand-off content each statement describes.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.