Anthropic Middleware¶
Reference docs
This page contains reference documentation for Anthropic Middleware. See the docs for conceptual guides, tutorials, and examples on using Anthropic Middleware.
Provider-specific middleware for Anthropic's Claude models:
| CLASS | DESCRIPTION |
|---|---|
AnthropicPromptCachingMiddleware |
Reduce costs by caching repetitive prompt prefixes |
ClaudeBashToolMiddleware |
Execute Claude's native bash tool with local command execution |
StateClaudeTextEditorMiddleware |
Provide Claude's text editor tool for state-based file editing |
FilesystemClaudeTextEditorMiddleware |
Provide Claude's text editor tool for filesystem-based file editing |
StateClaudeMemoryMiddleware |
Provide Claude's memory tool for state-based persistent agent memory |
FilesystemClaudeMemoryMiddleware |
Provide Claude's memory tool for filesystem-based persistent agent memory |
StateFileSearchMiddleware |
Search tools for state-based file systems |
AnthropicPromptCachingMiddleware
¶
AnthropicPromptCachingMiddleware(
type: Literal["ephemeral"] = "ephemeral",
ttl: Literal["5m", "1h"] = "5m",
min_messages_to_cache: int = 0,
unsupported_model_behavior: Literal["ignore", "warn", "raise"] = "warn",
)
Bases: 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.
Initialize the middleware with cache control settings.
| PARAMETER | DESCRIPTION |
|---|---|
type
|
The type of cache to use, only
TYPE:
|
ttl
|
The time to live for the cache, only
TYPE:
|
min_messages_to_cache
|
The minimum number of messages until the cache is used.
TYPE:
|
unsupported_model_behavior
|
The behavior to take when an unsupported model is used.
TYPE:
|
ClaudeBashToolMiddleware
¶
ClaudeBashToolMiddleware(
workspace_root: str | None = None,
*,
startup_commands: tuple[str, ...] | list[str] | str | None = None,
shutdown_commands: tuple[str, ...] | list[str] | str | None = None,
execution_policy: Any | None = None,
redaction_rules: tuple[Any, ...] | list[Any] | None = None,
tool_description: str | None = None,
env: dict[str, Any] | None = None,
)
Bases: ShellToolMiddleware
Middleware that exposes Anthropic's native bash tool to models.
Initialize middleware for Claude's native bash tool.
| PARAMETER | DESCRIPTION |
|---|---|
workspace_root
|
Base directory for the shell session. If omitted, a temporary directory is created.
TYPE:
|
startup_commands
|
Optional commands executed after the session starts.
TYPE:
|
shutdown_commands
|
Optional commands executed before session shutdown.
TYPE:
|
execution_policy
|
Execution policy controlling timeouts and limits.
TYPE:
|
redaction_rules
|
Optional redaction rules to sanitize output. |
tool_description
|
Optional override for tool description.
TYPE:
|
env
|
Optional environment variables for the shell session. |
StateClaudeTextEditorMiddleware
¶
Bases: _StateClaudeFileToolMiddleware
State-based text editor tool middleware.
Provides Anthropic's text_editor tool using LangGraph state for storage.
Files persist for the conversation thread.
Example
Initialize the text editor middleware.
| PARAMETER | DESCRIPTION |
|---|---|
allowed_path_prefixes
|
Optional list of allowed path prefixes. If specified, only paths starting with these prefixes are allowed. |
FilesystemClaudeTextEditorMiddleware
¶
FilesystemClaudeTextEditorMiddleware(
*,
root_path: str,
allowed_prefixes: list[str] | None = None,
max_file_size_mb: int = 10,
)
Bases: _FilesystemClaudeFileToolMiddleware
Filesystem-based text editor tool middleware.
Provides Anthropic's text_editor tool using local filesystem for storage.
User handles persistence via volumes, git, or other mechanisms.
Example
Initialize the text editor middleware.
| PARAMETER | DESCRIPTION |
|---|---|
root_path
|
Root directory for file operations.
TYPE:
|
allowed_prefixes
|
Optional list of allowed virtual path prefixes. Defaults to |
max_file_size_mb
|
Maximum file size in MB Defaults to
TYPE:
|
StateClaudeMemoryMiddleware
¶
StateClaudeMemoryMiddleware(
*,
allowed_path_prefixes: Sequence[str] | None = None,
system_prompt: str = MEMORY_SYSTEM_PROMPT,
)
Bases: _StateClaudeFileToolMiddleware
State-based memory tool middleware.
Provides Anthropic's memory tool using LangGraph state for storage. Files persist for the conversation thread.
Enforces /memories prefix and injects Anthropic's recommended system prompt.
Example
Initialize the memory middleware.
| PARAMETER | DESCRIPTION |
|---|---|
allowed_path_prefixes
|
Optional list of allowed path prefixes. Defaults to |
system_prompt
|
System prompt to inject. Defaults to Anthropic's recommended memory prompt.
TYPE:
|
FilesystemClaudeMemoryMiddleware
¶
FilesystemClaudeMemoryMiddleware(
*,
root_path: str,
allowed_prefixes: list[str] | None = None,
max_file_size_mb: int = 10,
system_prompt: str = MEMORY_SYSTEM_PROMPT,
)
Bases: _FilesystemClaudeFileToolMiddleware
Filesystem-based memory tool middleware.
Provides Anthropic's memory tool using local filesystem for storage. User handles persistence via volumes, git, or other mechanisms.
Enforces /memories prefix and injects Anthropic's recommended system
prompt.
Example
Initialize the memory middleware.
| PARAMETER | DESCRIPTION |
|---|---|
root_path
|
Root directory for file operations.
TYPE:
|
allowed_prefixes
|
Optional list of allowed virtual path prefixes. Defaults to |
max_file_size_mb
|
Maximum file size in MB Defaults to
TYPE:
|
system_prompt
|
System prompt to inject. Defaults to Anthropic's recommended memory prompt.
TYPE:
|
StateFileSearchMiddleware
¶
StateFileSearchMiddleware(*, state_key: str = 'text_editor_files')
Bases: AgentMiddleware
Provides Glob and Grep search over state-based files.
This middleware adds two tools that search through virtual files in state:
- Glob: Fast file pattern matching by file path
- Grep: Fast content search using regular expressions
Example
Initialize the search middleware.
| PARAMETER | DESCRIPTION |
|---|---|
state_key
|
State key to search Use
TYPE:
|