Skip to content

Conversation

@monish-instinct
Copy link

Fixes #34542

When using AnthropicPromptCachingMiddleware with Claude's code_execution feature, the middleware was incorrectly applying cache_control to tool_use and tool_result blocks that were called by code_execution. The Anthropic API rejects these requests because these blocks cannot have cache_control as they are not rendered in Claude's context.

This fix:

  • Collects tool_use_ids of blocks called by code_execution (identified by the 'caller' field)
  • Skips applying cache_control to tool_use blocks with a code_execution caller
  • Skips applying cache_control to tool_result blocks whose tool_use_id corresponds to a code_execution tool
  • Applies cache_control only to the next eligible block in the conversation

Fixes langchain-ai#34542

When using AnthropicPromptCachingMiddleware with Claude's code_execution feature, the middleware was incorrectly applying cache_control to tool_use and tool_result blocks that were called by code_execution. The Anthropic API rejects these requests because these blocks cannot have cache_control as they are not rendered in Claude's context.

This fix:
- Collects tool_use_ids of blocks called by code_execution (identified by the 'caller' field)
- Skips applying cache_control to tool_use blocks with a code_execution caller
- Skips applying cache_control to tool_result blocks whose tool_use_id corresponds to a code_execution tool
- Applies cache_control only to the next eligible block in the conversation
@github-actions github-actions bot added integration PR made that is related to a provider partner package integration anthropic `langchain-anthropic` package issues & PRs fix For PRs that implement a fix labels Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anthropic `langchain-anthropic` package issues & PRs fix For PRs that implement a fix integration PR made that is related to a provider partner package integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicPromptCachingMiddleware applies cache_control to tool_use/tool_result blocks called by code_execution, causing API errors

1 participant