Skip to content

perf(agent): stop doubling failed tool output in context - #418

Merged
mrnasil merged 1 commit into
mainfrom
perf/tool-error-tokens
Nov 7, 2026
Merged

mrnasil merged 1 commit into
mainfrom
perf/tool-error-tokens

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 7, 2026

Copy link
Copy Markdown
Member

First of four token-efficiency PRs. The findings come from requests captured with a scripted OpenAI-compatible mock server, counted with the o200k tokenizer.

Problem

When a tool call failed, its output went into the context twice:

  1. The tool result, capped at 32 KiB.
  2. A recovery note: The tool failed with error: {full output}. Please correct your approach. This copied the raw output with no cap, into a Role::User message. Truncate, spill, prune and shake only touch Role::Tool messages, so the note stayed in context and was re-sent on every step until compaction.

The cap also kept only the head of the output. The shell layer already keeps the tail of long output, so for a long test run the model saw only the middle. Neither the start nor the failures: line was in the tool result; the verdict only reached the model through the oversized note.

Change

  • The recovery note now names the failed tool in one line: `bash` failed; its output is in the tool result above. Correct your approach.
  • A fresh tool result over the cap keeps its first 8 KiB and its tail, with [N chars omitted] between them (cap_tool_text_head_tail). The result stays at or under TOOL_RESULT_MAX_CHARS, so the per-step head-only passes leave it alone until it is old.

Measured

One cargo test-shaped run: 4,000 lines, failures: as the last line, exit 101.

Before After
Tool result 11,920 tok, no failures: line 11,910 tok, includes failures: and the exit code
Recovery note 23,870 tok 18 tok
Total 35,790 11,928 (−67%)

The saving repeats on every later step of the session until compaction.

Checks

  • whycodes-agent --lib 764 passed; whycodes-session --lib 171 passed
  • fmt, clippy (session, agent), ratchets ok
  • Local llvm-cov on session and agent: every changed line is covered
  • docs/internals/turn-loop.md and compaction.md are updated

🤖 Generated with Claude Code

A failed tool call put its output in context twice. The tool result was
capped at 32 KiB, but the recovery note copied the raw output, uncapped,
into a user-role message that truncate/spill/prune never touch, so it rode
every later step until compaction. A red test run measured 11.9k tokens of
result plus a 23.9k-token note.

The cap also kept only the head. The shell already keeps the tail of
long output, so the model saw the middle of a test run: neither the start
nor the `failures:` verdict, which only reached it through the note.

- The recovery note now names the failed tool in one line and points at
  its result.
- A fresh tool result over the cap keeps its first 8 KiB and its tail,
  with `[N chars omitted]` between them. Older results still shrink from
  the head as before.

Measured on a captured request for one failing `cargo test`-shaped run:
35,790 -> 11,928 tokens, and the failure line is now in the result.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant