Skip to content

fix(server): convert Anthropic tool_use and tool_result blocks - #791

Merged
davide221 merged 1 commit into
mainfrom
fix/anthropic-tool-blocks
Sep 30, 2026
Merged

davide221 merged 1 commit into
mainfrom
fix/anthropic-tool-blocks

Conversation

@davide221

@davide221 davide221 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Problem

/v1/messages kept only the text blocks of each message. An assistant turn made of tool_use blocks rendered as an empty turn, and so did a user turn made of tool_result blocks, so the model never saw its own tool call or the result.

Measured with Claude Code against Qwen3.8-27B + DFlash2 on lucebox7 (R9700): on main it re-issued the same first Read call 50 times in two minutes, each prompt 33 tokens longer (an empty assistant turn, an empty user turn and a new system note per round).

Claude Code also ends every request with a system message (a token budget note), so ends_with_tool_result was always false for it and the agent-turn prefix snapshot from #775 never took the deep cut.

Change

  • Assistant tool_use blocks: their ids go through tool memory, like OpenAI tool_calls, so the turn replays the text the model generated. Ids the server no longer remembers (restart, eviction) fall back to the Qwen template's own <tool_call> rendering instead of disappearing.
  • User tool_result blocks: each becomes a tool message (string content or the text blocks of an array). The user's own text in that turn follows them.
  • http_detail::ends_with_tool_result(): the last message other than a system note decides.

Validation (lucebox7, Qwen3.8-27B UD-IQ4_XS + DFlash2, qualified launcher flags)

  • Claude Code completes a multi-step coding task (read, edit, add a unit test): 6 of 6 runs finished in 86-128 s.
  • Unit tests: two new normalization cases (tool memory hit and miss, tool_result order and content forms, trailing system note) plus the existing suite.

Not in this PR

Registering Anthropic replies in remember_agent_turn (which would let the prefix cache reuse the generated turn for Claude Code) is left out on purpose: it renders past tool-call turns with the empty <think></think> the model generated after, and with the current greedy default for Qwen3.8 that history made Claude Code loop on a failing edit in 5 of 6 runs. It belongs with sampling defaults for Qwen3.8.

Review in cubic

/v1/messages kept only text blocks. An assistant turn of tool_use blocks
rendered empty, and so did a user turn of tool_result blocks, so the model
never saw its own call or its result and Claude Code re-issued the same
first tool call forever.

- tool_use ids go through tool memory like OpenAI tool_calls; ids the server
  no longer remembers fall back to the Qwen template's <tool_call> rendering.
- Each tool_result becomes a tool message (string or text-block content);
  the user's own text in that turn follows them.
- Claude Code ends every request with a system note, so a request "ends with
  a tool result" when the last message other than a system note is one. The
  agent-turn prefix snapshot (#775) depends on it.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

@davide221
davide221 merged commit 508b648 into main Sep 30, 2026
9 checks passed
@davide221
davide221 deleted the fix/anthropic-tool-blocks branch October 12, 2026 07:37
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