claude-code - 💡(How to fix) Fix Context compaction orphans advisor_tool_result blocks, causing 400 invalid_request_error [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#53365Fetched 2026-04-26 05:17:39
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×4commented ×1

When Claude Code's context compaction runs near context limits, it removes server_tool_use blocks (advisor calls) while leaving their corresponding advisor_tool_result blocks in the conversation history. The API then rejects the orphaned result with a 400 error.

Error Message

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.9.content.0: unexpected `tool_use_id` found in `advisor_tool_result` blocks: srvtoolu_01FztVTApELE7Wm2bvq61F6g. Each `advisor_tool_result` block must have a corresponding `server_tool_use` block before it."},"request_id":"req_011CaRBes2rqG8cwxyP7mAgn"}

Root Cause

Context compaction strips earlier messages to free space but does not treat server_tool_use + advisor_tool_result as an atomic pair. It can remove the server_tool_use block while leaving the advisor_tool_result block, violating the API requirement that every result block have a matching tool_use block preceding it.

This does not affect regular (client-side) tool pairs, only server-side tools like advisor.

Fix Action

Fix / Workaround

Workarounds

Code Example

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.9.content.0: unexpected `tool_use_id` found in `advisor_tool_result` blocks: srvtoolu_01FztVTApELE7Wm2bvq61F6g. Each `advisor_tool_result` block must have a corresponding `server_tool_use` block before it."},"request_id":"req_011CaRBes2rqG8cwxyP7mAgn"}
RAW_BUFFERClick to expand / collapse

Summary

When Claude Code's context compaction runs near context limits, it removes server_tool_use blocks (advisor calls) while leaving their corresponding advisor_tool_result blocks in the conversation history. The API then rejects the orphaned result with a 400 error.

Error

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.9.content.0: unexpected `tool_use_id` found in `advisor_tool_result` blocks: srvtoolu_01FztVTApELE7Wm2bvq61F6g. Each `advisor_tool_result` block must have a corresponding `server_tool_use` block before it."},"request_id":"req_011CaRBes2rqG8cwxyP7mAgn"}

Steps to Reproduce

  1. Use the advisor tool (server-side tool) one or more times in a long conversation
  2. Continue the conversation until context compaction triggers
  3. Send the next message — the 400 error is returned and the session is broken

Root Cause

Context compaction strips earlier messages to free space but does not treat server_tool_use + advisor_tool_result as an atomic pair. It can remove the server_tool_use block while leaving the advisor_tool_result block, violating the API requirement that every result block have a matching tool_use block preceding it.

This does not affect regular (client-side) tool pairs, only server-side tools like advisor.

Expected Behavior

Compaction should treat server_tool_use / advisor_tool_result pairs atomically — either keep both or drop both. Alternatively, convert the pair to a synthetic summary message during compaction so the API contract is preserved.

Workarounds

  • /clear before the session grows large enough to trigger compaction
  • Use advisor sparingly in long sessions
  • /clear is the only recovery path once the error appears mid-session

Environment

  • Claude Code CLI
  • Model: claude-sonnet-4-6
  • Platform: Linux (WSL2)

extent analysis

TL;DR

To fix the issue, modify the context compaction logic to treat server_tool_use and advisor_tool_result blocks as an atomic pair, ensuring both are kept or dropped together.

Guidance

  • Review the context compaction algorithm to identify where server_tool_use blocks are being removed without their corresponding advisor_tool_result blocks.
  • Consider implementing a check to ensure that every advisor_tool_result block has a preceding server_tool_use block before sending the conversation history to the API.
  • As a temporary workaround, use the /clear command before the session grows large enough to trigger compaction or use the advisor tool sparingly in long sessions.
  • Investigate the possibility of converting server_tool_use and advisor_tool_result pairs to synthetic summary messages during compaction to preserve the API contract.

Example

No code snippet is provided as the issue does not include specific code details.

Notes

The provided workarounds (/clear command and using advisor sparingly) may not be suitable for all use cases, and a more robust solution would involve modifying the context compaction logic.

Recommendation

Apply a workaround, specifically using the /clear command before the session grows large enough to trigger compaction, as this is the most straightforward solution given the current implementation.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING