Context compaction orphans advisor_tool_result blocks, causing 400 invalid_request_error

Resolved 💬 3 comments Opened Apr 25, 2026 by sankaraju Closed Apr 29, 2026

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)

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗