Skip to main content

Dex Agent Framework

View as Markdown

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.

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 shows every Step and how a turn moves between them.

Install

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

Example: 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.

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.

The session Flow

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

ai_agent_session.pyPython
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

The allowed models, the system prompt, the context window, and the MCP servers are Worker configuration; Model per session explains why.

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.

app.pyPython
        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

What a session can do

  • 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).
  • 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).
  • 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).
  • Durable plans. Plan mode drafts a plan in the AgentPlan Attribute, and the user runs it with Execute plan (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).
  • Connector tools. Official Connector operations become tools, as for any agent SDK (Connectors as Tools).
  • Live events. Text, reasoning summaries, tool activity, approvals, and plan updates arrive on the session's AgentEvents Stream (Read the stream).

Run it locally

Start dexcli dev as in Run an example from the playground, then start one example. Both serve the page at http://127.0.0.1:8080/products/ai-agent/, so run one at a time.

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

The default demo model needs no credential, so you can try every feature without a key. Example configuration lists its slash commands and the variables for real models, and Session access explains the session token. The code is under examples/python/dex_examples/products/ai-agent, examples/python/ai-agent, and examples/typescript/src/products/ai-agent.

Next