The agent backend

Connect an AG-UI backend that reads the screen's context, uses its current tools and returns app tool calls to Tandem.

Tandem works with an AG-UI backend that follows the contract below. The reference agents implement it with Google ADK, LangGraph and Pydantic AI. Their tests exercise the AG-UI exchange with scripted responses.

Backend contract

  1. Put the run's AG-UI context into the model's prompt. It contains the values listed with .sees. The model needs this context to read the screen. None of the three reference frameworks adds it to the prompt automatically.
  2. Give the model the app tools from the current run. Discard app tool definitions from earlier runs. Tandem always sends tools, including an empty list when no tools are available. Your backend may also expose its own tools.
  3. Return app tool calls as TOOL_CALL_START, TOOL_CALL_ARGS and TOOL_CALL_END events with matching call IDs. Arguments may arrive in several fragments. Tandem executes the calls after the run finishes, subject to the screen's current capabilities and approvals.
  4. Accept each app tool result as a tool message in the next run. Match it to the original call by toolCallId. A result can report execution, refusal, rejection, cancellation or invalidation.
  5. Read the conversation from each run's messages, or keep a server-side history and deduplicate incoming messages by ID. Preserve the calls and results of your backend's own tools as well as the app's calls.
  6. Repeat the input's threadId and runId exactly in RUN_STARTED. Tandem skips a run with other IDs as a replay. When the response ends without the requested run, the chat shows a lost connection and Tandem reports a FormatException that says so.

The wire rules define the request body, message order and call IDs. Tool answers lists the result formats, including failures.

Tools, their descriptions and context are client input. Treat them as untrusted. A caller can replace them, so a client-supplied tool list cannot authorize access to the backend's data or tools.

For a web app, configure CORS to allow its origin. The reference agents have no CORS middleware, so browser builds need that addition.

Threads with an app-supplied ID

When the app passes TandemThread(id: ThreadId(conversationId)), every run uses that ID, and a reset keeps it. A reset drops the history from later runs. After session.startOver() or reopening on a changed screen, the next run carries the calls whose answers no run has sent yet, with those answers, and then the new message. After any other reset, it carries only the new message.

  • Check that the user may use that ID.
  • Decide what happens when two clients run on the thread at the same time.
  • Reconcile work whose delivery or result is uncertain after a run that breaks off, a run Tandem could not follow, or a screen that closes during a run or an approved method. The thread can then hold a call to an app tool without an answer. Accept the next run on that thread anyway. Otherwise every later run on that ID fails, and the app's way out is a new TandemThread with a new ID.

Authentication and connection lifetime

To send the user's token, give HttpAgentConnection an http.Client that adds it to requests. Pass that connection to TandemScope.connected and keep it in a field. Creating a new connection on each build replaces the session.

A sandboxed macOS app needs the com.apple.security.network.client entitlement to connect to the backend.

Another transport

For another transport, such as a WebSocket, implement AgentConnection. In run, pass agUiRun(input) only the decoded frames of this run's request, and end the stream when the transport says the request is over. Route frames by a per-request ID where the transport has one. Routing only by thread or socket mixes an ID-less RUN_ERROR or terminal event of an earlier run into the next one, and AG-UI allows a RUN_ERROR even after RUN_FINISHED. Give agUiRun the input with the IDs the server will echo, rewritten if the transport rewrites them, and deduplicate a resumed or reattached stream first.

agUiRun skips replayed runs and expands chunk events. If a delta depends on a snapshot from replay, send a STATE_SNAPSHOT inside the requested run first. Once the requested run started, another RUN_STARTED or an event that names another thread fails the stream with a FormatException, because AG-UI does not start a run while one is open. Tandem ends the stream at the first RUN_FINISHED or RUN_ERROR it passes through, even though AG-UI allows a later error.

The connection handles timeouts and cleans up that request when the session cancels the stream, as when the session is disposed during a run. Stop does not cancel the stream: the session reads the stopped run to its end. HttpAgentConnection fails a run whose transport goes quiet for 60 seconds.

A connection whose transport can cancel a run, such as a WebSocket protocol with a cancel message, can implement StoppableAgentConnection. On session.stop(), unless the server ended the run already, the session calls its stop once with the run's input. It still reads the run until the server ends it with RUN_FINISHED and the outcome cancelled, or with RUN_ERROR. Both end the stopped run quietly, and the session still reports the code and message of a RUN_ERROR. After a stop that threw, a RUN_ERROR fails the run. A stream that ends without either counts as a lost connection, so a transport that can only close should not implement it.

Framework adapters

The following requirements belong to the reference adapters. The AG-UI exchange above is the contract for other backends too.

Google ADK

The Google ADK agent uses ag-ui-adk.

  • ag-ui-adk stores AG-UI context in run metadata. The instruction provider adds it to the instructions for every model call.
  • ag-ui-adk drops a tool call if an earlier chunk of the same run called the same tool. Gemini sends parallel calls in separate chunks, so the streaming wrapper collects the model's whole turn and emits its calls in one chunk. LiteLLM already sends Ollama's calls in one chunk.
  • Each thread has an in-memory session. After a restart, the adapter restores the conversation from the app's messages. If the session is lost while app tool results are in flight, the adapter may repeat the turn from the user's message.
  • The example pins LiteLLM to 1.105. Version 1.106 fails tool calls unless the mcp package is installed.

LangGraph

The LangGraph agent uses ag-ui-langgraph.

  • ag-ui-langgraph stores the current context and tools under ag-ui in the graph's state. The agent node copies the context into the system prompt and binds the tools to the model.
  • A top-level tools state key accumulates tools from earlier runs. The reference graph omits that key to keep the model's app tools current.
  • The graph needs a checkpointer. The adapter reads the thread from it on every run and deduplicates incoming messages. The example uses InMemorySaver, which keeps threads in memory without expiry.
  • This adapter requires the model to stream tool calls. It reads only the first call of each chunk and expects arguments in later chunks. ChatOllama and Gemini can emit complete calls together, so the streaming wrapper splits them into the chunks the adapter expects.
  • The graph executes backend tools first. If the model also requested app tools, the graph then ends the run so Tandem can handle them. A graph interrupt() is not supported by Tandem.

Pydantic AI

The Pydantic AI agent uses its built-in AG-UI adapter.

  • Pydantic AI leaves client-supplied AG-UI context out of the prompt. The endpoint passes it as deps, and the agent's instructions add it to the prompt.
  • The reference agent stores no server-side conversation. Each run's messages contain the history it needs. App tool calls are deferred calls whose results arrive in the next run.
  • For Ollama, the example uses openai_reasoning_effort="none" to disable thinking. ModelSettings(thinking=False) does not reach Ollama through this adapter.
  • Pydantic AI 2.61 uses ag-ui-protocol 0.1, before AG-UI 1.0.