AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Langchain-Anthropic: Cache_control On A Tool_use Content Block Is Silently Dropped When A Matching Tool_calls Item Exists

In langchain-anthropic's _format_messages, when an AIMessage contains both a tool_use content block and a matching tool_call in the structured tool_calls field, the tool_use block is rebuilt from tool_calls and any extra keys on the original block, notably cache_control, are discarded. This causes prompt-cache breakpoints placed on tool_use blocks to be silently lost.

mediumConfidence 95%Langchain-AnthropicAgentslangchain-coreChainsAffected V1.4.7

Origin Analysis

The tool_use formatting branch prefers the structured tool_calls representation and constructs a new content block with only id, name, and input, omitting cache_control and any other metadata present in the original tool_use block.
Create an AIMessage with a content list containing a tool_use block that includes cache_control and a matching ToolCall in tool_calls. Pass the message to _format_messages. Observe that the output tool_use block no longer contains the cache_control key.

Fixing Code Block

if message.tool_calls and any(tc['id'] == block['id'] for tc in message.tool_calls): tool_call = next(tc for tc in message.tool_calls if tc['id'] == block['id']) formatted_block = { 'type': 'tool_use', 'id': tool_call['id'], 'name': tool_call['name'], 'input': tool_call['args'], } if 'cache_control' in block: formatted_block['cache_control'] = block['cache_control'] content.append(formatted_block) else: content.append(block)
The fix preserves the original block's cache_control key when rebuilding the tool_use block from structured tool_calls. This aligns the tool_use branch with the existing behavior for text, thinking, and server_tool_use/mcp_tool_use blocks, which already maintain cache_control.

Edge Case Audit

This change preserves cache_control without validating its structure; invalid values may still cause API errors at request time. To roll back, revert this branch change. No database or migration steps are required. Concurrency and multi-threading are unaffected because formatting operates on a per-message basis. Ensure tests cover cases with and without cache_control to avoid KeyError.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state