# xufei56789/deer-flow architecture

> Architecture diagram of the GitHub repository xufei56789/deer-flow, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xufei56789/deer-flow
- Repository: https://github.com/xufei56789/deer-flow

## Overview

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: [`backend/packages/harness/deerflow/agents/lead_agent/agent.py`](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/agents/lead_agent/agent.py) (Lead Agent), [`backend/packages/harness/deerflow/runtime/__init__.py`](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/runtime/__init__.py) (Runtime State), [`backend/app/gateway/routers/__init__.py`](https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/routers/__init__.py) (API Routes), [`backend/app/gateway/app.py`](https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/app.py) (Gateway API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Clients"]
  node_harness_client["Embedded Client<br/>python client<br/>[client.py]"]
  node_tui["TUI<br/>terminal ui<br/>[app.py]"]
end

subgraph group_backend["Backend"]
  node_gateway_app["Gateway API<br/>http api<br/>[app.py]"]
  node_gateway_auth["Auth<br/>[__init__.py]"]
  node_gateway_routes["API Routes<br/>rest routers<br/>[__init__.py]"]
  node_channel_ingress["IM Channels<br/>ingress adapters<br/>[__init__.py]"]
end

subgraph group_harness["Harness"]
  node_lead_agent["Lead Agent<br/>orchestrator<br/>[agent.py]"]
  node_middlewares["Middlewares<br/>policy chain<br/>[__init__.py]"]
  node_subagents["Sub-agents<br/>delegation layer<br/>[__init__.py]"]
  node_tools["Tools<br/>tooling layer<br/>[__init__.py]"]
  node_skills["Skills<br/>capability system<br/>[__init__.py]"]
  node_sandbox["Sandbox<br/>execution boundary<br/>[__init__.py]"]
  node_runtime_state["Runtime State<br/>lifecycle state<br/>[__init__.py]"]
  node_persistence["Persistence<br/>durable store<br/>[__init__.py]"]
  node_models["Model Providers<br/>llm adapters<br/>[__init__.py]"]
  node_mcp["MCP<br/>tool protocol<br/>[__init__.py]"]
  node_stream_bridge["Stream Bridge<br/>event streaming<br/>[__init__.py]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["Web Frontend<br/>next.js app<br/>[page.tsx]"]
  node_frontend_core["Frontend Core<br/>api client layer<br/>[api-client.ts]"]
  node_frontend_workspace["Workspace UI<br/>ui surface"]
end

subgraph group_infra["Infra"]
  node_tracing["Tracing<br/>observability<br/>[__init__.py]"]
end

node_frontend_app -->|"uses"| node_frontend_core
node_frontend_core -->|"API calls"| node_gateway_app
node_frontend_workspace -->|"binds"| node_frontend_core
node_gateway_app -->|"protects with"| node_gateway_auth
node_gateway_app -->|"exposes"| node_gateway_routes
node_gateway_routes -->|"dispatches to"| node_runtime_state
node_gateway_routes -->|"reads/writes"| node_persistence
node_gateway_routes -->|"proxies"| node_mcp
node_channel_ingress -->|"starts runs"| node_runtime_state
node_harness_client -->|"invokes"| node_runtime_state
node_tui -->|"uses"| node_harness_client
node_runtime_state -->|"executes"| node_lead_agent
node_lead_agent -->|"passes through"| node_middlewares
node_lead_agent -->|"delegates to"| node_subagents
node_lead_agent -->|"calls"| node_tools
node_lead_agent -->|"activates"| node_skills
node_lead_agent -->|"uses"| node_sandbox
node_lead_agent -->|"queries"| node_models
node_runtime_state -->|"streams via"| node_stream_bridge
node_runtime_state -->|"records"| node_persistence
node_runtime_state -->|"emits"| node_tracing
node_tools -->|"operates in"| node_sandbox
node_tools -->|"integrates with"| node_mcp

click node_gateway_app "https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/app.py"
click node_gateway_auth "https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/auth/__init__.py"
click node_gateway_routes "https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/routers/__init__.py"
click node_channel_ingress "https://github.com/xufei56789/deer-flow/blob/main/backend/app/channels/__init__.py"
click node_harness_client "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/client.py"
click node_tui "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tui/app.py"
click node_lead_agent "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/agents/lead_agent/agent.py"
click node_middlewares "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/agents/middlewares/__init__.py"
click node_subagents "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/subagents/__init__.py"
click node_tools "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tools/__init__.py"
click node_skills "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/skills/__init__.py"
click node_sandbox "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/sandbox/__init__.py"
click node_runtime_state "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/runtime/__init__.py"
click node_persistence "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/persistence/__init__.py"
click node_models "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/models/__init__.py"
click node_mcp "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/mcp/__init__.py"
click node_tracing "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tracing/__init__.py"
click node_stream_bridge "https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/runtime/stream_bridge/__init__.py"
click node_frontend_app "https://github.com/xufei56789/deer-flow/blob/main/frontend/src/app/page.tsx"
click node_frontend_core "https://github.com/xufei56789/deer-flow/blob/main/frontend/src/core/api/api-client.ts"
click node_frontend_workspace "https://github.com/xufei56789/deer-flow/blob/main/frontend/src/components/workspace/workspace-container.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_harness_client,node_tui toneBlue
class node_gateway_app,node_gateway_auth,node_gateway_routes,node_channel_ingress toneAmber
class node_lead_agent,node_middlewares,node_subagents,node_tools,node_skills,node_sandbox,node_runtime_state,node_persistence,node_models,node_mcp,node_stream_bridge toneMint
class node_frontend_app,node_frontend_core,node_frontend_workspace toneRose
class node_tracing toneIndigo
```

## Components

### Clients

User entrypoints and alternate runtimes

- [Embedded Client](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/client.py): python client · Library-style client that talks to the same runtime without the web stack · `backend/packages/harness/deerflow/client.py`
- [TUI](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tui/app.py): terminal ui · 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](https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/app.py): http api · Public backend surface for auth, threads, runs, uploads, skills, memory, models, channels, and artifacts · `backend/app/gateway/app.py`
- [Auth](https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/auth/__init__.py): auth layer · Login, JWT/OIDC, local auth, CSRF, and user provisioning · `backend/app/gateway/auth/__init__.py`
- [API Routes](https://github.com/xufei56789/deer-flow/blob/main/backend/app/gateway/routers/__init__.py): rest routers · Thread/run, artifacts, memory, skills, MCP, models, uploads, feedback, and channels endpoints · `backend/app/gateway/routers/__init__.py`
- [IM Channels](https://github.com/xufei56789/deer-flow/blob/main/backend/app/channels/__init__.py): ingress adapters · Telegram, Slack, Feishu, WeChat, WeCom, DingTalk, and Discord inbound workers · `backend/app/channels/__init__.py`

### Harness

Agent runtime, tools, state, and execution

- [Lead Agent](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/agents/lead_agent/agent.py): orchestrator · Default coordinator that plans work, delegates, and synthesizes responses · `backend/packages/harness/deerflow/agents/lead_agent/agent.py`
- [Middlewares](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/agents/middlewares/__init__.py): policy chain · Clarification, memory, loop detection, summarization, safety, token budgeting, and tool error handling · `backend/packages/harness/deerflow/agents/middlewares/__init__.py`
- [Sub-agents](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/subagents/__init__.py): delegation layer · Specialized workers such as bash and general-purpose agents · `backend/packages/harness/deerflow/subagents/__init__.py`
- [Tools](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tools/__init__.py): tooling layer · Built-in actions, ACP/skill tools, file presentation, task tools, and view-image helpers · `backend/packages/harness/deerflow/tools/__init__.py`
- [Skills](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/skills/__init__.py): capability system · Progressive skill loading, slash commands, permissions, and skill storage · `backend/packages/harness/deerflow/skills/__init__.py`
- [Sandbox](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/sandbox/__init__.py): execution boundary · Local and container-backed filesystem/runtime isolation for files, uploads, and outputs · `backend/packages/harness/deerflow/sandbox/__init__.py`
- [Runtime State](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/runtime/__init__.py): lifecycle state · Run manager, checkpointer, stream bridge, journal, and event handling · `backend/packages/harness/deerflow/runtime/__init__.py`
- [Persistence](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/persistence/__init__.py): durable store · Threads, runs, run events, users, feedback, migrations, and store providers · `backend/packages/harness/deerflow/persistence/__init__.py`
- [Model Providers](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/models/__init__.py): llm adapters · Provider factory and adapters for OpenAI, Claude, vLLM, MindIE, and patched variants · `backend/packages/harness/deerflow/models/__init__.py`
- [MCP](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/mcp/__init__.py): tool protocol · Model Context Protocol clients, cache, OAuth, session pooling, and tool bridges · `backend/packages/harness/deerflow/mcp/__init__.py`
- [Stream Bridge](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/runtime/stream_bridge/__init__.py): event streaming · 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](https://github.com/xufei56789/deer-flow/blob/main/frontend/src/app/page.tsx): next.js app · Landing pages, auth, docs, and authenticated workspace UI · `frontend/src/app/page.tsx`
- [Frontend Core](https://github.com/xufei56789/deer-flow/blob/main/frontend/src/core/api/api-client.ts): api client layer · Typed API wrappers and hooks for threads, skills, models, channels, memory, uploads, and MCP · `frontend/src/core/api/api-client.ts`
- [Workspace UI](https://github.com/xufei56789/deer-flow/blob/main/frontend/src/components/workspace/workspace-container.tsx): ui surface · Chats, artifacts, channels, settings, streaming, and thread views · `frontend/src/components/workspace/workspace-container.tsx`

### Infra

Deployment, tracing, and supporting services

- [Tracing](https://github.com/xufei56789/deer-flow/blob/main/backend/packages/harness/deerflow/tracing/__init__.py): observability · LangSmith/Langfuse integration and trace metadata propagation · `backend/packages/harness/deerflow/tracing/__init__.py`

## Connections

- 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
