Initial analysis agent
AI Models & Assistants / Knowledge library
Context Compaction
Replaces selected history with a shorter representation while retaining needed facts
Why learn Context Compaction?
It extends useful run length and lowers repeated input cost when complete history no longer fits.
The problem it solves
It controls context growth without simply deleting all earlier progress.
Where it came from
Compaction may summarize older turns, preserve recent or lossless items, and retain identifiers required by tools or APIs. A poor summary can silently remove constraints, provenance, or unresolved tasks.
Build your understanding
Work through these exercises in order. Try the task before opening a reference.
- Define Context Compaction and identify its place in an agent architecture.
- Choose a task where Context Compaction improves an agent system.
- Diagnose one failure caused by misusing Context Compaction.
- Interpret a trace or state transition involving Context Compaction.
- Explain one operational tradeoff of Context Compaction.
Documentation and sources
- OpenAI Agents SDK: Running agents (openai.github.io)
Connect the ideas
Explore next
Real World Example
Optimizing Multi-Agent Financial Analysis
Financial research firms managing complex, multi-step investment analysis workflows.
The business problem
Long-running agent chains often accumulate massive transcripts, leading to excessive token consumption and potential context window overflow during handoffs between specialized agents.
How this topic solves it
Context compaction replaces selected history with a shorter representation while retaining needed facts, specifically by enabling ordered transcript compaction during agent handoffs.
- Identify the specific handoff point where context volume becomes excessive.
- Configure the RunConfig to enable nest_handoff_history=True for the target handoff.
- Optionally define a handoff_history_mapper to customize how the transcript is summarized.
- Execute the agent chain, allowing the SDK to automatically place assistant summary segments around lossless message items.
Visualizing the transformation of raw history into a compacted transcript during a handoff.
Full history of interactions
SDK logic with nest_handoff_history=True
Summary segments + lossless items
Final synthesis agent
- Agent 1 → generates → Raw Transcript
- Raw Transcript → input → Compactor
- Compactor → transforms → Compacted Input
- Compacted Input → context for → Agent 2
Implementation example
This code demonstrates how to enable the compaction feature within the agent SDK to manage history size during handoffs.
from agents import RunConfig
# Enable nested handoff history to compact the transcript
config = RunConfig(nest_handoff_history=True)
# Apply this config to a specific handoff to manage context size
# agent.handoff(target_agent, nest_handoff_history=True)Expected business outcome
Improved efficiency in multi-agent workflows by reducing the total input size passed to subsequent agents while maintaining essential conversation history.
Limitations and checks
Compaction is an automated process that may lose nuance; verify accuracy by monitoring the agent's ability to recall specific details from the compacted segments.