Creates a middleware that provides todo list management capabilities to agents.
This middleware adds a write_todos tool that allows agents to create and manage
structured task lists for complex multi-step operations. It's designed to help
agents track progress, organize complex tasks, and provide users with visibility
into task completion status.
The middleware automatically injects system prompts that guide the agent on when
and how to use the todo functionality effectively. It also enforces that the
write_todos tool is called at most once per model turn, since the tool replaces
the entire todo list and parallel calls would create ambiguity about precedence.
todoListMiddleware(
options: TodoListMiddlewareOptions
): AgentMiddleware<ZodObject<__type, "strip", ZodTypeAny, __type, __type>, undefined, unknown, readonly [DynamicStructuredTool<ZodObject<__type, "strip", ZodTypeAny, __type, __type>, __type, __type, Command<unknown, __type, string>, "write_todos">]>| Name | Type | Description |
|---|---|---|
options | TodoListMiddlewareOptions |
import { todoListMiddleware, createAgent } from 'langchain';
const agent = createAgent({
model: "openai:gpt-4o",
middleware: [todoListMiddleware()],
});
// Agent now has access to write_todos tool and todo state tracking
const result = await agent.invoke({
messages: [new HumanMessage("Help me refactor my codebase")]
});
console.log(result.todos); // Array of todo items with status tracking