fix(grok): accept and strip Anthropic cache_control on tools and tool blocks - #37
Merged
Merged
Conversation
…d tool_result children Claude Code attaches ephemeral prompt-cache markers (cache_control) to tool definitions, tool_use blocks, and tool_result text children mid-tool-loop. The current translation hard-rejected these (e.g. "unsupported tool field: cache_control"), causing 400s when switching to Grok mid-session. Accept and validate the marker (matching existing system/text/tool_result handling) and drop it before forwarding to the upstream Responses API.
wtfsayo
force-pushed
the
grok-strip-anthropic-fields
branch
from
July 12, 2026 09:58
9e78701 to
f106b3d
Compare
Owner
|
Thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Claude Code inserts prompt-cache breakpoints (
cache_control: {type: "ephemeral"}) once a session has enough context, and it attaches those markers to tool definitions and totool_use/tool_resultblocks. On a fresh single-turn chat you never see them, so the happy path looks fine.The failure shows up multi-turn: after the agent has run a tool or two, every following turn replays the accumulated tool history with
cache_controlmarkers attached. The current Grok translation already acceptscache_controlonsystem,text, and top-leveltool_result, but hard-rejects it in three spots that only appear once a tool loop is underway:unsupported tool field: cache_controltool_useblocks →object.len() != 4tool_resulttext children →part.len() != 2So a session that works on turn one starts returning 400 the moment tools + caching breakpoints are in play (and reliably when switching to Grok mid-session). The marker is a client-side caching hint that upstream never needs to see.
Fix
parse_tools: allowcache_controlon tool definitions and validate it via the existingvalid_cache_controlhelper.tool_useblocks: allow an optionalcache_controlkey instead of requiring exactly 4 keys.tool_resulttext children: allow an optionalcache_controlkey instead of requiring exactly{type, text}.In all three cases the marker is validated and then dropped; nothing new is forwarded to the Responses API. This mirrors how
system/text/ top-leveltool_resultalready handle it.Testing
tool_use/tool_resultchildren, reject malformedcache_control.cargo test --all(503 passed),cargo fmt --check, and clippy clean.