Planning: the write_todos Pattern
How a deep agent stays on track: the write_todos planning pattern keeps a todo list in the model's attention across long tasks — explained with code.
By the ToolsHub team · Updated September 14, 2026
Over a long run, an agent forgets. Twenty steps in, the original goal has scrolled out of useful attention and it starts wandering. The fix is deliberate planning: the agent keeps a running todo list — writing the plan, checking items off, and re-reading it as it works.
Turn it on
In deepagents, planning is a piece of middleware you add — it gives the agent a write_todos tool.
from deepagents import create_deep_agent
from langchain.agents.middleware.todo import TodoListMiddleware
agent = create_deep_agent(
model="anthropic:claude-sonnet-4-6",
system_prompt="You are a deep agent. Start every task by writing a todo list, then work through it.",
middleware=[TodoListMiddleware()], # adds the write_todos planning tool
)Why it works
The todo list is just structured text the model keeps re-reading — but that's enough to anchor a long task. It also makes the agent's progress legible to you: you can watch the plan evolve and see exactly where it is. Prompt the agent to plan first, then act, and to update the list as it learns new information.
You can toggle planning on any design in the Deep Agent Scaffold Generator and see the middleware wired into the generated code.