prompt_caching.py — langchain Source File
Architecture documentation for prompt_caching.py, a python file in the langchain codebase. 5 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5174eefd_f47d_35f2_8053_b1fc12ac00b4["prompt_caching.py"] cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] 5174eefd_f47d_35f2_8053_b1fc12ac00b4 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 5174eefd_f47d_35f2_8053_b1fc12ac00b4 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 0c635125_6987_b8b3_7ff7_d60249aecde7["warnings"] 5174eefd_f47d_35f2_8053_b1fc12ac00b4 --> 0c635125_6987_b8b3_7ff7_d60249aecde7 c359d43e_cf8d_7bb0_e2b1_fdbe4302ab26["langchain_anthropic.chat_models"] 5174eefd_f47d_35f2_8053_b1fc12ac00b4 --> c359d43e_cf8d_7bb0_e2b1_fdbe4302ab26 50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"] 5174eefd_f47d_35f2_8053_b1fc12ac00b4 --> 50acc543_e5f0_2162_cf07_c2bf50723e0c style 5174eefd_f47d_35f2_8053_b1fc12ac00b4 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Anthropic prompt caching middleware.
Requires:
- `langchain`: For agent middleware framework
- `langchain-anthropic`: For `ChatAnthropic` model (already a dependency)
"""
from collections.abc import Awaitable, Callable
from typing import Literal
from warnings import warn
from langchain_anthropic.chat_models import ChatAnthropic
try:
from langchain.agents.middleware.types import (
AgentMiddleware,
ModelCallResult,
ModelRequest,
ModelResponse,
)
except ImportError as e:
msg = (
"AnthropicPromptCachingMiddleware requires 'langchain' to be installed. "
"This middleware is designed for use with LangChain agents. "
"Install it with: pip install langchain"
)
raise ImportError(msg) from e
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.
// ... (88 more lines)
Domain
Subdomains
Functions
Classes
Dependencies
- collections.abc
- langchain.agents.middleware.types
- langchain_anthropic.chat_models
- typing
- warnings
Source
Frequently Asked Questions
What does prompt_caching.py do?
prompt_caching.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in prompt_caching.py?
prompt_caching.py defines 2 function(s): langchain, msg.
What does prompt_caching.py depend on?
prompt_caching.py imports 5 module(s): collections.abc, langchain.agents.middleware.types, langchain_anthropic.chat_models, typing, warnings.
Where is prompt_caching.py in the architecture?
prompt_caching.py is located at libs/partners/anthropic/langchain_anthropic/middleware/prompt_caching.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/partners/anthropic/langchain_anthropic/middleware).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free