Context and compaction
Every model has a limit on how much text it can read at once. Raw tracks the size of what the model will receive, shows you that size, and can summarize older turns so a long session keeps working. This page explains the estimates, compaction and cache behavior.
Context estimate
Section titled “Context estimate”The context estimate covers three things: the system prompt, the tool definitions the agent selects, and the messages that are still active. It is an estimate of size, not a count from a tokenizer. It is marked with ~ in the terminal footer.
- If the last response reported token counts, Raw uses that count as a baseline and adds an estimate for what changed since.
- Otherwise, the whole context is estimated.
- A percentage appears only when the model sets
context_window_tokens. Without it, Raw has no window to measure against.
To get a percentage, set the window in the model entry:
{ "models": { "flash": { "provider": "deepseek", "method": "openai-chat-completions", "model_id": "deepseek-flash", "base_url": "https://api.deepseek.com", "api_key_env": "DEEPSEEK_API_KEY", "context_window_tokens": 1048576 } }}context_window_tokens is a value you supply. Raw does not look it up from the provider.
Compaction
Section titled “Compaction”Compaction replaces the model’s older context with a summary. The visible history stays as it was, so you can still scroll back through the conversation.
Compaction runs in one of two ways:
- Automatic. This is on by default when the model sets
context_window_tokens. Before a model request would exceed the trigger, Raw compacts first and shows its progress. The default trigger is 80% of the input budget, which is the context window minus the output reserve and a safety margin. Setcompact.trigger_tokensto choose another threshold, or tofalseto turn automatic compaction off. - Manual. Run
/compactin the REPL or the dashboard at any time. Without a declared context window, or withtrigger_tokensset tofalse, this is the only way compaction runs.
{ "agents": { "deepseek": { "model": "flash", "tools": { "use": ["builtin/read_file", "builtin/write_file", "builtin/bash"] }, "compact": { "trigger_tokens": 800000, "keep_recent_turns": 2, "max_output_tokens": 512 } } }}The compact settings are:
| Field | Default | Purpose |
|---|---|---|
trigger_tokens |
80% of the input budget when context_window_tokens is set; otherwise not set |
The estimated input size that starts automatic compaction. false keeps compaction manual. An explicit value requires context_window_tokens, and must leave room for the output reserve and a safety margin. |
keep_recent_turns |
2 | The number of most recent complete turns kept as they are, when they fit. |
keep_recent_tokens |
The smaller of 20,000 and a quarter of the input budget | The estimated size of the newest steps kept as they are. |
clear_tokens |
60% of the input budget when context_window_tokens is set; otherwise not set |
The estimated size at which old tool results are cleared, before any summary is written. false turns clearing off. |
max_output_tokens |
16,384, or less when the model’s output limit or a quarter of its context window is smaller | The cap on the summary response. |
instructions |
None | Extra text, up to 16,384 characters, added to every summary request for this agent. |
strategy |
"checkpoint" |
"native" lets the provider write the summary on Anthropic Messages and OpenAI Responses. See Provider-native compaction. |
What is kept and what is summarized
Section titled “What is kept and what is summarized”Compaction keeps some parts of the conversation word for word and summarizes only the older steps:
- Every message you typed, and every answer to
ask_user. When one of these is very large, the middle of an older entry is cut, and the cut names where its full text is. - A short working-state list that Raw records itself: the working directory, files written, edited or read, saved full outputs, running background processes, and the last
bashcommand with its exit code. - The newest steps, unchanged, up to
keep_recent_tokens. A step is an assistant message with its tool results, so a tool call is never separated from its result.
The older steps go to the selected model in a summary request that has no tools. When the request fits the agent’s own model’s window, Raw asks that model to write the summary from its current context instead, with its tools unchanged, so the provider can reuse the cached prefix where it supports that. Older images are described by file path, type and size, never by their data.
Before a summary is written, old tool results larger than 2 KB that come before the newest steps are cleared. Each one is replaced by a short stub that names a saved copy of its full output, and the model can read that path with read_file. Raw saves a complete copy first, and it keeps the result when it cannot. ask_user answers, load_skill results and results with images are never cleared.
A summary is accepted only when it is nonempty, complete and shorter than the transcript it replaces. If the summary request fails, is cancelled, or does not shrink the context, the previous context is kept unchanged. A summary cut at the output limit is requested once more with a larger budget when the model’s limit allows it. If it is still cut, the partial summary is kept and marked as cut.
Summaries are lossy. Decisions, changed files and open problems are usually kept, but a summary can omit details. If a detail matters, state it again in your next message.
When the history is large and the model’s window is known, Raw splits the older steps into chronological requests, and summarizes each one separately. Each request carries the summary written so far.
Repeated compaction
Section titled “Repeated compaction”A turn can compact several times when it fills the window again. If a summary leaves the request close to the trigger, the next time the trigger is reached Raw runs a fallback first. The fallback does not call a model. It clears old tool results, and if the request is still too large, it writes a mechanical checkpoint that keeps the user messages, the working state and the steps that fit. A failed automatic compaction shows a warning, and the turn continues with the fallback. A manual /compact still reports its error.
Provider-native compaction
Section titled “Provider-native compaction”Set compact.strategy to "native" to let the provider write the summary. This works on Anthropic Messages and OpenAI Responses. The kept messages, the working state and the fallback stay the same. Other providers, and a native attempt that fails, use the summary request above and show a warning that says why.
After compaction
Section titled “After compaction”Compaction rewrites the start of the model’s context once, so the next request may not reuse the cached prefix from before the compaction. Later turns build on the new prefix.
/clear removes the conversation history while keeping your agent configuration and the cumulative usage counts.
Prompt caching
Section titled “Prompt caching”Some providers can reuse the start of a prompt when it repeats. Raw helps by keeping that start stable: the system prompt, the tool definitions and the committed messages are sent in the same order on every turn. Raw does not add padding or warm-up requests to chase cache hits.
Whether a cache hit happens is decided by the provider. Raw reports what the provider returns and does not claim a hit without evidence.
| Method and service | What Raw sends |
|---|---|
| OpenAI Chat Completions and Responses | A stable prompt cache key for the session, unless you set an explicit cache.key. An explicit key takes precedence. |
| Anthropic Messages | A cache_control marker in auto mode. The retention can be 5m or 1h. |
| Google Generate Content | Stable content, relying on Google’s implicit caching. |
llama.cpp backend (cache.backend set to llama.cpp, Chat Completions method) |
cache_prompt: true. |
| Other services | No guessed cache field. |
The cache key changes when the effective runtime changes, such as when the selected tools change. A change that only edits a selected skill keeps the key and may add a reminder to reload the skill. Changing terminal appearance never changes the cache key.
Usage and unknown values
Section titled “Usage and unknown values”Raw shows token counts only when the provider reports them. A missing value is shown as unknown, never as zero. Cache-read and cache-write counts each have their own coverage, because a provider might report one and not the other.
The cache-read ratio is cache reads divided by input tokens. It is computed only for requests where both values are reported. A ratio does not prove that a particular backend served a cached prefix.
Use /stats in the REPL, or the Details tab in the dashboard’s side panel, to see the counts and how many requests reported each one.
Practical advice
Section titled “Practical advice”- Set
context_window_tokensfor every model you use, so the footer and dashboard can show a percentage, and automatic compaction can run. - Leave
compact.trigger_tokensat its default for most agents. If you set it, keep it below the context window, leaving room for the response. A value close to the window leaves little headroom. - Keep your system prompt and tool selection stable during a long session. Each change to them alters the cached prefix.
- Use
/compactbefore a long task when the footer shows a high percentage, instead of waiting for automatic compaction.