_should_apply_caching() — langchain Function Reference
Architecture documentation for the _should_apply_caching() function in prompt_caching.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD af09cbf7_14e8_e051_a561_13704e6d0806["_should_apply_caching()"] 747a705a_7ca0_fa0a_ac57_d0237478c286["AnthropicPromptCachingMiddleware"] af09cbf7_14e8_e051_a561_13704e6d0806 -->|defined in| 747a705a_7ca0_fa0a_ac57_d0237478c286 4fb57c0a_b89d_b7d7_57ad_bac2b722927b["wrap_model_call()"] 4fb57c0a_b89d_b7d7_57ad_bac2b722927b -->|calls| af09cbf7_14e8_e051_a561_13704e6d0806 970a7429_a57e_be43_2bab_421814cfcad8["awrap_model_call()"] 970a7429_a57e_be43_2bab_421814cfcad8 -->|calls| af09cbf7_14e8_e051_a561_13704e6d0806 style af09cbf7_14e8_e051_a561_13704e6d0806 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py lines 71–99
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
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _should_apply_caching() do?
_should_apply_caching() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py.
Where is _should_apply_caching() defined?
_should_apply_caching() is defined in libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py at line 71.
What calls _should_apply_caching()?
_should_apply_caching() is called by 2 function(s): awrap_model_call, wrap_model_call.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free