Lead Agent
The lead agent is the entry point for all user interactions. It’s implemented inbackend/src/agents/lead_agent/agent.py.
Key Components:
- Dynamic model selection
- Tool loading and management
- System prompt generation
- Middleware chain execution
Agent Creation
The agent is created via themake_lead_agent() function, registered in langgraph.json:
Runtime Configuration
The agent behavior can be customized per-request viaconfig.configurable:
Enable extended thinking mode for complex reasoning
Override the default model for this request
Enable TodoList middleware for task tracking
Enable sub-agent delegation tool
ThreadState
ThreadState extends LangGraph’sAgentState with DeerFlow-specific fields.
Location: backend/src/agents/thread_state.py
State Fields
messages
messages
Conversation history with custom
add_messages reducer.Type: list[BaseMessage]Includes HumanMessage, AIMessage, ToolMessage, and SystemMessage types.sandbox
sandbox
Sandbox instance for file operations and command execution.Type:
Optional[Sandbox]Set by SandboxMiddleware, used by sandbox tools.thread_data
thread_data
Thread-specific metadata and configuration.Type:
dictartifacts
artifacts
Output files created by the agent.Type:
list[dict]Reducer: merge_artifacts (deduplicates by path)todos
todos
Task list for Plan Mode.Type:
Optional[list[dict]]Only populated when is_plan_mode=true.uploaded_files
uploaded_files
List of files uploaded by the user.Type:
list[str]File paths in the uploads directory.viewed_images
viewed_images
Images loaded for vision-enabled models.Type:
list[dict]Reducer: merge_viewed_images (supports clearing)Custom Reducers
merge_artifacts: Deduplicates artifacts by pathMiddleware Chain
Middlewares execute in strict order around the agent:- ThreadDataMiddleware - Creates thread directories
- UploadsMiddleware - Injects uploaded files into conversation
- SandboxMiddleware - Acquires and stores sandbox instance
- DanglingToolCallMiddleware - Handles interrupted tool calls
- SummarizationMiddleware - Context reduction (optional)
- TodoListMiddleware - Task tracking (optional, Plan Mode)
- TitleMiddleware - Auto-generates thread title
- MemoryMiddleware - Queues conversations for memory updates
- ViewImageMiddleware - Injects image data for vision models
- SubagentLimitMiddleware - Enforces concurrent subagent limits
- ClarificationMiddleware - Handles ask_clarification interrupts (must be last)
Middleware Chain
Learn about each middleware in detail
System Prompt
The agent’s system prompt is dynamically generated with:- Skills: Enabled skills with their descriptions
- Memory: Top 15 facts and user context
- Sub-agents: Available sub-agent types
- Tools: Tool usage guidelines
- Sandbox paths: Virtual filesystem structure
Tool Loading
Tools are loaded viaget_available_tools():
- Config-defined tools: From
config.yamlvia reflection - MCP tools: From enabled MCP servers
- Built-in tools:
present_files,ask_clarification,view_image - Sub-agent tool:
task()for delegation
Model Selection
Models are created via the model factory:- Resolves model configuration from
config.yaml - Instantiates via reflection system
- Applies thinking mode overrides
- Handles environment variable resolution
Model Factory
Deep dive into model instantiation
Next Steps
Middleware Chain
Understand each middleware component
ThreadState
Deep dive into state management
Tools
Learn about available tools
Sub-agents
Delegate tasks to sub-agents