
Context Summary (Recommended)
For long-running conversations, use thecontext_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
- Messages accumulate normally until reaching
ceiling - When
ceilingis exceeded, older messages are summarized in the background - After summarization, model sees ~
floormessages (summary + recent) - The verbatim window starts right after the summary (no hidden messages)
- The cycle repeats, keeping context bounded between
floorandceiling
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
Theinstructions 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.
4. User provided tools
You can provide an agent with specialized Tools usingAgent.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. Theget_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 usingAdditionalContextSection:
images/main/main.py
Filter Conversation History
Control which messages appear in context. Create a custom section that filters messages - memory is passed directly toget_messages:
images/main/main.py
Summarize Conversation History
For long-running conversations, you can useSummarizedHistorySection directly
in a custom context template. This gives you more control than the context_summary
parameter while still benefiting from async summarization.
Using SummarizedHistorySection
TheSummarizedHistorySection provides non-blocking summarization that
dramatically improves response times:
images/main/main.py
- When conversation exceeds
ceiling, summarization is triggered - Summarization runs in the background - never blocks the response
- Returns cached summary immediately (even if slightly stale)
- The verbatim window starts right after the summary (no hidden messages)
- Re-summarizes when
batch_size(ceiling - floor) new messages accumulate
Monitoring:
Custom Summarization
For more control, you can implement your own summarization section. Memory is passed directly toget_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:- Memory stores all messages from conversations.
- Context template decides which messages to include.
- Sections receive memory as an argument and retrieve messages from it.
- Agent sends the combined context to the model.
- 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: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.
