How xufei56789/deer-flow works
1. Repository shape
- This is a monorepo for DeerFlow 2.0, an agent harness with three main execution surfaces: HTTP Gateway/backend, Next.js web frontend, and an embedded Python/TUI runtime that can run without the web stack.
- Most of the system is implemented in Python 3.12+ under backend/, with a separate Node.js 22+ frontend under frontend/.
2. Primary runtime boundary: Gateway vs harness
- The backend is split between a Gateway API (backend/app/gateway) and the harness/runtime (backend/packages/harness/deerflow).
- The Gateway is the public HTTP surface: auth, thread/run endpoints, uploads, skills, memory, models, channels, artifacts, and LangGraph-compatible routes.
- The harness contains the actual agent runtime: model orchestration, sub-agents, tools, memory, sandbox, persistence, tracing, and TUI support.
3. Core request flow
- A user message enters either the Gateway HTTP API, the web frontend, an IM channel, or the embedded client/TUI.
- The request is normalized into a thread/run invocation, then dispatched into the LangGraph-based agent runtime.
- During the run, the agent may call tools, spawn sub-agents, read/write sandbox files, update memory, and stream events back through the runtime stream bridge to the caller.
4. Agent orchestration layer
- The main orchestration logic lives under backend/packages/harness/deerflow/agents, subagents, tools, and runtime.
- The lead agent is the default coordinator; it can spawn specialized sub-agents, collect results, and synthesize the final answer.
- Middleware is a major architectural layer: there are dedicated middlewares for clarification, loop detection, memory injection, summarization, safety termination, token budgeting, tool error handling, uploads, and view-image handling.
5. Skills and tool extensibility
- Skills are a first-class capability system stored in skills/ and managed by backend/packages/harness/deerflow/skills.
- Skills are loaded progressively and can be activated via slash commands or by the agent runtime; they influence prompts and allowed behavior without loading everything up front.
- Tools are split between built-in harness tools and external tool integrations, including MCP servers, search providers, file tools, bash, and custom tool sync/install flows.
6. Sandbox and file-system execution
- The system treats filesystem access as an isolated execution boundary. Sandbox code is under backend/packages/harness/deerflow/sandbox with local and container-backed implementations.
- There are at least three execution modes: local execution, Docker-based execution, and provisioned Kubernetes/container execution through the aio sandbox/provisioner path.
- The sandbox exposes per-thread workspace, uploads, and outputs, which the agent uses for artifacts, reports, generated files, and intermediate state.
7. Persistence and runtime state
- Durable state is split into several stores under backend/packages/harness/deerflow/persistence and backend/packages/harness/deerflow/runtime.
- Key persisted concepts include threads, runs, run events, thread metadata, users, feedback, and database migrations.
- Runtime stores also handle run lifecycle, event streaming, checkpointer state, stream bridging, and journal-like recordkeeping, with SQLite/Postgres support called out in the README.
8. Authentication and authorization
- Auth is concentrated in backend/app/gateway/auth with support for local auth, password auth, JWT, OIDC, credential files, and user provisioning.
- The Gateway applies auth middleware and CSRF protections before requests reach thread/run operations.
- There is also internal auth for service-to-service calls, especially for IM channel workers and LangGraph-compatible internal requests.
9. Channels and external ingress
- The backend/app/channels subsystem handles inbound tasks from Telegram, Slack, Feishu/Lark, WeChat, WeCom, and DingTalk.
- These channels are not just notifications; they are alternate ingress paths that create or resume threads and run the same agent runtime.
- Channel connections can be user-owned and managed from the workspace UI, which means channel state is part of the product’s persistent user workspace, not a separate bot system.
10. Web frontend architecture
- The frontend is a Next.js app with routes for landing pages, docs, blog, auth, and the authenticated workspace.
- The workspace UI is organized around agents, chats, threads, artifacts, settings, channels, memory, and live streaming.
- Frontend data access is split into frontend/src/core/* API wrappers and React hooks, which mirror backend resources like threads, models, skills, uploads, memory, MCP, and channels.
11. Embedded client and TUI
- DeerFlow is not only web-based: backend/packages/harness/deerflow/client.py exposes an embedded Python client that uses the same schemas and runtime as the HTTP Gateway.
- The TUI under backend/packages/harness/deerflow/tui is a terminal-native front end built on top of that embedded client, sharing the same config, memory, sandbox, and thread store.
- This means the repo supports both “full platform” deployment and “library/CLI” use without splitting the agent logic.
12. Observability and tracing
- The system integrates with LangSmith and Langfuse for LLM/run tracing.
- Trace correlation is propagated through request metadata, thread/session IDs, and model invocation roots so that HTTP requests, agent runs, and callback traces can be joined.
- There is also explicit support for request trace IDs, run events, and replay fixtures/tests, which makes runtime observability a deliberate architecture concern.
13. Security and safety boundaries
- Security is treated as an architectural layer, not just deployment advice: auth middleware, CSRF, sandbox isolation, read-before-write gating, safety termination detectors, and artifact download protections are all present.
- The README warns that host access and public deployment are risky; the design assumes trusted local or tightly controlled environments unless additional controls are added.
- Blocking I/O checks and thread-boundary detectors in backend/tests/support reinforce the expectation that the event loop and runtime boundaries stay clean.
14. Supporting infrastructure
- The repo includes production and development deployment paths via Docker Compose, nginx, a provisioner service, and Makefile/script-based orchestration.
- CI covers backend unit tests, blocking I/O tests, frontend unit/e2e tests, and replay tests, reflecting the multi-surface architecture.
- Documentation is extensive and split by concern: backend architecture, auth, sandboxing, MCP, IM channels, streaming, memory, and TUI.
15. How to draw the diagram
- The cleanest architectural diagram would show:
- external users and IM channels feeding into the Gateway
- the Gateway delegating into the harness/runtime
- the runtime talking to LLM providers, sandbox/file system, persistence, MCP/search services, and tracing backends
- the web frontend and TUI/embedded client as alternate clients of the same backend/runtime
- The key boundary is that the agent logic lives in the harness, while the Gateway is the orchestration and API facade around it.
Main entry points: agent.py Lead Agent · runtime/__init__.py Runtime State · routers/__init__.py API Routes · app.py Gateway API
Components 21
Clients
User entrypoints and alternate runtimes
- Embedded Client · Library-style client that talks to the same runtime without the web stack
backend/packages/harness/deerflow/client.py - TUI · Terminal front end built on the embedded client and shared runtime state
backend/packages/harness/deerflow/tui/app.py
Backend
HTTP gateway, channels, and runtime facade
- Gateway API · Public backend surface for auth, threads, runs, uploads, skills, memory, models, channels, and artifacts
backend/app/gateway/app.py - Auth · Login, JWT/OIDC, local auth, CSRF, and user provisioning
backend/app/gateway/auth/__init__.py - API Routes · Thread/run, artifacts, memory, skills, MCP, models, uploads, feedback, and channels endpoints
backend/app/gateway/routers/__init__.py - IM Channels · Telegram, Slack, Feishu, WeChat, WeCom, DingTalk, and Discord inbound workers
backend/app/channels/__init__.py
Harness
Agent runtime, tools, state, and execution
- Lead Agent · Default coordinator that plans work, delegates, and synthesizes responses
backend/packages/harness/deerflow/agents/lead_agent/agent.py - Middlewares · Clarification, memory, loop detection, summarization, safety, token budgeting, and tool error handling
backend/packages/harness/deerflow/agents/middlewares/__init__.py - Sub-agents · Specialized workers such as bash and general-purpose agents
backend/packages/harness/deerflow/subagents/__init__.py - Tools · Built-in actions, ACP/skill tools, file presentation, task tools, and view-image helpers
backend/packages/harness/deerflow/tools/__init__.py - Skills · Progressive skill loading, slash commands, permissions, and skill storage
backend/packages/harness/deerflow/skills/__init__.py - Sandbox · Local and container-backed filesystem/runtime isolation for files, uploads, and outputs
backend/packages/harness/deerflow/sandbox/__init__.py - Runtime State · Run manager, checkpointer, stream bridge, journal, and event handling
backend/packages/harness/deerflow/runtime/__init__.py - Persistence · Threads, runs, run events, users, feedback, migrations, and store providers
backend/packages/harness/deerflow/persistence/__init__.py - Model Providers · Provider factory and adapters for OpenAI, Claude, vLLM, MindIE, and patched variants
backend/packages/harness/deerflow/models/__init__.py - MCP · Model Context Protocol clients, cache, OAuth, session pooling, and tool bridges
backend/packages/harness/deerflow/mcp/__init__.py - Stream Bridge · Bridges run events back to HTTP, IM channels, TUI, and embedded clients
backend/packages/harness/deerflow/runtime/stream_bridge/__init__.py
Frontend
Next.js workspace and docs UI
- Web Frontend · Landing pages, auth, docs, and authenticated workspace UI
frontend/src/app/page.tsx - Frontend Core · Typed API wrappers and hooks for threads, skills, models, channels, memory, uploads, and MCP
frontend/src/core/api/api-client.ts - Workspace UI · Chats, artifacts, channels, settings, streaming, and thread views
frontend/src/components/workspace/workspace-container.tsx
Infra
Deployment, tracing, and supporting services
- Tracing · LangSmith/Langfuse integration and trace metadata propagation
backend/packages/harness/deerflow/tracing/__init__.py
Connections 23
- Web Frontend → Frontend Core: uses · UI calls shared API wrappers and hooks
- Frontend Core → Gateway API: API calls · Frontend reads and mutates backend resources
- Workspace UI → Frontend Core: binds · Workspace components depend on the core API layer
- Gateway API → Auth: protects with · Authentication and CSRF gates wrap public routes
- Gateway API → API Routes: exposes · Route set for threads, runs, uploads, memory, skills, models, and channels
- API Routes → Runtime State: dispatches to · Thread/run operations enter the runtime lifecycle layer
- API Routes → Persistence: reads/writes · Routes persist threads, runs, users, feedback, and events
- API Routes → MCP: proxies · Gateway exposes MCP-related resource management
- IM Channels → Runtime State: starts runs · IM messages normalize into thread/run invocations
- Embedded Client → Runtime State: invokes · Embedded client calls the same runtime as the web backend
- TUI → Embedded Client: uses · TUI is built on the embedded client
- Runtime State → Lead Agent: executes · Run manager hands work to the main agent coordinator
- Lead Agent → Middlewares: passes through · Policy and context middlewares shape agent execution
- Lead Agent → Sub-agents: delegates to · Coordinator can spawn specialized sub-agents
- Lead Agent → Tools: calls · Agent performs tool actions during runs
- Lead Agent → Skills: activates · Skills affect prompts, permissions, and allowed behavior
- Lead Agent → Sandbox: uses · Agent reads and writes within isolated workspaces
- Lead Agent → Model Providers: queries · Agent plans and generates through model providers
- Runtime State → Stream Bridge: streams via · Runtime events are bridged back to callers
- Runtime State → Persistence: records · Run lifecycle and event data are durably stored
- Runtime State → Tracing: emits · Trace metadata and run spans propagate to observability backends
- Tools → Sandbox: operates in · File and execution tools run against sandboxed workspaces
- Tools → MCP: integrates with · Tool system can load MCP-backed capabilities