* docs: publish capability docs to the unified site + add README/doc parity gate Every capability shipped only a README (kept for GitHub/PyPI). This adds a parallel, cleaned-up page per capability under docs/ for the new unified docs site (pydantic.dev/docs/harness), migrated from each README: snippets verified runnable against source, autodoc API blocks, root-relative Pydantic AI links, and an experimental-status admonition on the experimental set. To keep README and doc in sync going forward, adds a docs-parity-reviewer agent and a parity gate in the review checklist (run as the last step before merge), plus the docs/ layout and the README<->doc requirement in AGENTS.md and the capability-authoring guide. * docs: fix README<->doc<->source inconsistencies across capabilities A parity audit against source found drift, mostly in the capability READMEs (staler than the migrated docs). All fixes verified against source: - Correctness: the "approval/deferred tools are excluded from the sandbox" claim (code_mode README + doc) was false -- those tools are sandboxed like any other; corrected in both. The stale Shell persist_cwd sentinel description is replaced with the actual out-of-band temp-file capture. filesystem protected default `.git/` -> `.git/*` (the bare form never matched). - Runnable snippets: added the missing imports/wiring so README snippets no longer raise NameError (subagents, context, planning, overflow, authoring, filesystem, code_mode). - Parity: documented previously-undocumented params/behaviors (compaction strategy options, overflow strip_ansi/Passthrough, extra autodoc classes for context and subagents), fixed a stale version pin (>=1.95.1 -> >=2.1.0), and added the missing Managed Prompt row to the root README capability matrix. - Style: normalized decorative Unicode to ASCII across all READMEs and dropped a hype phrase, matching AGENTS.md writing style and the docs. * docs: add nav.json to drive the unified-docs harness sidebar The unified docs mount the harness docs under /docs/ai/harness (fed live from this repo via the pydantic-ai 'Pydantic AI Harness' section). This nav.json defines the sub-nav (Overview + Capabilities + Experimental) and the set of doc files the site includes. * docs: migrate "What goes where?" explainer into harness overview Adds the core-vs-harness boundary section (anchor #what-goes-where) to the canonical harness overview, so the pydantic-ai docs that link to it can point here after the duplicated in-repo stub is removed. * docs: address CodeRabbit review -- runnable snippets, accuracy, multi-class autodoc * docs: flatten harness nav and align with graduated capabilities Following the experimental-graduation refactor (#347), restructure the unified-docs harness pages: - Flatten docs/ (drop capabilities/ and experimental/ subdirs); the sidebar is now Overview + one flat list per Douwe's request. - Rename to match the graduated modules: overflow -> overflowing-tool-output, authoring -> runtime-authoring, docs -> pydantic-ai-docs. - Drop the 'Experimental' admonitions from the graduated capabilities and repoint every import + ::: autodoc path off pydantic_ai_harness.experimental. - Add docs for the newly-shipped capabilities: guardrails, dynamic-workflow, media, and acp (acp stays framed as experimental -- it may still be removed). - Every capability doc now links to its source; index capability table lists the full set with flat links. * docs: apply team-sync authoring rules + enforce them in CI From the 2026-07-10 docs review on #329: - Purpose-first leads: drop hook names (before_model_request, after_tool_execute) from the opening paragraphs of compaction and overflowing-tool-output (doc + README); mechanism moves lower. - Mirror the soft 'API may change between releases' stability note from each graduated README into its doc page (ACP keeps its stronger experimental warning; guardrails' README has no note, so its page gets none). - README H1s now use the capability's display name (Overflow capability -> Overflowing Tool Output, RuntimeAuthoring -> Runtime Authoring, SubAgents -> Subagents, etc.). - Extend tests/test_docs_parity.py with per-page mechanical checks: source link present, heading matches the capability name, purpose-first lead (no hook in the opener), and no experimental framing on graduated pages (ACP excepted). - Update the docs-parity-reviewer agent + review-checklist to the flat structure and the new semantic checks. * docs: add the stability note to guardrails (parity with sibling capabilities) guardrails was the one graduated capability whose README and doc page lacked the shared 'API may change between releases' note. Add it to both. * fix: restore uv.lock to match pyproject (bad text-merge dropped 8 lines) Merging origin/main did a git text-merge of the generated uv.lock, leaving it inconsistent with pyproject.toml -- every CI job failed at 'uv sync --locked'. pyproject.toml is identical to main here, so the correct lock is main's. * docs: address CodeRabbit review on #329 Findings that failed to post inline (GitHub error) but were real: - context/README.md, planning/README.md: two nested examples still imported from pydantic_ai_harness.experimental.* -- repoint to the graduated modules. - guardrails/README.md: replace em dashes with '--' (repo style) and add the source-module link. - docs/media.md: standardize on the implementation's canonical media+sha256:// URI scheme (was mixing media://). - tests/test_docs_parity.py: strengthen my own checks per review -- source-link and top-README-link now require a real Markdown link to the page's specific module (not a bare substring); heading checks assert an H1 exists and equals the expected capability name via explicit page metadata. * fix: restore uv.lock [options.exclude-newer-package] block The lock lost its [options.exclude-newer-package] manifest (pydantic-ai-slim = false, ...) -- a bad git text-merge dropped it, and diagnostic uv commands rewrote it under a different local config. Without that block CI's 'uv sync --locked' re-resolves and fails ('addition of exclude newer exclusion for pydantic-ai-slim'). Restore origin/main's exact lock. * fix: restore uv.lock [options.exclude-newer-package] block A pre-commit hook was rewriting uv.lock under the local uv config, stripping the [options.exclude-newer-package] manifest (pydantic-ai-slim = false, ...). Without it CI's 'uv sync --locked' re-resolves and fails. Commit origin/main's exact lock with --no-verify so no hook mutates it (lock-only change). * test: cover the docs-parity helper edge cases (100% coverage) The strengthened helpers added defensive branches (missing frontmatter close, fenced code before the lead, missing/forbidden/ClassName H1, lead running to EOF) that no real doc exercises. Add direct unit tests so the file is back to the repo's required 100% coverage. * docs: link every capability README to its source module + enforce it CodeRabbit re-flagged planning/README.md for a missing source link. Only guardrails had one, so add the source-module link to all 15 remaining capability READMEs (matching the doc pages) and add a parity test so the requirement is mechanical and cannot silently regress. * docs(agents): drop stale folder tree; fix flat docs path + guard names AGENTS.md's File-structure ASCII tree and capability-authoring's doc paths still showed docs/capabilities// docs/experimental/ (flattened in this PR) and the old /docs/harness URL. Delete the tree rather than redraw it -- the layout is discoverable by listing the repo; keep only the non-obvious conventions (flat docs/, the README<->doc parity requirement). Also fix the Vocabulary guard examples (InputGuard/OutputGuard, not the nonexistent InputGuardrail/ CostGuard). * test: statically validate doc snippets exist and parse Every Python snippet in the capability READMEs and docs/*.md pages is now checked for the two failures a reader hits immediately: it does not parse (syntax), or it imports a pydantic_ai_harness symbol that does not exist (stale module path or renamed name -- the class of bug behind the experimental.* import drift). Static only: no model/network execution, so it needs no mocking. The four illustrative API-signature blocks opt out with a {test="skip"} fence (read by pytest-examples, stripped-safe for the unified-docs render). * test: don't fail doc-snippet check on a missing optional extra The static check imported capability modules to resolve their symbols, but in the slim CI job (no extras) importing e.g. pydantic_ai_harness.experimental.acp raises ModuleNotFoundError for the absent third-party 'acp' package -- the harness module exists, its extra just isn't installed. Distinguish a genuinely missing harness module (fail) from a missing extra (skip) by the ImportError's module name.
11 KiB
Compaction
Note
Import these capabilities from their submodule -- there is no top-level
pydantic_ai_harnessre-export:from pydantic_ai_harness.compaction import TieredCompactionThe API may change between releases. Where practical, breaking changes ship with a deprecation warning.
A menu of strategies for keeping an agent's conversation history within a model's context
window. Each is a Pydantic AI Capability that edits the message history just before each
request goes out; edits persist into the run's message history, so a trim/clear/summary carries forward to later
steps (it is not recomputed from the full history every turn).
All strategies preserve tool-call / tool-return pairing -- core does not validate this, and a provider rejects an orphaned pair. The zero-LLM strategies never call a model.
The menu
| Capability | Cost | What it does | Reach for it when |
|---|---|---|---|
ClampOversizedMessages |
zero-LLM | Head/tail-truncates a single oversized part (response text, tool-call args) | One runaway generation blew past the context cap and no other strategy can reach it |
SlidingWindow |
zero-LLM | Drops the oldest whole messages down to a tail | You only need the recent turns and can discard old context entirely |
ClearToolResults |
zero-LLM | Blanks the content of old tool results in place, keeping the last keep_pairs |
Tool outputs dominate context and can be re-fetched on demand (the cheap first tier) |
DeduplicateFileReads |
zero-LLM | Blanks every file read superseded by a newer read of the same file | The agent re-reads files and only the latest version matters |
SummarizingCompaction |
one LLM call | Summarizes older messages into a structured summary, keeping the recent tail | Old context still matters but must be compressed; use behind the cheap tiers |
TieredCompaction |
escalates | Runs cheap passes first, summarizes only if still over target_tokens |
You want a sensible default: spend the expensive summary only when needed |
LimitWarner |
zero-LLM | Injects an URGENT/CRITICAL warning as limits approach | You want the agent to wrap up rather than have its history rewritten |
Triggers
Every size-based strategy triggers on max_messages and/or max_tokens (estimated). Token counts
use a ~4-chars-per-token heuristic by default; pass a tokenizer callable (e.g. tiktoken) for
accuracy. DeduplicateFileReads runs on every request when no trigger is set (it is cheap and
near-lossless). TieredCompaction triggers and stops on a single target_tokens budget.
ClampOversizedMessages triggers per part (max_part_tokens / max_part_chars), not on the
whole history -- the failure it targets is one oversized part, not a large total.
ClampOversizedMessages: surviving a runaway generation
A single model response of repeated whitespace, or a single tool call with a giant payload, can
produce one part so large the next request exceeds the provider's context cap. None of the other
strategies can reach it: SlidingWindow drops the oldest messages but the offender is the newest;
ClearToolResults only touches tool results; LimitWarner never edits history; and feeding the
history to SummarizingCompaction hits the same cap.
ClampOversizedMessages truncates the offending part in place, keeping a head slice and a tail slice
with a [clamped: removed N of M characters] marker between them. Degenerate generations are
low-entropy repetition, so a head/tail slice loses little.
from pydantic_ai import Agent
from pydantic_ai_harness.compaction import ClampOversizedMessages
agent = Agent(
'openai:gpt-4o',
capabilities=[ClampOversizedMessages(max_part_tokens=50_000, keep_head_chars=2_000, keep_tail_chars=2_000)],
)
A part is clamped only when it is oversized and the clamp actually shrinks it, so keep
keep_head_chars + keep_tail_chars well below your per-part threshold.
It clamps two kinds of part inside each ModelResponse:
- Response text (
TextPart) -- the critical case, a runaway model-response text part. - Tool-call args (
ToolCallPart), whenclamp_tool_call_args=True(default) -- the same failure shape for a giant payload (e.g. a runawaywrite_plan). The args are replaced with a small JSON object{"_clamped": "<head>...<tail>"}so they stay valid function arguments; the original call already executed, so this only shrinks the history copy. Setclamp_tool_call_args=Falseto clamp response text only.
Request-side parts (user prompts, tool returns, system prompts) are deliberately out of scope:
user input should not be silently rewritten, and oversized tool returns are the job of
ClearToolResults.
Use it as the first tier of TieredCompaction, before ClearToolResults:
from pydantic_ai_harness.compaction import (
ClampOversizedMessages,
ClearToolResults,
TieredCompaction,
)
TieredCompaction(
tiers=[
ClampOversizedMessages(max_part_tokens=50_000),
ClearToolResults(max_tokens=1, keep_pairs=3),
],
target_tokens=120_000,
)
SlidingWindow and ClearToolResults options
SlidingWindow keeps the last keep_messages down to a tail; pass keep_tokens instead for a token
budget rather than a message count. By default preserve_first_user_message=True keeps the first user
turn even when it falls outside the window, so the agent does not lose the original task.
ClearToolResults keeps the last keep_pairs intact. Set clear_tool_inputs=True to also blank the
arguments of the cleared calls, and exclude_tools to a set of tool names whose results are never
cleared.
LimitWarner thresholds
Warnings begin at warning_threshold (default 0.7, a fraction of the limit) and escalate to CRITICAL
for iterations once the remaining request count drops to critical_remaining_iterations (default 3).
It watches max_iterations, max_context_tokens, and max_total_tokens, warning on whichever are
configured; narrow that with warn_on.
Cost: why summarization is the last resort
Summarization turns input tokens into output tokens, which are billed at a premium and generated
serially -- so it is genuinely expensive. The zero-LLM strategies touch only the cheaper input side.
The field consensus (Anthropic, OpenCode, Letta) is to clear/dedupe first and summarize only when
that is not enough -- which is exactly what TieredCompaction encodes:
from pydantic_ai import Agent
from pydantic_ai_harness.compaction import (
ClearToolResults,
DeduplicateFileReads,
SummarizingCompaction,
TieredCompaction,
)
agent = Agent(
'openai:gpt-4o',
capabilities=[
TieredCompaction(
tiers=[
DeduplicateFileReads(file_key=my_file_key),
ClearToolResults(max_tokens=1, keep_pairs=3),
SummarizingCompaction(max_messages=1, keep_messages=20), # model inherits the run's
],
target_tokens=120_000,
)
],
)
A tier inside TieredCompaction is driven directly by the orchestrator, which re-measures after each
and stops once under target_tokens -- so a tier's own max_* trigger is irrelevant there (set it to
anything valid). Any object with async def compact(messages, ctx) -> list[ModelMessage]
(CompactionStrategy) can be a tier, so you can plug in your own.
Cache tradeoff (read before using ClearToolResults)
Clearing or deduplicating rewrites message content, which invalidates the provider's prompt cache
from the edit point onward -- the next request pays a cache-write. Use ClearToolResults'
min_clear_tokens to skip clearing that reclaims too little to be worth busting the cache.
Model inheritance
SummarizingCompaction(model=...) accepts a model name or Model; when left None it inherits the
running agent's model. No token caps are imposed on the summary call.
By default incremental=True extends an existing summary from a prior compaction rather than
regenerating it from scratch, and preserve_first_user_message=True keeps the original task turn even
when it falls outside the window. Pass keep_tokens to trim the retained tail to a token budget instead
of keep_messages.
Usage accounting
The summary call is a real request to the model, so its full usage -- tokens and the request
itself -- is folded into the run's ctx.usage. This is deliberate: it keeps cost honest, keeps the
request count consistent (a model request that didn't count as one would be the surprise), and lets a
UsageLimits request limit catch a runaway compaction. A run-request / iteration limiter will
therefore see compaction calls among its requests.
DeduplicateFileReads.file_key
There is no default file_key: identifying a file read is agent-specific, and a wrong guess would
drop live data. Supply a callable mapping a ToolCallPart to a stable file key, or None when the
call is not a file read:
from pydantic_ai.messages import ToolCallPart
def my_file_key(call: ToolCallPart) -> str | None:
if call.tool_name != 'read_file':
return None
args = call.args
return args.get('path') if isinstance(args, dict) else None
Tracing
When core instrumentation is active (the Instrumentation capability, agent.instrument, or
Agent.instrument_all()), each strategy emits a compact_messages span on the run's tracer the
moment it actually compacts -- that is, in before_model_request, once the strategy's threshold is
exceeded (ClampOversizedMessages emits only when a part is actually clamped). TieredCompaction
emits a single span for the whole escalation rather than one per tier, because it drives each tier's
compact directly. Without instrumentation the tracer is a no-op, so the span adds no overhead.
The span name is the static compact_messages; the strategy is an attribute, not part of the name,
to keep span cardinality low. Attributes:
| Attribute | Type | Meaning |
|---|---|---|
gen_ai.conversation.compacted |
bool | Always true; the OpenTelemetry GenAI convention's flag for a compacted context |
compaction.strategy |
str | Strategy class name (e.g. SlidingWindow, SummarizingCompaction) |
compaction.messages_before |
int | Message count before compaction |
compaction.messages_after |
int | Message count after compaction |
compaction.tokens_before |
int | Estimated token count before compaction |
compaction.tokens_after |
int | Estimated token count after compaction |
gen_ai.conversation.compacted is the GenAI semantic convention's flag; the rest is
harness-specific. Token counts use the strategy's tokenizer when set, otherwise the
~4-chars-per-token heuristic.
Raw message content is not recorded.
Out of scope
These strategies compress or drop context inside the window. Moving large tool outputs out of the window -- overflowing them to a file the agent (or a subagent) can query on demand -- is a separate capability, not lossy truncation. Prefer it over capping individual tool outputs.