The Dify Agent Runtime is a standalone, FastAPI-based service designed for the high-performance execution of autonomous agents. Built on the Agenton framework, it provides a structured environment for Pydantic AI execution, bridging the gap between Dify's orchestration studio and the iterative reasoning loops required by modern large language models (LLMs).
The runtime handles complex multi-step reasoning, tool invocation, and stateful session management. It is decoupled from the main Flask-based Dify API, allowing it to scale independently and leverage asynchronous Python patterns optimized for long-running agentic tasks.
At the heart of the runtime is the concept of an Agent Soul Configuration, which defines an agent’s identity including model choice, prompts, tools, and knowledge sources. The runtime executes these configurations using the Agenton compositor framework, which manages layered nodes and providers to execute the agent plan.
This system orchestrates:
The following diagram contextualizes the Agent Runtime service within the broader Dify ecosystem, mapping natural language system concepts to the corresponding code entities and services:
Mapping:
AgentRosterService manages agent soul config objects.FastAPI Entrypoint handles REST calls in the agent runtime.Agenton Compositor composes and executes agent plans.Sources: api/services/agent/roster_service.py1-100 api/models/agent.py1-50 api/controllers/agent/runtime.py1-80 dify-agent/on/compositor.py1-100 dify-agent/on/layers/layer_node.py1-70 api/pyproject.toml12-48 docker/docker-compose.yaml221-240
| Component | Code Entity / File Reference | Description |
|---|---|---|
| Server | FastAPI (api/controllers/agent/runtime.py) | Provides REST API endpoints to run agent execution requests. |
| Compositor Framework | Agenton (dify-agent/on/compositor.py) | Core framework managing agent execution graph and layering. |
| Agent Configuration | AgentSoulConfig (api/models/agent.py) | Defines AI model, prompts, tools, and knowledge for agent. |
| Layer Architecture | LayerNode / LayerProvider (dify-agent/on/layers) | Modular execution units composing reasoning and tool calls. |
| Tool Invocation | Tool Engine (api/services/tool/*.py) | Manages invocation of built-in and API-based tools. |
| Code Execution Sandbox | Dify Sandbox service in Docker (sandbox container) | Isolated environment for secure code execution requests. |
| AI Runtime | Pydantic AI concepts | Typed and structured AI execution integrated with Agenton. |
Sources: api/pyproject.toml12-48 api/models/agent.py10-40 dify-agent/on/compositor.py1-100 api/services/tool/tool_manager.py1-80 docker/docker-compose.middleware.yaml105-130
The Dify Agent Runtime operates as a distinctly scalable execution layer for autonomous agents within the overall Dify platform. It integrates deeply with backend data stores, tool systems, and vector knowledge retrieval services.
ToolManager system.Sources: api/services/tool/tool_manager.py1-80 docker/.env.example164-190 docker/docker-compose.yaml70-80
Below is the typical execution flow from a Dify Console or App invoking the Agent Runtime through its FastAPI endpoint, showing the interaction of key components and eventual client response streaming:
Key features here include session snapshot management, streaming back agent thoughts and tool calls, and structured Pydantic output enforcing schema correctness.
Sources: api/configs/feature/__init__.py80-100 api/controllers/agent/runtime.py10-70 docker/docker-compose.yaml221-240
For detailed technical insights into specific parts of the Dify Agent Runtime, consult these child pages:
This page covers:
LayerNode and LayerProvider abstractions,Topics include:
/run endpoint),Focuses on:
dify-agent-runtime HTTP server using shellctl,This parent page outlines the overall architecture, roles, and relations of the Dify Agent Runtime within the Dify platform. For deep dives into its framework, server API, and system-level sandbox runtime, visit the linked child pages.
Sources:
api/pyproject.toml1-51
docker/docker-compose.yaml221-240
api/configs/feature/__init__.py80-101
api/controllers/agent/runtime.py1-80
dify-agent/on/compositor.py1-100
dify-agent/on/layers/layer_node.py1-70
api/services/agent/roster_service.py1-100
api/models/agent.py1-50
api/services/tool/tool_manager.py1-80
docker/.env.example164-190
docker/docker-compose.middleware.yaml105-130
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.