Add a summary token budget to `SummarizingCompaction`

Closed
LI
1mo ago
+366-4

Closes #11437

The summary prompt had no length target, so repeated compaction could grow the summary. Add brevity guidance and an optional budget, and reject output-limit-truncated summaries before they replace history.

New public surface: keyword-only SummarizingCompaction.max_summary_tokens: int | None = None.

Before: no summary-specific output cap. After: SummarizingCompaction(max_messages=60, max_summary_tokens=1024) states 1024 tokens in the prompt and caps the summary request at the smaller of that budget and its effective max_tokens. Incremental summaries rewrite the previous summary within the budget. A finish_reason='length' response is rejected and retried once with the effective limit restated; another truncation raises UnexpectedModelBehavior, allowing a configured FallbackCompaction to try its next strategy. Partial summaries do not replace history.

Verification: Regression tests: 38 passed, plus both capability composition-policy checks, including empty/partial truncation, retry recovery/exhaustion, preserved history and fallback. Compaction, documentation parity and snippet checks: 2270 passed, 82 skipped, with two POSIX workspace test files excluded on Windows. Ruff check/format and targeted Pyright pass. The construction example runs unchanged; no live model requests.

Existing users: the prompt becomes more concise and truncated summaries fail after one retry instead of replacing history. Output settings stay unchanged when the optional budget is omitted. An explicit budget is recommended; a non-None default awaits benchmarking.

The requested review changes also remove recursive model inspection, explicit summary-time model inference, and budget override warnings. Settings are capped after normal merging. Prompt template edits remain limited to brevity and budget instructions.

Implemented with Codex. Draft pending maintainer review.

PR decisions

2026-10-27 — pre-push review 1 of 3 at ecefb56e436d12c19a8664039171c523ae6b4035

Decision: completed; remediated model-default limit handling, explicit override warning, and skill reference documentation.
Why: reviewed tree 3072d511f370a947287709a1f585e0378127c2c2; full branch scope f0358b2fe0273262645a2d96a4dd9452aa5ac83f..ecefb56e436d12c19a8664039171c523ae6b4035.
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-27 — pre-push review 2 of 3 at 55700983f0b5ea22c9f04addaed9e54b01075648

Decision: reserved before dispatch.
Why: tree dea853f48b286cfca9292f3998bac285ecc57e7f; follow-up scope ecefb56e436d12c19a8664039171c523ae6b4035..55700983f0b5ea22c9f04addaed9e54b01075648, full branch context f0358b2fe0273262645a2d96a4dd9452aa5ac83f..55700983f0b5ea22c9f04addaed9e54b01075648.
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-27 — outcome of pre-push review 2 at 55700983f0b5ea22c9f04addaed9e54b01075648

Decision: remediated fallback defaults and capability settings cap.
Why: two actionable findings verified; added four FunctionModel regression cases and run-level settings cap.
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-27 — pre-push review 3 of 3 at dd02716c51c26d170e438253c4a6afd730749d91

Decision: reserved before dispatch.
Why: tree bc9facc982274529b60d9919a46b4946c8f1a25c; follow-up scope 55700983f0b5ea22c9f04addaed9e54b01075648..dd02716c51c26d170e438253c4a6afd730749d91, with full branch context from f0358b2fe0273262645a2d96a4dd9452aa5ac83f.
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-27 — outcome of pre-push review 3 at dd02716c51c26d170e438253c4a6afd730749d91

Decision: remediated capability-selected model limit handling; review budget exhausted (3 of 3).
Why: final local commit 058691b69f664a9ab360256cba6727c5d9acaac4, tree 113f11cfd141818b6e8d12c853e4cf06ea962bed; 927 tests passed, 17 skipped; full Ruff and targeted Pyright passed. Published the same five files as remote commit 30777774369a6c8d90efa17b0627f8c85411793c on fork main 24e03f1bae46dae27796ddb8aaae912cef0e1668 (all original changed files verified equal to local baseline).
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-27 — wrapper-model regression coverage

Decision: add the missing wrapper-model case without changing runtime code; the review budget remains exhausted (3 of 3).
Why: final local commit 644944d53c8da479d2313bb15b7dc670ffcb9fa9, tree ebface43c3d02045c2822210b96076f21f0b7aae. The 23 focused tests pass with no missing new runtime lines or branches; Ruff and Pyright pass. Remote follow-up commit cce21f84e52ecaf33821001e1dd66e4bccc75a2b.
Source: https://github.com/LittleMIKUya/pydantic-ai/tree/fix/11437-summary-token-budget

2026-10-28 -- Review follow-up at 27829e330d7f722e4711856613c24b1d409cb541

Decision: follow DouweM's requested changes: reject length-stopped summaries through core output retries; retry once with the effective budget restated, then raise UnexpectedModelBehavior for configured FallbackCompaction. Remove recursive model inspection, explicit infer_model and budget override warnings. Keep default None, recommend a budget, and limit template edits to brevity and budget instructions.
Why: reproduced all nine original truncation/fallback cases failing on the previous head. The updated focused suite has 38 passing cases. Broad verification: 2270 passed, 82 skipped with UTF-8; two POSIX workspace test files excluded on Windows. Full Ruff check/format and targeted Pyright passed. Documentation parity finding remediated. Candidate tree b0a777878748b956d82a11e67db2cc0903d2eae1. The existing 3-of-3 substantive review budget remains exhausted; no fourth dispatch.
Source: https://github.com/pydantic/pydantic-ai/pull/11544#pullrequestreview-5672944894

2026-10-28 -- Published review follow-up

Decision: pushed follow-up commit e1c9c24194a615467b5d5cf6c2feed3e036f84db to the existing PR, preserving prior commits. Metadata and documentation parity review corrections applied.
Why: the review requested simpler settings and safe failure for truncated summaries; runtime and tests passed the focused local verification above.
Source: https://github.com/pydantic/pydantic-ai/pull/11544

2026-10-28 -- CI composition-policy follow-up

Decision: register the internal _CompleteSummary validator as Anonymous in the existing COMBINE_POLICY table. Repeating the validator checks the same response; no runtime behavior changed.
Why: CI on e1c9c24194a615467b5d5cf6c2feed3e036f84db identified this missing declaration in test_every_capability_declares_a_combine_policy. Both the registry guard and its _CompleteSummary policy case now pass with the 38 summary tests (40 passed total). Full Ruff check/format passed. Local commit edf76708e2edc8e6077a4c995d463d2999565a27, tree dc7b16661e285c79d2f30d26fae7abeb2c330f64; remote follow-up 7b0e110be287f7ed03652625b9162d6f6ea43382.
Source: https://github.com/pydantic/pydantic-ai/actions/runs/40618040629/job/122585085712

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it. (None.)
  • PR title is fit for the release changelog.