AI Space

AI Models & Assistants / Knowledge library

Context Compaction

Replaces selected history with a shorter representation while retaining needed facts

Long-running agent context technique

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.

  1. Define Context Compaction and identify its place in an agent architecture.
  2. Choose a task where Context Compaction improves an agent system.
  3. Diagnose one failure caused by misusing Context Compaction.
  4. Interpret a trace or state transition involving Context Compaction.
  5. Explain one operational tradeoff of Context Compaction.

Documentation and sources

Connect the ideas

Explore next

Agent Context ManagementAgent MemoryAgent Checkpointing

Real World Example

Illustrative business scenario · AI-generated from linked documentation · Not a measured customer case study

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.

  1. Identify the specific handoff point where context volume becomes excessive.
  2. Configure the RunConfig to enable nest_handoff_history=True for the target handoff.
  3. Optionally define a handoff_history_mapper to customize how the transcript is summarized.
  4. Execute the agent chain, allowing the SDK to automatically place assistant summary segments around lossless message items.
Handoff Compaction Flow · Process flow

Visualizing the transformation of raw history into a compacted transcript during a handoff.

Agent 1

Initial analysis agent

Raw Transcript

Full history of interactions

Compactor

SDK logic with nest_handoff_history=True

Compacted Input

Summary segments + lossless items

Agent 2

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.

python · Illustrative code; not executed or production-validated.

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.

Sources

Generated 2026-10-18 with gemini-3.1-flash-lite. Your assessment includes two questions about this example.

Catalog source: technology-maxxing. This page reproduces the existing catalog’s learning material and source links.