langchain.js
    Preparing search index...

    Function todoListMiddleware

    • 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.

      Parameters

      Returns AgentMiddleware<
          ZodObject<
              {
                  todos: ZodDefault<
                      ZodArray<
                          ZodObject<
                              {
                                  content: ZodString;
                                  status: ZodEnum<["pending", "in_progress", "completed"]>;
                              },
                              "strip",
                              ZodTypeAny,
                              { content: string; status: "pending"
                              | "in_progress"
                              | "completed" },
                              { content: string; status: "pending" | "in_progress" | "completed" },
                          >,
                          "many",
                      >,
                  >;
              },
              "strip",
              ZodTypeAny,
              {
                  todos: {
                      content: string;
                      status: "pending"
                      | "in_progress"
                      | "completed";
                  }[];
              },
              {
                  todos?: {
                      content: string;
                      status: "pending"
                      | "in_progress"
                      | "completed";
                  }[];
              },
          >,
          undefined,
          InferInteropZodOutput<TContextSchema>,
          readonly [any],
      >

      A configured middleware instance that provides todo management capabilities

      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
      • TodoMiddlewareState for the state schema
      • writeTodos for the tool implementation