langchain - ✅(Solved) Fix Prompt caching does not work when connecting to third-party agents [2 pull requests, 4 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35219Fetched 2026-04-08 00:27:09
View on GitHub
Comments
4
Participants
2
Timeline
11
Reactions
0
Author
Participants
Assignees
Timeline (top)
labeled ×3commented ×2cross-referenced ×2referenced ×2

I want to use LangChain to integrate the Claude prompt caching feature of a third-party agent. For this, I need to provide a metadata.user_id to the third party agent. I attempted to configure it using the config, but it didn't work [the same code works with the official Claude API]. Later, I reviewed and debugged the source code and found that the metadata.user_id wasn't being loaded in the self._client.messages.create(**payload) call in /langchain_anthropic/chat_models.py So I modified that part of the code as follows:

    def _create(self, payload: dict) -> Any:
        if "betas" in payload:
            return self._client.beta.messages.create(**payload)
        payload["metadata"]={
            "user_id": "my-user-123",
        }
        return self._client.messages.create(**payload)

This way it can work. I want to know whether this is because of my coding issues or if the metadata.user_id wasn't loaded in here.

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

I want to use LangChain to integrate the Claude prompt caching feature of a third-party agent. For this, I need to provide a metadata.user_id to the third party agent. I attempted to configure it using the config, but it didn't work [the same code works with the official Claude API]. Later, I reviewed and debugged the source code and found that the metadata.user_id wasn't being loaded in the self._client.messages.create(**payload) call in /langchain_anthropic/chat_models.py So I modified that part of the code as follows:

    def _create(self, payload: dict) -> Any:
        if "betas" in payload:
            return self._client.beta.messages.create(**payload)
        payload["metadata"]={
            "user_id": "my-user-123",
        }
        return self._client.messages.create(**payload)

This way it can work. I want to know whether this is because of my coding issues or if the metadata.user_id wasn't loaded in here.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

anthropic: 0.76.0 filetype: 1.2.0 google-genai: 1.62.0 httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.0.6 openai: 2.15.0 orjson: 3.11.5 packaging: 25.0 pydantic: 2.12.5 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 tenacity: 9.1.2 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.13.0 zstandard: 0.25.0

PR fix notes

PR #35223: fix(anthropic): pass metadata.user_id from config to API

Description (problem / solution / changelog)

Summary

Fixes an issue where metadata.user_id from the config parameter was not being passed to the Anthropic API, preventing prompt caching from working correctly with third-party agents that require user identification.

Changes

  • Modified _get_request_payload method in ChatAnthropic to extract metadata from kwargs
  • Added logic to include metadata with user_id in the API payload when present
  • Maintains backward compatibility - metadata is only added when explicitly provided

How I verified this works

Created and ran unit tests verifying:

  • metadata is correctly passed to API payload when provided
  • metadata is absent from payload when not provided
  • metadata without user_id doesn't trigger inclusion

Fixes #35219

Changed files

  • libs/partners/anthropic/langchain_anthropic/chat_models.py (modified, +9/-0)

PR #35243: fix(anthropic): pass metadata.user_id from config to API requests

Description (problem / solution / changelog)

Description Fixes metadata.user_id being dropped when passed via config to ChatAnthropic and AnthropicLLM, preventing prompt caching and user tracking with third-party Claude providers.

Fixes #35226

Changes Added extract_metadata_from_run_manager() helper in _client_utils.py to extract user_id from run manager metadata Updated ChatAnthropic._generate() and _agenerate() to pass metadata to API requests Updated AnthropicLLM._call() and _acall() with same metadata extraction logic Added 10 unit tests covering both sync and async operations Breaking Changes None. This is a bug fix that enables existing functionality.

Verification All 10 new unit tests pass All 15 existing Anthropic tests pass Ran make format, make lint, and make test successfully Manually verified metadata is correctly passed to Anthropic API Technical Details The issue occurred because _generate(), _agenerate(), _call(), and _acall() methods received run_manager with metadata but never extracted it. The Anthropic API expects a metadata field with user_id for features like prompt caching. This fix extracts user_id from run_manager.metadata and includes it in the API request payload.

AI Contribution Disclaimer: This PR was developed with assistance from an AI coding assistant. All code has been reviewed, tested, and verified to meet LangChain standards.

Changed files

  • libs/partners/anthropic/langchain_anthropic/_client_utils.py (modified, +29/-0)
  • libs/partners/anthropic/langchain_anthropic/chat_models.py (modified, +9/-0)
  • libs/partners/anthropic/langchain_anthropic/llms.py (modified, +60/-4)
  • libs/partners/anthropic/tests/unit_tests/test_llms_metadata.py (added, +42/-0)
  • libs/partners/anthropic/tests/unit_tests/test_metadata_user_id.py (added, +174/-0)
  • libs/partners/anthropic/uv.lock (modified, +1/-1)

Code Example

from langchain_anthropic import ChatAnthropic
from langchain_core.messages import SystemMessage, HumanMessage 

model = ChatAnthropic(
    model_name="claude-opus-4-6",
    temperature=1,
    api_key="sk-ant-xxx",
    base_url="https://code.newcli.com/claude/droid",
    default_headers={
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
        "anthropic-beta": "prompt-caching-2024-07-31"
    },
)
system_message = SystemMessage(
    content=[
        {
            "type": "text",
            "text": "You are a technology expert."
        },
        {
            "type": "text",
            "text": "foo" * 1000,
            "cache_control": {"type": "ephemeral"}
        }
    ],

)

human_message = HumanMessage(content="What's LangChain, according to its README?")

messages = [system_message, human_message]
metadata = {
    "user_id": "my-user-123"
}
config = {
    "metadata": {
        "user_id": "my-user-123",
    }
}
print("--- 1st Invocation ---")
response_1 = model.invoke(messages, config=config)
print("--- 2nd Invocation ---")
usage_1 = response_1.response_metadata
print(f"Usage 1: {usage_1}")

---



---

def _create(self, payload: dict) -> Any:
        if "betas" in payload:
            return self._client.beta.messages.create(**payload)
        payload["metadata"]={
            "user_id": "my-user-123",
        }
        return self._client.messages.create(**payload)
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

from langchain_anthropic import ChatAnthropic
from langchain_core.messages import SystemMessage, HumanMessage 

model = ChatAnthropic(
    model_name="claude-opus-4-6",
    temperature=1,
    api_key="sk-ant-xxx",
    base_url="https://code.newcli.com/claude/droid",
    default_headers={
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
        "anthropic-beta": "prompt-caching-2024-07-31"
    },
)
system_message = SystemMessage(
    content=[
        {
            "type": "text",
            "text": "You are a technology expert."
        },
        {
            "type": "text",
            "text": "foo" * 1000,
            "cache_control": {"type": "ephemeral"}
        }
    ],

)

human_message = HumanMessage(content="What's LangChain, according to its README?")

messages = [system_message, human_message]
metadata = {
    "user_id": "my-user-123"
}
config = {
    "metadata": {
        "user_id": "my-user-123",
    }
}
print("--- 1st Invocation ---")
response_1 = model.invoke(messages, config=config)
print("--- 2nd Invocation ---")
usage_1 = response_1.response_metadata
print(f"Usage 1: {usage_1}")

Error Message and Stack Trace (if applicable)

Description

I want to use LangChain to integrate the Claude prompt caching feature of a third-party agent. For this, I need to provide a metadata.user_id to the third party agent. I attempted to configure it using the config, but it didn't work [the same code works with the official Claude API]. Later, I reviewed and debugged the source code and found that the metadata.user_id wasn't being loaded in the self._client.messages.create(**payload) call in /langchain_anthropic/chat_models.py So I modified that part of the code as follows:

    def _create(self, payload: dict) -> Any:
        if "betas" in payload:
            return self._client.beta.messages.create(**payload)
        payload["metadata"]={
            "user_id": "my-user-123",
        }
        return self._client.messages.create(**payload)

This way it can work. I want to know whether this is because of my coding issues or if the metadata.user_id wasn't loaded in here.

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 23.6.0: Wed May 14 13:52:52 PDT 2025; root:xnu-10063.141.1.705.2~2/RELEASE_ARM64_T8122 Python Version: 3.11.2 (v3.11.2:878ead1ac1, Feb 7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)]

Package Information

langchain_core: 1.2.7 langchain: 1.2.6 langsmith: 0.6.4 langchain_anthropic: 1.3.1 langchain_google_genai: 4.2.0 langchain_openai: 1.1.7 langgraph_sdk: 0.3.3

Optional packages not installed

langserve

Other Dependencies

anthropic: 0.76.0 filetype: 1.2.0 google-genai: 1.62.0 httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.0.6 openai: 2.15.0 orjson: 3.11.5 packaging: 25.0 pydantic: 2.12.5 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 tenacity: 9.1.2 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.13.0 zstandard: 0.25.0

extent analysis

Problem Summary

The user is trying to use LangChain to integrate the Claude prompt caching feature of a third-party agent. They want to provide a metadata.user_id to the third-party agent but are unable to do so using the config.

Root Cause Analysis

The root cause of the issue is that the metadata.user_id is not being loaded in the self._client.messages.create(**payload) call in langchain_anthropic/chat_models.py. The user has modified the code to manually add the metadata.user_id in the _create method.

Fix Plan

To fix this issue, we need to modify the langchain_anthropic/chat_models.py file to correctly load the metadata.user_id from the config. We can do this by adding the following code to the _create method:

def _create(self, payload: dict) -> Any:
    if "betas" in payload:
        return self._client.beta.messages.create(**payload)
    payload["metadata"] = self.config["metadata"]
    return self._client.messages.create(**payload)

This will load the metadata.user_id from the config and add it to the payload.

Verification

To verify that the fix worked, we can run the same code as before and check if the metadata.user_id is being loaded correctly.

Extra Tips

  • Make sure to update the langchain_anthropic package to the latest version to ensure that the fix is applied.
  • If you are still experiencing issues, try printing out the payload and config to see if the metadata.user_id is being loaded correctly.
  • Consider opening a pull request to the LangChain repository to fix this issue and make it available to all users.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING