---
url: /development/deep-agent-runtime.md
---

# Deep-Agent runtime

How `mucgpt-core-service/app/agent` builds and runs the agent. Model profiles are registered once at startup; the agent itself is built per request in `init_agent()`.

![Deep Agent runtime](./architecture/03-deep-agent-runtime.png)

## Startup (`warmup_app()`)

* **ModelRegistry** — one chat model per `models[]` entry (see [Models](../operations/models.md)).
* **Harness profile per model** — enabled Deep Agents built-ins, summarization (80 %, keep 10) only if `enable_summarization`, TodoList (if `write_todos`), per-tool run limits from `builtin_tool_run_limits` (default: 20 calls for `read_file` and `grep`; configurable per model, `null` disables a limit), sub-agent only if task + `enable_subagents`.
* **PromptPool** — Langfuse prompts with local `.md` fallbacks (see [Prompts](../operations/prompts.md)).
* **Postgres** — `FencedPostgresSaver` and `AsyncPostgresStore`.

## Per request (`init_agent()`)

* `ToolCollection.get_tools(user)` — local + MCP tools (see [Tools & MCP](./tools-mcp.md)).
* `CompositeBackend` — state files plus the read-only `/documents/` mount (see [Virtual file system](./virtual-file-system.md)).
* Checkpointer only if `persist` (otherwise approval tools are removed).
* `MUCGPTAgent` → `create_deep_agent(...)` with the MUCGPT middleware chain: Context → ToolStatus → ToolError → TokenUsage, plus Deep Agents defaults and harness-profile extras. State is `DefaultAgentState`, context is `RequestContext`, and `interrupt_on` covers tools that require approval.

## Agent loop (one run)

1. START: messages (+ `data_sources`).
2. Model call: `ContextMiddleware` injects enabled tools, date, `data_sources` and the prompt; `ToolStatusMiddleware` adds the `status_message` argument; `TokenUsageMiddleware` records usage.
3. Tool execution (local · MCP · built-ins): `ToolStatusMiddleware` strips `status_message` again, `ToolErrorMiddleware` maps exceptions to tool messages.
4. Loop back to the model until no tool calls remain → END with the final answer.
5. A tool call that needs approval raises an approval interrupt before the tool runs: the run pauses, the user decides, the run resumes.
6. With `persist`, a checkpoint is written after every step (`thread_id` = conversation id).
