backend/docs/plan_mode_usage.md
This document describes how to enable and use the Plan Mode feature with TodoList middleware in DeerFlow 2.0.
Plan Mode adds a TodoList middleware to the agent, which provides a write_todos tool that helps the agent:
The TodoList middleware is built on LangChain's TodoListMiddleware.
Plan mode is controlled via runtime configuration through the is_plan_mode parameter in the configurable section of RunnableConfig. This allows you to dynamically enable or disable plan mode on a per-request basis.
from langchain_core.runnables import RunnableConfig
from deerflow.agents.lead_agent.agent import make_lead_agent
# Enable plan mode via runtime configuration
config = RunnableConfig(
configurable={
"thread_id": "example-thread",
"thinking_enabled": True,
"is_plan_mode": True, # Enable plan mode
}
)
# Create agent with plan mode enabled
agent = make_lead_agent(config)
False
config.get("configurable", {}).get("is_plan_mode", False)When plan mode is enabled with default settings, the agent will have access to a write_todos tool with the following behavior:
The agent will use the todo list for:
The agent will skip using the todo list for:
from langchain_core.runnables import RunnableConfig
from deerflow.agents.lead_agent.agent import make_lead_agent
# Create agent with plan mode ENABLED
config_with_plan_mode = RunnableConfig(
configurable={
"thread_id": "example-thread",
"thinking_enabled": True,
"is_plan_mode": True, # TodoList middleware will be added
}
)
agent_with_todos = make_lead_agent(config_with_plan_mode)
# Create agent with plan mode DISABLED (default)
config_without_plan_mode = RunnableConfig(
configurable={
"thread_id": "another-thread",
"thinking_enabled": True,
"is_plan_mode": False, # No TodoList middleware
}
)
agent_without_todos = make_lead_agent(config_without_plan_mode)
You can enable/disable plan mode dynamically for different conversations or tasks:
from langchain_core.runnables import RunnableConfig
from deerflow.agents.lead_agent.agent import make_lead_agent
def create_agent_for_task(task_complexity: str):
"""Create agent with plan mode based on task complexity."""
is_complex = task_complexity in ["high", "very_high"]
config = RunnableConfig(
configurable={
"thread_id": f"task-{task_complexity}",
"thinking_enabled": True,
"is_plan_mode": is_complex, # Enable only for complex tasks
}
)
return make_lead_agent(config)
# Simple task - no TodoList needed
simple_agent = create_agent_for_task("low")
# Complex task - TodoList enabled for better tracking
complex_agent = create_agent_for_task("high")
make_lead_agent(config) is called, it extracts is_plan_mode from config.configurable_build_middlewares(config)_build_middlewares() reads is_plan_mode and calls _create_todo_list_middleware(is_plan_mode)is_plan_mode=True, a TodoListMiddleware instance is created and added to the middleware chainwrite_todos tool to the agent's toolsetmake_lead_agent(config)
│
├─> Extracts: is_plan_mode = config.configurable.get("is_plan_mode", False)
│
└─> _build_middlewares(config)
│
├─> ThreadDataMiddleware
├─> SandboxMiddleware
├─> SummarizationMiddleware (if enabled via global config)
├─> TodoListMiddleware (if is_plan_mode=True) ← NEW
├─> TitleMiddleware
└─> ClarificationMiddleware
packages/harness/deerflow/agents/lead_agent/agent.py_create_todo_list_middleware(is_plan_mode: bool) - Creates TodoListMiddleware if plan mode is enabled_build_middlewares(config: RunnableConfig) - Builds middleware chain based on runtime configmake_lead_agent(config: RunnableConfig) - Creates agent with appropriate middlewaresPlan mode is controlled via the is_plan_mode parameter in RunnableConfig.configurable:
config = RunnableConfig(
configurable={
"is_plan_mode": True, # Enable plan mode
# ... other configurable options
}
)
DeerFlow uses custom system_prompt and tool_description for the TodoListMiddleware that match the overall DeerFlow prompt style:
<todo_list_system>) for structure consistency with DeerFlow's main promptThe custom prompts are defined in _create_todo_list_middleware() in /Users/hetao/workspace/deer-flow/backend/packages/harness/deerflow/agents/lead_agent/agent.py:57.
TodoListMiddleware with custom DeerFlow-style promptsis_plan_mode=False) to maintain backward compatibilityClarificationMiddleware to allow todo management during clarification flows