Skip to main content
DeerFlow’s agent system is built on LangGraph, providing a powerful framework for orchestrating AI agents with tools, memory, and sub-agents.

Lead Agent

The lead agent is the entry point for all user interactions. It’s implemented in backend/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 the make_lead_agent() function, registered in langgraph.json:

Runtime Configuration

The agent behavior can be customized per-request via config.configurable:
boolean
default:"false"
Enable extended thinking mode for complex reasoning
string
Override the default model for this request
boolean
default:"false"
Enable TodoList middleware for task tracking
boolean
default:"true"
Enable sub-agent delegation tool

ThreadState

ThreadState extends LangGraph’s AgentState with DeerFlow-specific fields. Location: backend/src/agents/thread_state.py

State Fields

Conversation history with custom add_messages reducer.Type: list[BaseMessage]Includes HumanMessage, AIMessage, ToolMessage, and SystemMessage types.
Sandbox instance for file operations and command execution.Type: Optional[Sandbox]Set by SandboxMiddleware, used by sandbox tools.
Thread-specific metadata and configuration.Type: dict
Output files created by the agent.Type: list[dict]Reducer: merge_artifacts (deduplicates by path)
Task list for Plan Mode.Type: Optional[list[dict]]Only populated when is_plan_mode=true.
List of files uploaded by the user.Type: list[str]File paths in the uploads directory.
Images loaded for vision-enabled models.Type: list[dict]Reducer: merge_viewed_images (supports clearing)

Custom Reducers

merge_artifacts: Deduplicates artifacts by path
merge_viewed_images: Merges images, supports clearing

Middleware Chain

Middlewares execute in strict order around the agent:
  1. ThreadDataMiddleware - Creates thread directories
  2. UploadsMiddleware - Injects uploaded files into conversation
  3. SandboxMiddleware - Acquires and stores sandbox instance
  4. DanglingToolCallMiddleware - Handles interrupted tool calls
  5. SummarizationMiddleware - Context reduction (optional)
  6. TodoListMiddleware - Task tracking (optional, Plan Mode)
  7. TitleMiddleware - Auto-generates thread title
  8. MemoryMiddleware - Queues conversations for memory updates
  9. ViewImageMiddleware - Injects image data for vision models
  10. SubagentLimitMiddleware - Enforces concurrent subagent limits
  11. 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
Example:

Tool Loading

Tools are loaded via get_available_tools():
Tool Sources:
  1. Config-defined tools: From config.yaml via reflection
  2. MCP tools: From enabled MCP servers
  3. Built-in tools: present_files, ask_clarification, view_image
  4. Sub-agent tool: task() for delegation

Model Selection

Models are created via the model factory:
The 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