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

ChatAnthropic Does Not Propagate Metadata.User_id To Third-Party Agent, Breaking Prompt Caching

ChatAnthropic ignores the metadata provided in the invoke config, so the user_id is not sent to the Anthropic API. This prevents prompt caching from working when connecting to third-party agents that require user_id for cache partitioning. The issue can be reproduced by invoking the model with a config containing metadata and observing that the request payload lacks the metadata field.

mediumConfidence 90%LangchainEmbeddingsVector StoresAffected V<=1.3.1

Origin Analysis

The _create method builds the API request payload without including metadata from the run config. The metadata is available via the callback run manager (run_manager.metadata) but is never passed to the client's messages.create call.
Run the provided code with a third-party Anthropic-compatible API that requires metadata.user_id for prompt caching. Observe that the second invocation does not use cached prompts because the API request lacks the metadata field. Debugging shows that payload does not contain 'metadata' when calling self._client.messages.create.

Fixing Code Block

# In langchain_anthropic/chat_models.py # Modify _generate method: # After constructing payload and before calling _create, add: if run_manager and run_manager.metadata: user_id = run_manager.metadata.get("user_id") if user_id: payload["metadata"] = {"user_id": user_id} # Similarly, in _stream method, add the same block before calling _create or handling streaming.
The fix extracts the user_id from the run_manager's metadata (which is populated from the config passed to invoke) and adds it to the request payload under the 'metadata' key as expected by the Anthropic API. This ensures that the user_id is forwarded to third-party agents, enabling correct cache partitioning.

Edge Case Audit

This fix only forwards the user_id field; other metadata keys are ignored. If the API expects a different metadata structure, this may not suffice. Ensure that the third-party agent accepts the 'user_id' key; otherwise, the request may fail. If the user has set metadata via other means (e.g., directly on the client), this change may override or conflict. Rollback: revert this change and manually add metadata to the payload as done in the issue if unexpected errors occur.

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