Skip to main content

Module context_window

Module context_window 

Source
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§

ContextBreakdown
Where the tokens of a conversation go (for /context).
ContextBudget
Context budget of one request: the window, the output reserve and the automatic-compaction threshold.
PruneStats
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 text into chunks of at most max_chars characters, on line boundaries where possible.
context_tool_definitions
context_note and rewind_context, handled by the agent loop itself.
context_window_for
Context window for model: an explicit model_context_windows entry, then an explicitly configured model_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_tokens tokens. 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 to target_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 to budget_tokens counting 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