Expand description
Token-aware context management: per-model window sizes, token estimates, tool-output pruning and the pieces of a structured compaction.
Everything here is pure (no I/O, no provider calls) so it can be tested
directly; agent_engine::compact_messages drives it.
Structs§
- Context
Breakdown - Where the tokens of a conversation go (for
/context). - Context
Budget - Context budget of one request: the window, the output reserve and the automatic-compaction threshold.
- Prune
Stats - Result of
prune_tool_results.
Constants§
- MAX_
AUTO_ COMPACT_ PCT - The automatic-compaction threshold never goes above this share of the usable window, so a long tool loop cannot run straight into an overflow (and a compaction cannot immediately re-trigger itself).
- SUMMARY_
MARKER - Prefix of the user message that carries a compaction summary.
- SUMMARY_
SYSTEM_ PROMPT - Instructions for the summarizer. The sections are what a coding (or any long-running) session needs to continue without the original history.
Functions§
- breakdown
- budget_
note - One line telling the model how full its context is.
- chunk_
text - Split
textinto chunks of at mostmax_charscharacters, on line boundaries where possible. - context_
tool_ definitions context_noteandrewind_context, handled by the agent loop itself.- context_
window_ for - Context window for
model: an explicitmodel_context_windowsentry, then an explicitly configuredmodel_context_window, then the built-in table, then the default. - estimate_
block_ tokens - estimate_
message_ tokens - estimate_
messages_ tokens - estimate_
text_ tokens - Rough token count of a string: four bytes per token for ASCII-heavy text, three for text dominated by multi-byte characters (CJK).
- fallback_
summary - Summary written without a model, used when summarization fails: it keeps the earlier summary, the user’s requests, the files changed and the last commands run.
- is_
summary_ message - keep_
recent_ budget - Index at which the kept (verbatim) tail starts when keeping roughly the
last
keep_tokenstokens. The tail always starts on a plain user message (never between a tool call and its result) and keeps at least the last such message. Returns 0 when there is nothing older to summarize. Tokens of recent history kept verbatim by a compaction that must bring the conversation down totarget_tokens: the configured tail, but never more than half the target. Otherwise a small window (a local model, an eval case) whose whole history fits in the configured tail could never be compacted and would run into a context overflow instead. - previous_
summary - The summary text carried by a leading summary message, if any.
- prune_
tool_ results - Clear older tool outputs without calling a model.
- recent_
user_ requests - The user’s own words in
messages(newest last), excluding tool results and summaries, capped tobudget_tokenscounting from the newest. - render_
for_ summary - Render messages for the summarizer: tool results and tool inputs are capped so one huge output cannot crowd out the rest.
- summary_
message_ text - The user message that replaces summarized history: the summary plus the facts re-injected verbatim (the user’s own requests and the files changed).
- summary_
request - token_
split - touched_
files - Files changed through the file tools in
messages. - truncate_
chars