Skip to main content

Keep long conversations manageable

Long-running agents can outgrow the amount of conversation a model can read at once. Compaction saves older turns separately so the agent can continue with recent messages and retrieve earlier details when needed. It is different from shortening a large tool result or saving shared application data in ContextStore.

from ish import Agent

agent = Agent(
model="gpt-4o-mini",
instructions="Help with the task and preserve important facts from the conversation.",
context_compaction="auto",
compaction_turn_threshold=12,
context_window_tokens=32_768,
)

Compaction is off by default. Enable it when long conversations approach the model's context limit. The runtime keeps the opening instructions and recent turns, archives older complete exchanges, and can retrieve archived details when the agent needs them. Shared values in ContextStore are separate and are not removed by conversation compaction.

When compaction runs​

Choose a turn threshold, a context-window budget, or both. The runtime compacts before sending a prompt that is likely to exceed the configured budget. Compaction preserves complete tool-call exchanges, so a prompt never contains a tool result without its matching request.

What stays available​

The opening instructions and recent turns remain in the prompt. Older turns stay in the archive and can be read in pages. If you also enable tool-result offloading, that feature shortens individual tool outputs; it does not replace conversation archival.

Configure automatic compaction across SDKs​

Manual compaction (ContextCompactor / compact_messages)​

Every SDK also exposes the standalone Rust message compactor (ContextCompactor in Python, TypeScript, Go, C#, and Java; ish_harness::context::compact_messages and compact_messages_recoverable in Rust). Defaults are max_messages = 40, keep_first = 2, and keep_last = 20.

from ish import ChatMessage, ContextCompactor

messages = [
ChatMessage(role="user", content="first"),
ChatMessage(role="assistant", content="middle"),
ChatMessage(role="user", content="last"),
]
compacted = ContextCompactor.compact(messages, max_messages=2, keep_first=1, keep_last=1)

When to use compaction​

Enable compaction (and optionally context_window_tokens) for long conversations. ISH saves older turns outside the active prompt and can retrieve them later if needed. Keep critical rules in system instructions or ContextStore rather than relying only on old chat messages. Use tool output management to keep large tool results from filling the conversation.