Skip to main content
ChatCLI implements a comprehensive tool result management system that ensures integrity, controls context size, and progressively compacts old results. This is essential for long agent sessions where dozens of tool calls can saturate the context window.

Tool Result Pairing

Every tool_use (a tool call made by the model) must have a corresponding tool_result in the conversation history. When this pairing breaks — due to interruption, timeout, or silent error — the API rejects the history. The EnsureToolResultPairing system automatically validates and repairs:

Synthetic Results

When a tool_use has no corresponding result, ChatCLI injects:
The message instructs the model to not repeat the failed tool call, preventing infinite retry loops.

3-Phase Validation

1

ID Collection

Traverses the entire history collecting tool_use IDs (from assistant messages) and tool_result IDs (from tool messages).
2

Misalignment Detection

Compares the two sets of IDs. Tool uses without a result are “missing”. Tool results without a use are “orphans”. Duplicate IDs are flagged for deduplication.
3

History Reconstruction

Rebuilds the history: removes orphans, deduplicates tool_use IDs, and injects synthetic results after assistant messages with unmatched tool_uses.

Result Budget Enforcement

Tool results such as large file reads or command output can quickly consume the context window. The budget system limits the aggregate size at three levels:

Per-tool truncation (capability)

Plugins that implement plugins.TruncationAware declare their own cap — useful when the tool has non-default context needs: Truncation preserves the historical head/tail shape (5000-char preview + 1000-char suffix + [TRUNCATED N chars omitted, M kept] marker).

How Enforcement Works

The budget is applied in two passes:
Each individual result is checked against DefaultPerResultMaxChars (20KB). If it exceeds the limit, the full content is saved to disk and replaced with a preview:

Disk Persistence

Truncated results are saved as temporary files inside the Session Workspace, instead of the legacy global /tmp/chatcli-tool-results/:
Moving to a per-session directory has two important effects:
  1. Isolation between sessions. Multiple chatcli instances running in parallel on the same host no longer share the overflow pool.
  2. On-demand reads by the agent. The scratch dir is on the agent’s read allowlist, so when the model encounters the [full output saved to ...] marker in the preview, it can open the file with read_file:
Before this release, the path was a dead end — the read tool blocked it because it was outside the workspace boundary. The budget “saved the output” but the agent had no way to access it.
Files are automatically cleaned up when the session ends (ChatCLI.cleanup), respecting CHATCLI_AGENT_KEEP_TMPDIR=true for debugging. A periodic global cleanup is no longer required.

Preview: Head + Tail

The preview retains the beginning and end of the result to maximize usefulness:

Progressive Microcompaction

Microcompaction progressively reduces the size of old tool results as the conversation advances, without losing critical information: With the compression layer active, both levels are lossless: the original result is archived in the CCR store before the cut and the preview/summary carries a <<ccr:KEY>> marker (preserved across levels) that the model expands with @recall.

Content Type Detection

The summary automatically identifies the content type for context:

Microcompaction Configuration

Only results larger than 3,000 chars are compacted. Small results are always preserved. Results from write and execution tools are preserved as they contain critical error information.

Complete Flow

Tool result management is applied in this order during the agent loop:

Complete Configuration


Next Steps

Session Workspace

Where overflow files live and how the agent reads them.

Subagent Delegation

Complementary strategy to avoid saturating context with raw data.

Context Recovery

What happens when even with budgeting the context overflows.

Cost Tracking

Monitor token consumption including tool results.