Learning resourcesReviewed · Pydantic

Pydantic AI Compaction Guide

Pydantic AI's compaction guide is documentation for developers building conversational AI agents, but shrinking history requires care with provider support and retained context. It explains native compaction from OpenAI and Anthropic, custom history editing, and ready-made harness strategies for trimming or summarizing older messages.

Researched 15 Nov 2026

In short.

This guide explains conversation compaction for Pydantic AI developers, but its approaches differ in provider support and how they preserve context and tool state. It covers native provider APIs, model-independent history processors and harness strategies coordinated by TieredCompaction. Its most useful detail concerns what happens after older history is replaced: tools may need to be advertised again, provider artifacts have compatibility limits, and client-supplied compaction has trust implications. Package compatibility, monetary costs and measured outcomes are unknown from this page. Nothing was executed or tested.

Researched and written by an AI model and checked by a second AI model. How it works

Covers the complete selected page at [https://pydantic.dev/docs/ai/capabilities/compaction/index.md], including native compaction, client-held history, custom history editing and the harness overview. The opening documentation-index and query-parameter notices are present in the source; the referenced index and all linked pages are excluded. No code, API request, UI integration or compaction strategy was executed or tested. Licensing, monetary costs, release compatibility and measured outcomes remain unknown.

The overview

identity

The resource is the Pydantic AI documentation page titled Compaction at [https://pydantic.dev/docs/ai/capabilities/compaction/index.md]. It teaches ways to reduce conversation history as it approaches a model's context window, the amount of input the model can handle.

The page covers provider-native compaction, custom history editing and an overview of Pydantic AI Harness strategies. Detailed API references and related guides are linked, but their contents are outside this resource's selected scope.

assessment

The guide is useful for developers deciding how to keep long agent conversations manageable. Its strength is explaining the consequences of compaction for tool discovery, capability loading, provider switching and history supplied by a frontend.

It provides implementation guidance rather than measured results. Summary accuracy, task success, latency and actual token or cost savings are not established, and the page does not supply a complete runnable application.

users

The main audience is developers maintaining Pydantic AI agents whose conversations accumulate many turns or large tool results. Developers writing capabilities or toolsets also need the guidance on rebuilding state after a compaction boundary.

Frontend developers can use the client-history discussion to understand what happens when a browser sends conversation history back to the server. Beginners receive a short explanation of compaction, but the API discussion assumes familiarity with model requests, message history and tool calls.

capabilities

The documented native capabilities are OpenAICompaction for the OpenAI Responses API and AnthropicCompaction for Anthropic. Supported models also expose Model.compact_messages for compaction at an application-chosen moment.

For use across providers, a ProcessHistory capability can wrap custom history processing. Suggested approaches include retaining recent messages and summarizing older ones.

Pydantic AI Harness adds sliding-window trimming, clearing old tool results, deduplicating repeated file reads, clamping oversized message parts and LLM summarization. The page recommends TieredCompaction, which escalates from cheaper strategies to more expensive ones only as needed to meet the target.

Using it

requirements

Applying the guide requires access to a Pydantic AI application's message history and the relevant model or capability integration. Native compaction requires a supporting provider and model implementation; custom history processing is described as working with every provider.

An application can inspect ctx.context_window_used when deciding whether to compact. This is the fraction of the model's context window occupied as of the last response, and it can be None when unknown.

The page does not specify installation commands, minimum package versions, supported model identifiers or a complete environment configuration. Those requirements remain unknown within this scope.

mechanism

Native compaction produces a provider artifact that can replace earlier history in subsequent requests. Compatibility follows the provider's canonical system. A model from another provider may ignore that artifact and still need the full earlier history, which is why the guide advises retaining the complete stored message list.

A compaction part also establishes a visibility boundary for tool discoveries and capabilities loaded on demand. Authors should derive announcements and related state from post_compaction_window so that information is advertised again when the history carrying it has been replaced. Harness SummarizingCompaction records replaced capability loads and tool discoveries again after summarizing.

A custom summary belongs in a request-side CompactionPart with content set and without provider identity fields. Provider models send it as user-voice historical context inside a conversation-summary wrapper. Provider-generated compaction parts remain in their original ModelResponse and provider role.

setup

Choose the integration that matches the application: a native provider capability, a custom processor wrapped in ProcessHistory, or a harness strategy. Preserve recent context and tool-call integrity when replacing older messages.

For manual native compaction, the documented implementations are OpenAIResponsesModel.compact_messages and AnthropicModel.compact_messages. They return a ModelResponse containing the compaction part for message history. The instructions argument replaces the agent's instructions for the OpenAI compaction request, whereas Anthropic treats it as a focus for the summary.

Applications retaining history on the server should persist the full conversation and supply it through message_history. The guide says model adapters already omit history superseded by compatible native compaction, so stored history should not be trimmed around those boundaries.

example

An illustrative, unexecuted use case is a coding assistant whose conversation contains repeated file reads and large tool outputs. The documented harness strategies could deduplicate reads, clear older results or clamp oversized parts before using summarization if more reduction is needed.

If the application writes its own summary, it would replace the summarized messages with a ModelRequest containing a content-only CompactionPart while retaining recent context and valid tool-call relationships. The page supplies no concrete thresholds or measured outcome for this scenario.

Assessment & evidence

limitations

Replacing or removing older history can remove information needed later; this is a practical implication of the described mechanisms, and the page does not quantify it. Context-window usage may also be unknown, so a trigger cannot always rely on that value.

Boundary handling depends on what the serving model actually received. Foreign-provider artifacts, OpenAI parts lacking encrypted content and Anthropic parts lacking summary content do not hide earlier tool-callability evidence. A boundary emitted in the same response as a tool call is too late to change the context that produced that call.

Client-held history has specific limits. AG-UI carries compaction as activity messages that @ag-ui/client does not send back. A client can also replay compaction artifacts previously produced by the server's provider account, and opaque OpenAI artifacts cannot be inspected. When the application supplies its own server-side history, client compaction items are ignored; accepted client compaction never replaces the agent's system prompt.

reasoning

The choice depends on provider compatibility, the importance of older details and where authoritative history is stored. Native compaction is relevant when the required provider supports it. Custom processing or harness strategies are relevant when history editing must work across providers.

Keeping full history on the server is the documented response when opaque client-submitted artifacts are unacceptable. That also preserves earlier messages for models that cannot use another provider's compaction artifact. TieredCompaction is the page's recommended default, but no comparative measurements establish it as best for every workload.

alternatives

The page's alternatives are different approaches within Pydantic AI. A recent-message window removes older turns without a summarization call. Clearing tool results, deduplicating reads and clamping parts target particular sources of history growth. Summarization replaces older content with a shorter account, while native compaction delegates the operation to a supporting provider.

The evidence does not compare other agent frameworks or external compaction libraries, so their relative features and performance are not assessed.

evidence

The evidence consists of the complete selected Compaction documentation page. It supports descriptions of the named APIs, history representations, compatibility rules and client-history trust boundaries.

Linked API references, provider guides, harness documentation, UI documentation and the documentation index are outside the available evidence. No software source files, independent benchmarks or execution results accompany this page.

Access & freshness

costs

Monetary costs are unknown. The page gives no provider prices, compaction fees, summarization-model rates or example bills.

The guide describes recent-message trimming as zero-cost and most harness edits as requiring no LLM. This distinguishes them from model-based summarization; it does not establish that operating the application is free. Actual savings depend on the strategy and workload and are not measured here.

rights

The selected page provides no licence text or redistribution terms. Documentation reuse rights, underlying library licensing, required attribution and provider-specific terms are unknown from this evidence.

freshness

The supplied page snapshot was captured on 2026-11-15 at 07:55:59.440 UTC. Its source revision is sha256:4cbfd6e919c5ed13f88f1f188e02d1dd1279fd260833f5dc680ca72a73fbb1b3.

The page does not state a publication date, last-update date or applicable Pydantic AI release. The capture identifies the documented snapshot; compatibility with an installed version or later documentation remains unknown.

actions

Before implementation, confirm that the installed Pydantic AI version and chosen provider support the documented APIs, and establish current pricing. Choose whether the client or server holds conversation history before designing compaction handling.

Evaluate the chosen strategy on representative conversations. Check retention of important facts, tool-call integrity, rediscovery of tools and capabilities, and behavior when context usage is unknown. For frontend integrations, verify that compaction records survive the actual client protocol.

Measure usage, latency and task outcomes before claiming savings or improved performance. If switching providers, verify that retaining full server history supplies the context required by a model that cannot use the existing compaction artifact.

Evidence for this report

selected-page.txtselected · checked 2026-11-15