跳到主要内容

Agent SDK adapters

查看 Markdown

Dex runs agents built with nine agent SDKs, plus the Dex Agent Framework. Every one of them runs on the same session Flow through its own adapter. This page lists the session features of each adapter, how it runs a turn as Steps, its limits, and what a turn costs. Use Dex with Agent SDKs explains the session Flow that they share. The rules for sessions on the Dex Agent Framework, such as plans, durable waits, and MCP servers, are in the Dex Agent Framework reference.

Sections that moved to other pages

Features by harness

Every harness gets a durable session, tool policies, approvals, Connectors as tools, queued messages, and live events on the AgentEvents Stream. Every one that runs in Python or TypeScript also gets Steer, cancel_turn, and delete_queued_message. Eino runs on the Go core, which has none of them.

FrameworkLanguagesEach tool call in its own StepContext compaction by DexQuestions and plans
Claude Agent SDKPython, TypeScriptWrites, reads that need approval, and Connector calls. Other reads run inside the model call's Step.No. Claude Code compacts its own transcript.No
EinoGoYes. The tools of an agent tool's sub-agent run inside that tool's Step.No. The Go core has no compaction.No
Google ADKPythonYesYesNo
LangGraphPythonYes. Provider-run tools run at the provider.YesNo
MastraTypeScriptYes, except tools that Mastra runs itselfYesNo
OpenAI Agents SDKPython, TypeScriptFunction tools. Hosted tools run at the provider.YesNo
PiTypeScriptYesYesYes
Pydantic AIPythonYes. Provider-run tools run at the provider.YesNo
Vercel AI SDKTypeScriptYes. Provider-executed tools run at the provider.YesNo
Dex Agent FrameworkPython, TypeScriptYesYesYes

A tool call in its own Step runs again after a crash only by the rules of What runs again: Dex can retry a read or an idempotent write with the same key, and gives any other write one attempt per call. A new call from the model can still repeat an effect. A tool that runs inside the model call's Step, or at the provider, runs again when that Step retries.

Your agent SDK's terms in Dex

In your agent SDKIn DexWhat changes for you
Thread, conversation, sessionA session: one Flow runIt survives crashes and deploys.
Run the agentThe send_user_message RPC from a Client (sendUserMessage in TypeScript)Dex saves the message first. Messages sent during a turn queue.
TurnA turn: the Steps from one user message to the replyIn Python and TypeScript, Steer and Stop end the active turn early. That section says what happens to running tools and remote tasks.
LLM callA RunModel Step, with one or more model callsDex saves the Step's result.
Tool call, function callBy default, its own Step: CallTool for a read or an idempotent write, CallSingleAttemptTool for any other writeThe tool policy picks the Step. Features by harness lists the tools an agent SDK runs inside RunModel or at the provider.
Parallel tool callsTool Steps that run in parallel, then a ResumeTurn Step. Connector writes of one response run in order.ResumeTurn waits for every result and gives them to the model.
Human in the loop, tool confirmationAn AwaitToolApproval Step, answered with the resolve_interrupt RPC (resolveInterrupt in TypeScript) or in Dex WebThe wait can last days and holds no Worker.
Checkpointer, durable execution pluginSaved Step results. The adapter keeps the agent SDK's own state between Steps.The agent code needs no determinism rules.
Chat history, short-term memoryThe session's AttributesA session keeps one conversation. A new session starts empty.
Streaming callbacksThe AgentEvents StreamIt is best effort. get_agent_snapshot (getAgentSnapshot in TypeScript) reads the saved conversation.
MCP servers, tool integrationsTool sources. Official Connectors come in through ConnectorToolSource.Each Connector call gets its credentials and an idempotency key.

The session ID is the Flow ID. Between turns the session waits for the next user message and holds no Worker task slot. A stopped session stays readable for the retention period.

Steer and Stop

These rules apply to every Python and TypeScript harness, including the Dex Agent Framework. The Go core has no Steer or cancel_turn.

  • Steer (steer_user_message, steerUserMessage in TypeScript) applies a message at the turn's next safe Step boundary. It never interrupts a model call or a tool call that has started. Tool calls that have not started, and approvals, questions, and durable waits that are still waiting, record REJECTED with the cancellation reason STEERED. The turn then ends, and a new turn starts with every steered message, in order. Steer takes new text or the message_id of a queued message. While no turn runs, steered text queues like send_user_message, and a queued message stays queued. If another Step takes that queued message first, the RPC raises ChannelMessageNotFoundError and changes nothing; reload the snapshot. While a remote tool task runs, Steer waits for it.
  • Stop (cancel_turn, cancelTurn in TypeScript) ends the turn with no new turn. It cancels a running model call or compaction Step at once and drops its uncommitted answer. Whether the provider request itself stops depends on the provider client's cancellation support. It cancels the Steps that wait for an approval, an answer, a durable_wait Timer, or a remote task's next poll. A tool call Step that has started runs to the end, and the turn ends after it. Calls that have not started record REJECTED with the cancellation reason TURN_CANCELLED. A started remote task is abandoned, and its call records outcome_unknown; a runner that implements ToolTaskCanceller is asked to cancel the task. A model call that already saved its final answer completes the turn. cancel_turn takes an optional turn ID; pass the same one when you retry. A second call for the same turn changes nothing. While no turn is active, cancel_turn returns false and changes nothing.
  • A turn that a Worker without steering started or advanced cannot be steered or stopped.

How your framework runs a turn

Most adapters stop the framework before a tool runs, so each tool call runs in its own Step, and most run in parallel (Connector writes of one response run in order): CallTool for a read or an idempotent write, CallSingleAttemptTool for any other write. A call that needs approval waits in AwaitToolApproval first. The next RunModel gives the framework the recorded results, so it does not run those calls again. A turn that looks up an order runs RunModel, CallTool, ResumeTurn, and RunModel. The Claude Agent SDK is the exception: it runs reads that need no approval inside RunModel. With Pi, Dex runs the loop itself, so every Pi tool call runs in its own Step.

The tabs show how each framework is stopped and resumed, and where a framework differs.

The Claude Agent SDK runs Claude Code as an agent. Its agent loop runs in the Claude Code CLI, a subprocess of the Worker. The Dex adapter runs that loop inside a durable session. It is available for Python and TypeScript.

When to use it

Use it when you want the Claude Code agent loop, its context management, and its tools. Use it when your agent already uses the Claude Agent SDK.

Use the Dex Agent Framework when you want one model call per Step, parallel tool Steps, or context compaction by Dex.

How a session runs

Claude Code runs a whole turn, so the adapter splits a turn into segments. One RunModel Step runs the CLI for one segment. Model calls run inside the segment. So do read tools that need no approval: a deferred read would restart the CLI. A read that needs approval leaves the segment like a write. A Connector tool call is the exception: it always leaves the segment, even a read. See Connect Connectors.

A write tool call ends the segment. It waits in AwaitToolApproval when it needs approval. Then it runs in its own Step: CallTool for an idempotent write, CallSingleAttemptTool for any other write. ResumeTurn then returns to RunModel, which resumes the same Claude session. The CLI gets the recorded result and does not run that call again.

A failed segment retries within its attempts and reruns whole. Model calls and reads inside it run again. A write that ran in its own Step does not run again when the segment retries.

A turn that looks up an order runs one RunModel Step. A turn that cancels an order runs RunModel, AwaitToolApproval, CallSingleAttemptTool, ResumeTurn, and RunModel.

Limits

Each tab lists the limits of one adapter that change how you write an agent, with what to do instead. The Dex Agent Framework has its own limits.

  • One write call per segment leaves the turn. A reply with two writes costs one more segment and model call. The writes of one reply never run as parallel Steps.
  • Tools that run inside the CLI, such as Bash or Edit, are at least once. They run again when a segment reruns. Files the CLI changes are not recorded.
  • A segment must finish within the RunModel method timeout, 15 minutes by default. Raise it with run_model_limit in Python or runModelLimit in TypeScript.
  • Claude Code compacts its own transcript, and context compaction by Dex stays off. A long session ends at the state limit, not the context window: the active state of one session, 64 MiB by default (Session limits).
  • A recorded tool result keeps text only.
  • A write call inside a subagent is denied. The model is asked to call it from the main conversation.
  • The Claude Code CLI inherits the Worker's environment, including any other Anthropic or Claude Code sign-in setting. Set the options' env so that ANTHROPIC_API_KEY is its only credential, as the examples' build_claude_cli_environment does.

Limits for long sessions has the server limits.

Latency and cost

Every session Step is SYNC: Dex saves its start before a Worker gets it, and its result before the session moves on. Take a turn whose model asks for three ordinary tools without approval, in one response, and then answers, with one model call in each RunModel. The turn runs two RunModel Steps, three tool Steps in parallel, and one ResumeTurn, so it waits on four Steps in a row. Performance measured a p50 of about 105 ms for a SYNC Step on a 2-vCPU server at half of its peak load. At that p50, the four Steps add about 0.4 seconds to the turn, on top of the model and tool time.

To retry an expensive model call fewer times, lower its attempts; Step timeouts lists the option and the defaults.

The complete files

The quick start program is three files per language. Apart from the example's order desk and settings, they use only Dex, Dex Agents, and your agent SDK:

FilePythonTypeScriptGo
Your agent module, plain and durable, with every importclaude_agent_sdk · google_adk · langchain · openai_agents · pydantic_aiclaude-agent-sdk · mastra · openai-agents · pi · vercel-aieino
The runner: Registry, Worker, Client, and one turn with approvalssession.pyquick-start-session.tssession.go
The smallest host: one Worker, one Client, one turnembed.pyquick-start-embed.tsembed.go
The entry point that asks for approval at the prompt__main__.pyquick-start-main.tsmain.go

The other packages that the install commands list are your agent SDK and the packages the example uses, such as its model client; install the ones your agent uses. Tools the agent SDK runs itself, such as MCP servers and hosted tools, need the changes in its Limits tab. The Order Support host registers the Claude agent only when ANTHROPIC_API_KEY is set, and serves the other harnesses without it.

Serve sessions from a web app

A web app splits the host across routes:

  1. The Registry, Worker, and Client. The Worker connects out to Dex, so it needs no open port. Both use a blob cache, a local cache for large values.
  2. A route that starts a session. The session ID is the Flow ID.
  3. A route that sends a user message, and one that reads the session snapshot with the agent's reply or streams it.
  4. A route that approves or rejects a waiting tool call; see Approve from your application.

The handler that sends a message queues it on the session and returns. It replaces the agent SDK call that waited for the reply:

Agent SDKCall it replaces
Claude Agent SDKquery
EinoRunner.Query
Google ADKRunner.run_async
LangGraphagent.invoke
Mastraagent.generate
OpenAI Agents SDKRunner.run
Piagent.prompt
Pydantic AIagent.run
Vercel AI SDKgenerateText

The reply arrives on the session's AgentEvents Stream, which a chat UI reads as server-sent events. Polling the snapshot suits scripts and tests. The Order Support Agent example has these routes over HTTP, for every agent SDK.

The whole module, quick_start/web.py, is a complete app: it hosts the Worker and the Client and serves every route a chat UI needs.

web.pyPython
"""A complete web app for a durable agent: start, message, read, stream, approve.

It hosts the Worker and the Client in the web process. Add your own
authentication before you expose these routes: anyone who knows a session ID
can read and change that session here.
"""

from __future__ import annotations

import uuid
from pathlib import Path
from typing import Any

from dex_agents import AgentSessionFlow
from dex_agents.ui_stream import (
AGENT_EVENT_STREAM_HEADERS,
agent_snapshot_to_json,
stream_agent_event_frames,
)
from quart import Quart, Response, jsonify, make_response, request

from agent_harness_examples.quick_start.safe_send import send_user_message_once
from agent_harness_examples.quick_start.session import QuickStartApp

def create_web_app(
session_flow: AgentSessionFlow, server_address: str, blob_cache_dir: Path
) -> Quart:
"""Returns a Quart app whose routes run one durable agent's sessions."""
web = Quart(__name__)
apps: list[QuickStartApp] = []

send_user_message_once retries a send with best-effort duplicate detection; Retry a message send has the module and its limits.

例子: examples/python/agent-harnesses/agent_harness_examples/quick_start/web.py