from langchain_anthropic import ChatAnthropicfrom langchain_anthropic.middleware import StateClaudeTextEditorMiddlewarefrom langchain.agents import create_agentfrom langchain_core.runnables import RunnableConfigfrom langgraph.checkpoint.memory import MemorySaveragent = create_agent( model=ChatAnthropic(model="claude-sonnet-4-6"), tools=[], middleware=[ StateClaudeTextEditorMiddleware( allowed_path_prefixes=["/project"], ), ], checkpointer=MemorySaver(),)# 使用 thread_id 来持久化跨调用的状态config: RunnableConfig = {"configurable": {"thread_id": "my-session"}}# Claude 现在可以创建和编辑文件(存储在 LangGraph 状态中)result = agent.invoke( {"messages": [{"role": "user", "content": "Create a file at /project/hello.py with a simple hello world program"}]}, config=config,)print(result["messages"][-1].content)
I've created a simple "Hello, World!" program at `/project/hello.py`. The program uses Python's `print()` function to display "Hello, World!" to the console when executed.
完整示例:基于文件系统的文本编辑器
import tempfilefrom langchain_anthropic import ChatAnthropicfrom langchain_anthropic.middleware import FilesystemClaudeTextEditorMiddlewarefrom langchain.agents import create_agent# 为此演示创建一个临时工作区目录。# 在生产环境中,请使用持久化目录路径。workspace = tempfile.mkdtemp(prefix="editor-workspace-")agent = create_agent( model=ChatAnthropic(model="claude-sonnet-4-6"), tools=[], middleware=[ FilesystemClaudeTextEditorMiddleware( root_path=workspace, allowed_prefixes=["/src"], max_file_size_mb=10, ), ],)# Claude 现在可以创建和编辑文件(存储在磁盘上)result = agent.invoke( {"messages": [{"role": "user", "content": "Create a file at /src/hello.py with a simple hello world program"}]})print(result["messages"][-1].content)
I've created a simple "Hello, World!" program at `/src/hello.py`. The program uses Python's `print()` function to display "Hello, World!" to the console when executed.
from langchain_anthropic import ChatAnthropicfrom langchain_anthropic.middleware import StateClaudeMemoryMiddlewarefrom langchain.agents import create_agentfrom langchain_core.runnables import RunnableConfigfrom langgraph.checkpoint.memory import MemorySaveragent = create_agent( model=ChatAnthropic(model="claude-sonnet-4-6"), tools=[], middleware=[StateClaudeMemoryMiddleware()], checkpointer=MemorySaver(),)# 使用 thread_id 来持久化跨调用的状态config: RunnableConfig = {"configurable": {"thread_id": "my-session"}}# Claude 现在可以使用记忆来跟踪进度(存储在 LangGraph 状态中)result = agent.invoke( {"messages": [{"role": "user", "content": "Remember that my favorite color is blue, then confirm what you stored."}]}, config=config,)print(result["messages"][-1].content)
Perfect! I've stored your favorite color as **blue** in my memory system. The information is saved in my user preferences file where I can access it in future conversations.
完整示例:基于文件系统的记忆
代理将自动:
在开始时检查 /memories 目录
在执行期间记录进度和想法
随着工作进展更新记忆文件
import tempfilefrom langchain_anthropic import ChatAnthropicfrom langchain_anthropic.middleware import FilesystemClaudeMemoryMiddlewarefrom langchain.agents import create_agent# 为此演示创建一个临时工作区目录。# 在生产环境中,请使用持久化目录路径。workspace = tempfile.mkdtemp(prefix="memory-workspace-")agent = create_agent( model=ChatAnthropic(model="claude-sonnet-4-6"), tools=[], middleware=[ FilesystemClaudeMemoryMiddleware( root_path=workspace, ), ],)# Claude 现在可以使用记忆来跟踪进度(存储在磁盘上)result = agent.invoke( {"messages": [{"role": "user", "content": "Remember that my favorite color is blue, then confirm what you stored."}]})print(result["messages"][-1].content)
Perfect! I've stored your favorite color as **blue** in my memory system. The information is saved in my user preferences file where I can access it in future conversations.
I found 5 Python files in the project:1. `/project/main.py` - Main application file2. `/project/utils/__init__.py` - Utils package initialization3. `/project/utils/helpers.py` - Helper utilities4. `/project/tests/__init__.py` - Tests package initialization5. `/project/tests/test_main.py` - Main test fileWould you like me to view the contents of any of these files?
完整示例:搜索记忆文件
from langchain_anthropic import ChatAnthropicfrom langchain_anthropic.middleware import ( StateClaudeMemoryMiddleware, StateFileSearchMiddleware,)from langchain.agents import create_agentfrom langchain.messages import HumanMessagefrom langchain_core.runnables import RunnableConfigfrom langgraph.checkpoint.memory import MemorySaveragent = create_agent( model=ChatAnthropic(model="claude-sonnet-4-6"), tools=[], middleware=[ StateClaudeMemoryMiddleware(), StateFileSearchMiddleware(state_key="memory_files"), ], checkpointer=MemorySaver(),)# 使用 thread_id 来持久化跨调用的状态config: RunnableConfig = {"configurable": {"thread_id": "my-session"}}# 第一次调用:记录一些记忆result = agent.invoke( {"messages": [HumanMessage("Remember that the project deadline is March 15th and code review deadline is March 10th")]}, config=config,)# 代理创建记忆文件,这些文件存储在状态中print("Memory files created:", list(result["memory_files"].keys()))# 第二次调用:搜索我们刚刚记录的记忆# 状态通过 checkpointer 自动持久化result = agent.invoke( {"messages": [HumanMessage("Search my memories for project deadlines")]}, config=config,)print(result["messages"][-1].content)
I found your project deadlines in my memory! Here's what I have recorded:## Important Deadlines- **Code Review Deadline:** March 10th- **Project Deadline:** March 15th## Notes- Code review must be completed 5 days before final project deadline- Need to ensure all code is ready for review by March 10thIs there anything specific about these deadlines you'd like to know or update?