Skip to main content
Automatically manage history for the 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. Context Window compress and truncate cycle
Context Window replaces manual context management. All token budgeting, history compression, and truncation is handled automatically through a single configuration object.

How Context Window Works

Context Window is configured on a Pipeline 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 than keep_recent_turns), compression kicks in:
  1. Split - Separate items into old turns and recent turns (keeping the last N user exchanges).
  2. Render - Convert old items into human-readable text for the summarization prompt.
  3. Summarize - Call the LLM (or summary_llm) to generate a concise summary.
  4. Replace - Remove all old items and insert the summary as a system message tagged with the internal Summary source (rendered as [Conversation Summary]).
The summary preserves:
  • 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:
  1. Remove the oldest non-protected items one at a time.
  2. Function call/output pairs are removed together to avoid orphaned tool calls.
  3. Continue until both max_tokens and max_context_items are satisfied.
  4. 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.
That’s 3 tool calls + 1 text response = 4 rounds, well within 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.

References

Examples

Chat Context

Compress and truncate long conversations.

SDK Reference

Context Window

Context Window in the Python API reference.

Chat Context

ChatContext in the Python API reference.