---
title: "Dex Agent Framework"
description: "The Dex Agent Framework runs a durable agent without another agent SDK, with questions, durable waits, plans, steering, compaction, and MCP tools in every session."
canonical_url: "https://docs.superdurable.io/build-ai-agents/dex-agent-framework/"
source: "docs/content/agent-frameworks/dex-agent-framework.mdx"
locale: "en"
language: "python"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Dex Agent Framework

Code on this page is in Python. Other languages: [TypeScript](https://docs.superdurable.io/build-ai-agents/dex-agent-framework.typescript.md)

The Dex Agent Framework runs a durable agent without another agent SDK. Use it for a new agent: every session gets questions, durable waits, plans, steering, compaction, and MCP tools. If your agent already uses an agent SDK, see [Use Dex with Agent SDKs](https://docs.superdurable.io/build-ai-agents/use-dex-with-agent-sdks.md).

<a id="when-to-use-it"></a>

<a id="how-a-session-runs"></a>

## How a session runs

Each model call is one `RunModel` Step, and each tool call is its own Step. Dex builds every model input from the session's stored messages, so the agent needs no provider conversation API. Between turns, the session waits durably for the next user message and holds no Worker. [Step graph](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#step-graph) shows every Step and how a turn moves between them.

<a id="install"></a>

## Install

**Python**

```bash
pip install "dex-agents[litellm,mcp]"
```

Example: [agents/python/core](https://github.com/superdurable/dex/tree/main/agents/python/core)

The **litellm** extra and the **ai** package add model clients. The **mcp** extra and `@modelcontextprotocol/sdk` add MCP tools. Leave out what you do not use.

<a id="the-example"></a>

## The example

The AI Agent example, in Python and TypeScript, chats with a user and calls trusted MCP tools. It waits for approval before external writes, asks the user questions, waits on a durable Timer, and drafts plans for review.

<a id="the-session-flow"></a>

### The session Flow

The session Flow's type is **AIAgentSession**. Each session picks one of the models that the Worker allows:

**Python**

```python
def build_ai_agent_session_flow(
    models: Sequence[str],
    system_prompt: str,
    context_window_tokens: int,
    mcp_servers: Sequence[McpServerConfig],
    slack_connection: slack.Connection | None,
) -> AgentSessionFlow:
    """Builds the agent; ``models`` are the allowed model names, and the first is the default.

    ``slack_connection`` None leaves out the Slack connector tools.
    """
    clients = {model: _model_client(model) for model in models}
    mcp_tool_sources = (
        [McpToolSource(mcp_servers, resource_and_prompt_tools_enabled=True)] if mcp_servers else []
    )
    definition = AgentDefinition(
        name="ai-agent",
        instructions=system_prompt,
        model=clients[models[0]],
        # Settings hold only a model name; an unknown one gets the default, never a new client.
        resolve_model=lambda settings: clients.get(str(settings.get("model")), clients[models[0]]),
        tool_sources=[*mcp_tool_sources, *build_slack_tool_sources(slack_connection)],
        built_in_tools=frozenset(BuiltInTool),
        plan_mode_enabled=True,
        compaction=CompactionPolicy(context_window_tokens=context_window_tokens),
    )
    return stepwise_agent_session_flow(definition, flow_type=AI_AGENT_SESSION_FLOW_TYPE)
```

Example: [examples/python/dex_examples/products/ai-agent/ai_agent_session.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/products/ai-agent/ai_agent_session.py)

The allowed models, the system prompt, the context window, and the MCP servers are Worker configuration; [Model per session](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#model-per-session) explains why.

<a id="worker"></a>

### Worker

The Worker registers the session Flow with the application's other Flows. Agent session Steps are async, so the Python Registry allows async handlers.

**Python**

```python
        self.ai_agent_mcp_servers = load_mcp_servers(config.agent_mcp_config)
        self.ai_agent = build_ai_agent_session_flow(
            config.agent_models,
            config.agent_system_prompt,
            config.agent_context_tokens,
            self.ai_agent_mcp_servers,
            ai_agent_slack_connection,
        )
        # ...
        self.registry = Registry(tuple(flows), allow_async_handlers=True)
```

Example: [examples/python/dex_examples/app.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/app.py)

<a id="what-a-session-can-do"></a>

## What a session can do

<a id="durable-context"></a>

<a id="queued-messages-and-steer"></a>

<a id="questions-and-durable-waits"></a>

<a id="durable-plans"></a>

<a id="mcp-tools-and-approvals"></a>

<a id="live-events"></a>

<a id="connect-connectors"></a>

- **Durable context and compaction.** The session stores every message, and a `CompactContext` Step summarizes older ones before the model input outgrows its window ([Durable context](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#durable-context)).
- **Queued messages and Steer.** A message sent during a turn waits in a durable queue, and **Steer** applies it at the turn's next safe Step boundary ([Queued messages and Steer](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#queued-messages-and-steer)).
- **Questions and durable waits.** The `ask_user` tool waits for the user's answer, and `durable_wait` waits on a Timer, with no Worker held ([Questions and durable waits](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#questions-and-durable-waits)).
- **Durable plans.** **Plan mode** drafts a plan in the `AgentPlan` Attribute, and the user runs it with **Execute plan** ([Plans](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#durable-plans)).
- **MCP tools and approvals.** Trusted MCP servers from Worker configuration offer tools, and each call runs in its own Step under a tool policy ([MCP tools and approvals](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#mcp-tools-and-approvals)).
- **Connector tools.** Official Connector operations become tools, as for any agent SDK ([Connectors as Tools](https://docs.superdurable.io/build-ai-agents/connectors-as-tools.md?framework=built-in-agent-loop#connect-connectors)).
- **Live events.** Text, reasoning summaries, tool activity, approvals, and plan updates arrive on the session's `AgentEvents` Stream ([Read the stream](https://docs.superdurable.io/reference/agents/event-stream.md#read-the-stream)).

<a id="run-the-example"></a>

## Run it locally

Start **dexcli dev** as in [Run an example from the playground](https://docs.superdurable.io/install.md), then start one example. Both serve the page at `http://127.0.0.1:8080/products/ai-agent/`, so run one at a time.

**Python**

```bash
cd examples/python
uv sync --locked
cd ai-agent
npm ci
npm run build
cd ..
export DEX_AGENT_MCP_CONFIG="$PWD/ai-agent/mcp-servers.local.yaml"
export DEX_AGENT_SESSION_KEY="$(openssl rand -hex 32)"
uv run --frozen python main.py
```

Example: [examples/python/ai-agent](https://github.com/superdurable/dex/tree/main/examples/python/ai-agent)

The default demo model needs no credential, so you can try every feature without a key. [Example configuration](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#example-configuration) lists its slash commands and the variables for real models, and [Session access](https://docs.superdurable.io/reference/agents/dex-agent-framework.md#session-access) explains the session token. The code is under [examples/python/dex_examples/products/ai-agent](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/products/ai-agent), [examples/python/ai-agent](https://github.com/superdurable/dex/tree/main/examples/python/ai-agent), and [examples/typescript/src/products/ai-agent](https://github.com/superdurable/dex/tree/main/examples/typescript/src/products/ai-agent).

## Next

- [Production checklist](https://docs.superdurable.io/production-operations/production-checklist.md#agents): what to set up before you run sessions for real users.
- [Dex Agent Framework reference](https://docs.superdurable.io/reference/agents/dex-agent-framework.md): the Step graph, the session rules, and the limits.
- [Agents reference](https://docs.superdurable.io/reference/agents.md)
