Pipeline. Configure a ContextWindow with token and item budgets. Before each LLM call it summarizes older turns and truncates excess items so your agent preserves key memory without exceeding limits.
How Context Window Works
Context Window is configured on aPipeline instance via the context_window parameter. It runs a two-step management cycle before every LLM call:
Three item types are always protected and never removed:
Configuration Parameters
Adjust these parameters to match your application’s token limits, cost targets, and expected tool-call patterns.Processing Cycle
Before each LLM call, an internal process is automatically executed. This process performs two steps in sequence.Step 1: Compress
When the context exceeds the token or item budget and there are enough old turns to compress (more user turns thankeep_recent_turns), compression kicks in:
- Split - Separate items into old turns and recent turns (keeping the last N user exchanges).
- Render - Convert old items into human-readable text for the summarization prompt.
- Summarize - Call the LLM (or
summary_llm) to generate a concise summary. - Replace - Remove all old items and insert the summary as a system message tagged with the internal Summary source (rendered as
[Conversation Summary]).
- Key facts, names, and numbers
- Decisions made and their reasoning
- Tool/function call results and outcomes
- Commitments or promises the assistant made
- User objectives, preferences, and unresolved tasks
Step 2: Truncate
After compression (or if compression wasn’t needed), truncation enforces hard limits:- Remove the oldest non-protected items one at a time.
- Function call/output pairs are removed together to avoid orphaned tool calls.
- Continue until both
max_tokensandmax_context_itemsare satisfied. - If only protected items remain, stop even if still over budget.
How Tool Chaining Works
Context Window works with tool chaining. Here’s the lifecycle of a multi-tool turn:1
User request
User says “Plan for Dubai” → LLM returns
get_weather(Dubai).2
First tool runs
Tool executes → result added to context → LLM called again.
3
Chained tool call
LLM returns
get_clothing_advice(22°C) → execute → call LLM again.4
Next tool call
LLM returns
get_activity_suggestion(22°C, "jacket") → execute → call LLM.5
Final response
LLM returns text “Dubai is 22°C, wear a jacket, go hiking!” → spoken by TTS.
max_tool_calls_per_turn=10.
Some LLMs (Anthropic Claude, OpenAI GPT-4o) can return multiple tool calls in a single response. These are collected and executed in parallel using
asyncio.gather, then all results are added to context before the next LLM call. Google Gemini sends one tool call at a time (always sequential).Example
A full example combining Context Window with tool chaining for a production-ready travel assistant:What’s Next
Context Management
How ChatContext records conversation memory.
Agent Handoffs
Share context across specialized agents.