- Compaction replaces older messages with a summary.
- Tool result previews send the model only the beginning and the end of a large tool result.
contextWindowsets when compaction starts, and makes the run shrink a request that doesn’t fit.
Compaction
Compaction replaces older messages with a summary in the requests sent to the model. It also applies to a single long run without a conversation, such as the coding task example. Compaction is on by default. Before each model call, once the request exceedscompaction.maxTokens, the run replaces its older messages with a summary and keeps the newest messages that fit in retainTokens. Each summary costs at least one model call.
The default maxTokens depends on contextWindow:
Set
compaction.maxTokens to choose the threshold yourself, or compaction: false to turn compaction off:
retainTokensdefaults to a quarter ofmaxTokens. The run keeps at most half of what the instructions and tool definitions leave ofmaxTokens, so that the next step doesn’t compact again.retainTokensmust be less thanmaxTokens, including a defaultmaxTokensthatcontextWindowsets. Otherwise,agent()throws aTypeError.- With
compaction: false, the run doesn’t summarize. It truncates the older messages instead, when a request doesn’t fit the context window or the provider reports an overflow. - If a summary fails, the run logs a warning and truncates the older messages instead, for the rest of the run. The next run summarizes again.
- The summary replaces messages only in the requests:
result.messageskeeps every message that the run loaded and added.
Customize the summary
By default, the summary records each user request and how far it got, the constraints that still apply, the results and exact identifiers that matter, decisions, errors, approvals, and the work that remains, in under about 2,000 words. Setcompaction.instructions to replace this guidance, for example to keep what your domain needs or to shorten the summary:
compaction.model, for example to a smaller model:
- When the agent’s model summarizes because the request exceeded
maxTokens, the run sends the instructions and tools of its last request with its current messages, and asks for the summary at the end, so the provider can serve the earlier messages from its prompt cache. A compaction before the run’s first model call has no earlier request, so it uses a separate request. - Otherwise, the model receives the older messages and the earlier summary in a separate request. Restate Harness compares models by object, so a second
aiModelfor the same provider model also gets a separate request. To keep the cached request, leave outcompaction.modelor pass the agent’s own model object.
Compaction in conversations
With aConversation, the run also stores its summary in the conversation:
- The run stores the summary under
conversation/summaryat its next save. See Storage layout. The next run starts from the summary and the messages after it, so it doesn’t summarize the same messages again. - A run loads only the messages after the stored summary, so
result.messagesstarts there. - A truncation is never stored, so the next run loads the messages that the truncation dropped.
- If storing the summary fails, for example because the summary is longer than 64,000 characters, the run logs a warning and continues. The next run loads every message after the last stored summary.
- A later run can’t read a previewed tool result from a message that the stored summary replaced. The model receives the error code
unknown_ref.
Tool result previews
Tool result previews are on by default. For a tool result larger thanmaxTokens, the model receives the beginning and the end, previewTokens in total, with a reference to the full result. The model reads the rest with the built-in read_tool_result tool:
previewTokens defaults to half of maxTokens. Set toolResults: false to send every result in full. result.messages and the saved conversation always keep every result in full. The name read_tool_result is reserved while previews are on.
Context window
Restate Harness can’t read the model’s context window. SetcontextWindow on the agent to the model’s context window in tokens, for example contextWindow: 128000. Set it for a model with a context window under 200,000 tokens, because the default compaction threshold of 170,000 tokens doesn’t fit such a window. For a model with a much larger window, such as 1,000,000 tokens, set it to start compaction later.
contextWindow sets the default compaction.maxTokens. Before each model call, the run also shrinks a request that wouldn’t fit together with the reply: it summarizes the older messages, or truncates them when compaction is false.
For the reply, the run reserves maxOutputTokens, or 32,768 tokens when maxOutputTokens is unset. contextWindow must be greater than this reserve, or agent() throws a TypeError, so set maxOutputTokens for a model with a smaller context window.
If the provider reports a context overflow, the run shrinks the request once and sends it again. If the run can’t shrink the request, or the new request overflows too, the run fails.
aiModel recognizes an overflow only from HTTP 413 or the provider error code context_length_exceeded. Any other overflow error fails the run, so set contextWindow for providers that don’t return these signals. A custom model reports an overflow by throwing ContextOverflowError.