Skip to main content
Context is the structured information than an agent gives its language model at each conversational turn. Flowchart showing the agent loop: (1) Build context from instructions, memory, and tools, (2) Send context to language model, (3) Receive tool calls from model, (4) Execute tools and gather responses, (5) Repeat until goal is achieved or no more tool calls are needed. It includes system instructions, conversation history, and additional information that helps the model decide the agent’s next step. Think of context as everything a model “sees” when making a decision about the next step an agent should take in its journey. Agents use Context Templates to build context.
For long-running conversations, use the context_summary parameter in Agent.start() to automatically manage context size. This is the simplest and recommended approach for agents that need to sustain conversations with many messages.

Basic Usage

images/main/main.py

Custom Configuration

images/main/main.py

How It Works

  1. Messages accumulate normally until reaching ceiling
  2. When ceiling is exceeded, older messages are summarized in the background
  3. After summarization, model sees ~floor messages (summary + recent)
  4. The verbatim window starts right after the summary (no hidden messages)
  5. The cycle repeats, keeping context bounded between floor and ceiling

Configuration Options

Performance Benefits

The async summarization approach provides significant performance improvements:
context_summary cannot be used together with context_template. Use context_summary for automatic summarization, or context_template for full control over context structure.

Default Context Template

By default, agents use a context template with the following structure:

1. System instructions

The instructions that you provide Agent.start(..., instructions="", ...) become a system message that appears first in the context.

2. Framework instructions

Agent’s provide a collection of built-in tools. Instructions to use these tools are automatically injected after the system instructions.
  • Time tools - Get current time in UTC or local format
  • Filesystem tools (if enabled) - Read, write, search files
  • User input tool (if enabled) - Pause and ask the user for clarification
  • Subagent tools (if configured) - Delegate work to specialized sub-agents

3. Conversation history

Messages from the current conversation, including:
  • System messages.
  • User messages.
  • Model responses.
  • Tool calls and their results.
The agent retrieves this from Memory to maintain conversational context.

4. User provided tools

You can provide an agent with specialized Tools using Agent.start(..., tools=[], ...). Instructions to use these tools are injected as the final piece of context.

Customize Context

You can customize how context is built by providing a custom Context Template when starting an agent.

Custom Context Template

Provide a custom context template when starting an agent:
images/main/main.py

Custom Sections

Create your own section by implementing the section interface. The get_messages method receives memory as its first argument, giving you direct access to the agent’s conversation history:
images/main/main.py

Add Dynamic Context

Inject additional information into the context using AdditionalContextSection:
images/main/main.py

Filter Conversation History

Control which messages appear in context. Create a custom section that filters messages - memory is passed directly to get_messages:
images/main/main.py

Summarize Conversation History

For long-running conversations, you can use SummarizedHistorySection directly in a custom context template. This gives you more control than the context_summary parameter while still benefiting from async summarization.
For most use cases, prefer Agent.start(..., context_summary=True) which automatically configures summarization. Use SummarizedHistorySection directly only when you need a custom context template structure.

Using SummarizedHistorySection

The SummarizedHistorySection provides non-blocking summarization that dramatically improves response times:
images/main/main.py
How it works:
  1. When conversation exceeds ceiling, summarization is triggered
  2. Summarization runs in the background - never blocks the response
  3. Returns cached summary immediately (even if slightly stale)
  4. The verbatim window starts right after the summary (no hidden messages)
  5. Re-summarizes when batch_size (ceiling - floor) new messages accumulate
Configuration options: Monitoring:

Custom Summarization

For more control, you can implement your own summarization section. Memory is passed directly to get_messages:
images/main/main.py
Custom synchronous summarization blocks on each request, adding significant Use SummarizedHistorySection for better performance in production.

Retrieval-Augmented Generation (RAG)

You can also automatically inject search results into context but adding a section that searches a knowledge base of documents for recent messages in the conversation history. However, it is usually better to give an agent the ability to search the knowledge base as a tool. For complete documentation on creating knowledge bases and turning them into tools, see Knowledge.

Context and Memory

Context works closely with Memory:
  1. Memory stores all messages from conversations.
  2. Context template decides which messages to include.
  3. Sections receive memory as an argument and retrieve messages from it.
  4. Agent sends the combined context to the model.
The separation allows you to:
  • Store complete conversation history in memory.
  • Send only relevant context to the model.
  • Add dynamic information without modifying stored messages.
  • Implement features like filtering and summarization.

Section Interface

All context sections implement this interface:
The memory argument gives sections direct access to the agent’s conversation history without needing to store it at construction time. This makes sections simpler to create and test.