Context compaction orphans advisor_tool_result blocks, causing 400 invalid_request_error
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
- Use the
advisortool (server-side tool) one or more times in a long conversation - Continue the conversation until context compaction triggers
- 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
/clearbefore the session grows large enough to trigger compaction- Use
advisorsparingly in long sessions /clearis the only recovery path once the error appears mid-session
Environment
- Claude Code CLI
- Model: claude-sonnet-4-6
- Platform: Linux (WSL2)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗