Dify Agent Monorepo Structure#
Overview#
dify-agent is a standalone subpackage within the Dify monorepo, introduced in PR #36087. It implements Agent V2 β an async microservice model replacing the legacy synchronous BaseAgentRunner stack β and lives at dify-agent/ under the repo root .
The package runs as a FastAPI server (dify_agent.server.app:app) backed by Redis Streams for event persistence, and communicates with the Dify Plugin Daemon for LLM calls .
Source Namespaces#
The src/ directory contains four top-level packages, all installed from a single pyproject.toml :
| Namespace | Path | Role |
|---|---|---|
agenton | src/agenton/ | Core stateless Layer-graph composition framework |
agenton_collections | src/agenton_collections/ | Generic Layer implementations (Pydantic AI bridge, plain prompt/history layers) |
dify_agent | src/dify_agent/ | Dify-specific runtime: FastAPI server, Redis storage, Plugin Daemon integration |
shellctl | src/shellctl/ | Shell session client bindings |
Key subdirectories within dify_agent/#
| Subdirectory | Purpose |
|---|---|
server/ | FastAPI app factory (app.py), routes, SSE generator, ServerSettings |
runtime/ | RunScheduler, AgentRunRunner, compositor/agent factory |
layers/ | Layer implementations: dify_plugin, shell, ask_human, output |
storage/ | RedisRunStore β run records and Redis Stream events |
client/ | Sync/async HTTP client with SSE reconnect logic |
protocol/ | Public HTTP wire-protocol DTOs (CreateRunRequest, etc.) |
runtime_backend/ | Backend profile: local, enterprise, e2b, openshell selector logic |
Full directory tree with file-level descriptions:
Dependency Management (pyproject.toml + uv)#
dify-agent ships its own pyproject.toml and a checked-in uv.lock, making it independently buildable without the api/ project.
Base dependencies (always installed) :
httpx,httpx2β HTTP client transportpydantic,pydantic-ai-harness>=0.20.0,<0.21.0,pydantic-ai-slim>=2.30.0,<3.0.0β data validation, LLM agent runtime, and history compactiontyping-extensions
Optional extras :
| Extra | Key packages | Use case |
|---|---|---|
server | fastapi, uvicorn, redis, e2b, openshell, grpcio, jwcrypto, logfire, jsonschema, graphon | Production server deployment |
Dev toolchain is in the [dependency-groups] dev group : pyrefly, ruff, pytest, coverage.
Type checking is configured via [tool.pyrefly]:
project-includes = ["src", "examples"]β check source and examples onlyproject-excludes = [".venv", "tests/"]β skip testssearch-pathresolves imports fromsrc,examples/agenton, andexamples/dify_agentpython-platform = "linux",python-version = "3.12.0"
Run via make typecheck β pyrefly check with --group dev --extra server.
Install all extras for local development:
cd dify-agent && uv sync --all-extras --all-groups
Docker Image: dify-agent-backend#
The dify-agent/Dockerfile uses a three-stage multi-stage build targeting python:3.12-slim-trixie:
Stage 1 β base : Installs uv (pinned to 0.8.9) via pip. Shared by both subsequent stages.
Stage 2 β packages : Copies dify-agent/pyproject.toml, uv.lock, README.md, and src/ from the repo root build context, then runs:
uv sync --frozen --no-dev --no-editable --extra server
--frozen trusts the checked-in lock for reproducible builds; --no-editable bakes source into site-packages.
Stage 3 β production :
- Sets locale/encoding environment variables
- Creates a non-root
difyuser (uid 1001, matching thedify-apiimage convention) - Copies the virtualenv from the
packagesstage into/app/api/.venv - Pre-creates
/app/api/storage(bind-mounted at runtime) - Exposes port 5050
- Entrypoint:
uvicorn dify_agent.server.app:app --host 0.0.0.0 --port 5050
The build context is the repository root β not dify-agent/ β so paths in COPY are repo-root-relative .
CI/CD#
Four images are published in CI :
| Image | Dockerfile |
|---|---|
langgenius/dify-agent-backend | dify-agent/Dockerfile |
langgenius/dify-agent-local-sandbox | dify-agent-runtime/docker/Dockerfile |
Each is built in a parallel matrix (one job per platform: linux/amd64, linux/arm64) using Depot runners, pushed by digest, then merged into a multi-platform manifest .
Related Entry Points#
| File | Purpose |
|---|---|
| dify-agent/pyproject.toml | Package config, extras, toolchain: [tool.pyrefly] type-checker settings, uv dependency groups |
| dify-agent/Dockerfile | dify-agent-backend image definition |
src/dify_agent/server/app.py | FastAPI create_app() β top-level wiring |
src/dify_agent/server/settings.py | ServerSettings β all DIFY_AGENT_* env vars |
src/dify_agent/runtime/compositor_factory.py | create_default_layer_providers() |
src/agenton/layers/base.py | Core Layer abstraction and lifecycle |
See also: Agent V2 Architecture | Agent Runtime Backend Initialization