AnthropicPromptCachingMiddleware Class — langchain Architecture
Architecture documentation for the AnthropicPromptCachingMiddleware class in prompt_caching.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 747a705a_7ca0_fa0a_ac57_d0237478c286["AnthropicPromptCachingMiddleware"] 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|extends| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e 977b57b2_5d0e_bcf4_a43e_b52857105005["ChatAnthropic"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|extends| 977b57b2_5d0e_bcf4_a43e_b52857105005 82799faf_65e6_2297_ad64_9a84f61e2277["prompt_caching.py"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|defined in| 82799faf_65e6_2297_ad64_9a84f61e2277 9dc9ccc0_1a4f_2a75_3c2c_9e8e1412fabf["__init__()"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|method| 9dc9ccc0_1a4f_2a75_3c2c_9e8e1412fabf af09cbf7_14e8_e051_a561_13704e6d0806["_should_apply_caching()"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|method| af09cbf7_14e8_e051_a561_13704e6d0806 4fb57c0a_b89d_b7d7_57ad_bac2b722927b["wrap_model_call()"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|method| 4fb57c0a_b89d_b7d7_57ad_bac2b722927b 970a7429_a57e_be43_2bab_421814cfcad8["awrap_model_call()"] 747a705a_7ca0_fa0a_ac57_d0237478c286 -->|method| 970a7429_a57e_be43_2bab_421814cfcad8
Relationship Graph
Source Code
libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py lines 30–147
class AnthropicPromptCachingMiddleware(AgentMiddleware):
"""Prompt Caching Middleware.
Optimizes API usage by caching conversation prefixes for Anthropic models.
Requires both `langchain` and `langchain-anthropic` packages to be installed.
Learn more about Anthropic prompt caching
[here](https://platform.claude.com/docs/en/build-with-claude/prompt-caching).
"""
def __init__(
self,
type: Literal["ephemeral"] = "ephemeral", # noqa: A002
ttl: Literal["5m", "1h"] = "5m",
min_messages_to_cache: int = 0,
unsupported_model_behavior: Literal["ignore", "warn", "raise"] = "warn",
) -> None:
"""Initialize the middleware with cache control settings.
Args:
type: The type of cache to use, only `'ephemeral'` is supported.
ttl: The time to live for the cache, only `'5m'` and `'1h'` are
supported.
min_messages_to_cache: The minimum number of messages until the
cache is used.
unsupported_model_behavior: The behavior to take when an
unsupported model is used.
`'ignore'` will ignore the unsupported model and continue without
caching.
`'warn'` will warn the user and continue without caching.
`'raise'` will raise an error and stop the agent.
"""
self.type = type
self.ttl = ttl
self.min_messages_to_cache = min_messages_to_cache
self.unsupported_model_behavior = unsupported_model_behavior
def _should_apply_caching(self, request: ModelRequest) -> bool:
"""Check if caching should be applied to the request.
Args:
request: The model request to check.
Returns:
`True` if caching should be applied, `False` otherwise.
Raises:
ValueError: If model is unsupported and behavior is set to `'raise'`.
"""
if not isinstance(request.model, ChatAnthropic):
msg = (
"AnthropicPromptCachingMiddleware caching middleware only supports "
f"Anthropic models, not instances of {type(request.model)}"
)
if self.unsupported_model_behavior == "raise":
raise ValueError(msg)
if self.unsupported_model_behavior == "warn":
warn(msg, stacklevel=3)
return False
messages_count = (
len(request.messages) + 1
if request.system_message
else len(request.messages)
)
return messages_count >= self.min_messages_to_cache
def wrap_model_call(
self,
request: ModelRequest,
handler: Callable[[ModelRequest], ModelResponse],
) -> ModelCallResult:
"""Modify the model request to add cache control blocks.
Args:
request: The model request to potentially modify.
handler: The handler to execute the model request.
Extends
Source
Frequently Asked Questions
What is the AnthropicPromptCachingMiddleware class?
AnthropicPromptCachingMiddleware is a class in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py.
Where is AnthropicPromptCachingMiddleware defined?
AnthropicPromptCachingMiddleware is defined in libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py at line 30.
What does AnthropicPromptCachingMiddleware extend?
AnthropicPromptCachingMiddleware extends AgentMiddleware, ChatAnthropic.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free