Middleware vs tools
langchain-anthropic provides two ways to use Claude’s native tools:
- Middleware (this page): Production-ready implementations with built-in execution, state management, and security policies
- Tools (via
bind_tools): Low-level building blocks where you provide your own execution logic
When to use which
Feature comparison
Example: Middleware vs tools comparison
Example: Middleware vs tools comparison
Using middleware (turnkey solution):Using tools (bring your own execution):
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 directly marks stable agent content such as the system prompt and tool definitions, and passescache_control through model_settings. The chat model and provider then handle the message-tail and provider-specific caching behavior, allowing the conversation history 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
Prompt caching reduces API costs by caching tokens, but does not provide conversation memory. To persist conversation history across invocations, use a checkpointer like
MemorySaver.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).
State-based text editor
Filesystem-based text editor
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
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: State-based text editor
Full example: State-based text editor
Full example: Filesystem-based text editor
Full example: Filesystem-based text editor
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
State-based memory
Filesystem-based memory
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: State-based memory
Full example: State-based memory
The agent will automatically:
- Check
/memoriesdirectory at start - Record progress and thoughts during execution
- Update memory files as work progresses
Full example: Filesystem-based memory
Full example: Filesystem-based memory
The agent will automatically:
- Check
/memoriesdirectory at start - Record progress and thoughts during execution
- Update memory files as work progresses
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: Search text editor files
Full example: Search text editor files
The middleware adds Glob and Grep search tools that work with state-based files.
Full example: Search memory files
Full example: Search memory files
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

