Skip to main content

Anthropic middleware integration

Middleware specifically designed for Anthropic's Claude models. Learn more about middleware.

MiddlewareDescription
Prompt cachingReduce costs by caching repetitive prompt prefixes
Bash toolExecute Claude's native bash tool with local command execution
Text editorProvide Claude's text editor tool for file editing
MemoryProvide Claude's memory tool for persistent agent memory
File searchSearch tools for state-based file systems

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

Use caseRecommendedWhy
Production agents with bashMiddlewarePersistent sessions, Docker isolation, output redaction
State-based file editingMiddlewareBuilt-in LangGraph state persistence
Filesystem file editingMiddlewareWrites to disk with path validation
Custom execution logicToolsFull control over execution
Quick prototypeToolsSimpler, bring your own callback
Non-agent use with bind_toolsToolsMiddleware requires create_agent

Feature comparison

FeatureMiddlewareTools
Works with create_agent✅✅
Works with bind_tools❌✅
Built-in state management✅❌
Custom execute callback❌✅

Example: Middleware vs tools comparison


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 passes cache_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

Tip

For simpler use cases, you can also use automatic caching by passing cache_control at invocation time without middleware. The middleware is recommended when you need explicit control over cache breakpoints on system prompts and tool definitions.

Info

Learn more about Anthropic prompt caching strategies and limitations.

API reference: AnthropicPromptCachingMiddleware

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import AnthropicPromptCachingMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    system_prompt="<Your long system prompt here>",
    middleware=[AnthropicPromptCachingMiddleware(ttl="5m")], # [!code highlight]
)

Configuration options

Full example

Bash tool

Execute Claude's native bash_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

Info

This middleware wraps ShellToolMiddleware and exposes it as Claude's native bash tool.

API reference: ClaudeBashToolMiddleware

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import ClaudeBashToolMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[ # [!code highlight]
        ClaudeBashToolMiddleware( # [!code highlight]
            workspace_root="/workspace", # [!code highlight]
        ), # [!code highlight]
    ], # [!code highlight]
)

Configuration options

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

Note

Available in two variants: State-based (files in LangGraph state) and Filesystem-based (files on disk).

API references:

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import StateClaudeTextEditorMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[StateClaudeTextEditorMiddleware()], # [!code highlight]
)
from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import FilesystemClaudeTextEditorMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[ # [!code highlight]
        FilesystemClaudeTextEditorMiddleware( # [!code highlight]
            root_path="/workspace", # [!code highlight]
        ), # [!code highlight]
    ], # [!code highlight]
)

Claude's text editor tool supports the following commands:

  • view - View file contents or list directory
  • create - Create a new file
  • str_replace - Replace string in file
  • insert - Insert text at line number
  • delete - Delete a file
  • rename - Rename/move a file

Configuration options

Full example: State-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

Info

Claude's memory tool uses a /memories directory and automatically injects a system prompt encouraging the agent to check and update memory.

API reference: StateClaudeMemoryMiddleware, FilesystemClaudeMemoryMiddleware

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import StateClaudeMemoryMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[StateClaudeMemoryMiddleware()], # [!code highlight]
)
from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import FilesystemClaudeMemoryMiddleware
from langchain.agents import create_agent

agent_fs = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[ # [!code highlight]
        FilesystemClaudeMemoryMiddleware( # [!code highlight]
            root_path="/workspace", # [!code highlight]
        ), # [!code highlight]
    ], # [!code highlight]
)

Configuration options

Full example: State-based memory

Full example: Filesystem-based memory

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

API reference: StateFileSearchMiddleware

from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import (
    StateClaudeTextEditorMiddleware,
    StateFileSearchMiddleware,
)
from langchain.agents import create_agent

agent = create_agent(
    model=ChatAnthropic(model="claude-sonnet-4-6"),
    tools=[],
    middleware=[ # [!code highlight]
        StateClaudeTextEditorMiddleware(), # [!code highlight]
        StateFileSearchMiddleware(),  # Search text editor files [!code highlight]
    ], # [!code highlight]
)

Configuration options

Full example: Search text editor files

Full example: Search memory files