[FEATURE] Add an overridable `Model.compact()` and route summarization through it
Human Overview
Summarization should go through the model, so each provider can decide how to compact. Model.compact() gets a working default that does today's client-side summary, and providers that support native compaction override it, the same way countTokens() works.
SDK Language
Both / Not language-specific
Problem Statement
SummarizeStrategy builds the summary itself, on the client. A provider that can compact server-side, keeping preserved thinking valid, has no way to take over. The strategy also has nothing to tell it where a safe cut point is (a user-prompt boundary, never between a toolUse and its toolResult).
Proposed Solution
Part 1 of design 0022:
- Add a concrete
Model.compact(), not an abstract one. The default is today's client-side summary, and it returns[summary, ...preserved]orundefined.- TS:
compact(messages, options?: CompactOptions)withpreservedMessages,systemPrompt,toolSpecs,instructions. - Python:
compact(messages, *, preserved_messages=0, tool_specs=None, system_prompt=None, instructions=None).
- TS:
- Add a shared
compactionSplitIndexhelper. It moves the cut back to a user-prompt boundary, never splits atoolUse/toolResultpair, and never summarizes pinned messages. - The strategy decides when to compact and the model decides how. Message-level summaries in
SummarizeStrategyalways callmodel.compact()(through_compact()), passing the live system prompt and tool specs, andSummarizeConfig.systemPromptasinstructions. BaseOffloadStrategygains_isRetroactive(_isMessageLevel || _preserveRecent > 0). A retroactive per-block summary callscompact()only when the model reportsserverCompaction === true. Otherwise it keeps today's behavior.
Done when:
- Python and TypeScript have unit tests for the default
compact(),compactionSplitIndex(tool pairs, pinned messages, user-prompt boundaries), and the strategy routing. - Summarization output and behavior are unchanged for models that don't override
compact().
Use Case
A provider adds native compaction by overriding one method, and every summarize strategy (context_manager="auto", agentic, proactive_summarization) picks it up.
Additional Context
- Blocks: #5401, #5404
- Design:
team/designs/0022-server-side-compaction-support.md(#5242) - Reference (earlier prototype, not the spec): #5287
As-built notes from #5470, which resolves this issue. They record where the implementation differs from the spec above.
-
Name and shape. The method shipped as
Model.summarize(messages, *, tool_specs, system_prompt, system_prompt_content, instructions, cancel_signal)and returnsSummarizeResult { summary, usage }. It is notcompact()returning[summary, ...preserved]. The strategy keeps ownership of the cut (_compaction_split_index) and of placement. It is marked experimental: treat it as a placeholder until the API follow-up below settles the final shape. -
Routing. For now, the strategy detects an override by function identity. A model counts as overriding when
summarizeis replaced, by a subclass or on the instance. Only an override gets the conversation's opening prefix; every other selection gets the base summary. TheModel.summarizedocstring states this as the contract. #5466 replaces the identity check with a capability. -
Behavior change for models that don't override. The "unchanged" acceptance line does not fully hold:
- The base summary keeps message 0 verbatim.
- A recency pass's cut moves off the last message, so a text summary never joins the live turn.
- With
preserve_recent≤ 1 (or 2 with a trailing tool pair), a pass whose only remaining candidate is the last message is now skipped. In the reviewer's fuzz, that is 85 of 3,000 histories thatmainsummarizes. The built-in configs never hit it.
These are described in the PR. The skipped case is tracked in #5736.
Follow-ups: #5733 (final API shape), #5734 (evaluation), #5735 (summarizer prompt), #5736 (skipped passes).