| Middleware | Description |
|---|---|
| Prompt caching | Reduce costs by caching repetitive prompt prefixes |
| Bash tool | Execute Claude’s native bash tool with local command execution |
| Text editor | Provide Claude’s text editor tool for file editing |
| Memory | Provide Claude’s memory tool for persistent agent memory |
| File search | Search tools for state-based file systems |
Prompt caching
Reduce costs and latency by caching static or repetitive prompt content (like system prompts, tool definitions, and conversation history) on Anthropic’s servers. This middleware implements a conversational caching strategy that places cache breakpoints after the most recent message, allowing the entire conversation history (including the latest user message) to be cached and reused in subsequent API calls. Prompt caching is useful for the following:- Applications with long, static system prompts that don’t change between requests
- Agents with many tool definitions that remain constant across invocations
- Conversations where early message history is reused across multiple turns
- High-volume deployments where reducing API costs and latency is critical
Learn more about Anthropic prompt caching strategies and limitations.
AnthropicPromptCachingMiddleware
Configuration options
Configuration options
Cache type. Only
'ephemeral' is currently supported.Time to live for cached content. Valid values:
'5m' or '1h'Minimum number of messages before caching starts
Behavior when using non-Anthropic models. Options:
'ignore', 'warn', or 'raise'Full example
Full example
The middleware caches content up to and including the latest message in each request. On subsequent requests within the TTL window (5 minutes or 1 hour), previously seen content is retrieved from cache rather than reprocessed, significantly reducing costs and latency.How it works:
- First request: System prompt, tools, and the user message “Hi, my name is Bob” are sent to the API and cached
- Second request: The cached content (system prompt, tools, and first message) is retrieved from cache. Only the new message “What’s my name?” needs to be processed, plus the model’s response from the first request
- This pattern continues for each turn, with each request reusing the cached conversation history
Bash tool
Execute Claude’s nativebash_20250124 tool with local command execution. The bash tool middleware is useful for the following:
- Using Claude’s built-in bash tool with local execution
- Leveraging Claude’s optimized bash tool interface
- Agents that need persistent shell sessions with Anthropic models
This middleware wraps
ShellToolMiddleware and exposes it as Claude’s native bash tool.ClaudeBashToolMiddleware
Configuration options
Configuration options
ClaudeBashToolMiddleware accepts all parameters from ShellToolMiddleware, including:Base directory for the shell session
Commands to run when the session starts
Execution policy (
HostExecutionPolicy, DockerExecutionPolicy, or CodexSandboxExecutionPolicy)Rules for sanitizing command output
Full example
Full example
Text editor
Provide Claude’s text editor tool (text_editor_20250728) for file creation and editing. The text editor middleware is useful for the following:
- File-based agent workflows
- Code editing and refactoring tasks
- Multi-file project work
- Agents that need persistent file storage
Available in two variants: State-based (files in LangGraph state) and Filesystem-based (files on disk).
StateClaudeTextEditorMiddleware, FilesystemClaudeTextEditorMiddleware
Configuration options
Configuration options
StateClaudeTextEditorMiddleware (state-based)Optional list of allowed path prefixes. If specified, only paths starting with these prefixes are allowed.
FilesystemClaudeTextEditorMiddleware (filesystem-based)Root directory for file operations
Optional list of allowed virtual path prefixes (default:
["/"])Maximum file size in MB
Full example
Full example
Claude’s text editor tool supports the following commands:
view- View file contents or list directorycreate- Create a new filestr_replace- Replace string in fileinsert- Insert text at line numberdelete- Delete a filerename- Rename/move a file
Memory
Provide Claude’s memory tool (memory_20250818) for persistent agent memory across conversation turns. The memory middleware is useful for the following:
- Long-running agent conversations
- Maintaining context across interruptions
- Task progress tracking
- Persistent agent state management
Claude’s memory tool uses a
/memories directory and automatically injects a system prompt encouraging the agent to check and update memory.StateClaudeMemoryMiddleware, FilesystemClaudeMemoryMiddleware
Configuration options
Configuration options
StateClaudeMemoryMiddleware (state-based)Optional list of allowed path prefixes. Defaults to
["/memories"].System prompt to inject. Defaults to Anthropic’s recommended memory prompt that encourages the agent to check and update memory.
FilesystemClaudeMemoryMiddleware (filesystem-based)Root directory for file operations
Optional list of allowed virtual path prefixes. Defaults to
["/memories"].Maximum file size in MB
System prompt to inject
Full example
Full example
File search
Provide Glob and Grep search tools for files stored in LangGraph state. File search middleware is useful for the following:- Searching through state-based virtual file systems
- Works with text editor and memory tools
- Finding files by patterns
- Content search with regex
StateFileSearchMiddleware
Configuration options
Configuration options
State key containing files to search. Use
"text_editor_files" for text editor files or "memory_files" for memory files.Full example
Full example
The middleware adds Glob and Grep search tools that work with state-based files.