docs/capabilities/compaction.md
As a conversation grows, its message history can approach the model's context window. Compaction keeps it in check by shrinking older messages — trimming, clearing, or summarizing them — while preserving recent context and tool-call integrity. Pydantic AI supports this at several levels, from provider-native APIs to model-agnostic history editing.
Some providers expose a built-in compaction API that runs on their side. Pydantic AI wraps these as capabilities:
| Provider | Capability | Details |
|---|---|---|
| OpenAI Responses API | [OpenAICompaction][pydantic_ai.models.openai.OpenAICompaction] | OpenAI compaction |
| Anthropic | [AnthropicCompaction][pydantic_ai.models.anthropic.AnthropicCompaction] | Anthropic compaction |
Each uses the corresponding provider API, so it's only available on that provider.
To compact on any model, edit the message history yourself with a history processor wrapped as a [ProcessHistory][pydantic_ai.capabilities.ProcessHistory] capability — this works with every provider. Common patterns:
Pydantic AI Harness packages a menu of ready-made, model-agnostic compaction strategies: mostly zero-LLM history editing — sliding-window trimming, clearing old tool results, deduplicating repeated file reads, clamping oversized message parts — plus LLM summarization for when that's not enough, and a TieredCompaction orchestrator (the recommended default) that escalates from cheap to expensive strategies only as far as needed to fit the target.