# Runtime
URL: /docs/concepts/runtime

The runtime holds the conversation, runs each turn against your backend, and is the only part of the chat that talks to it.

> For AI agents: a documentation index is available at [llms.txt](/llms.txt). Use `.md` for canonical markdown pages; `.mdx` is kept as a backwards-compatible alias on supported URL paths.

A runtime is the conversation's state and the engine that moves it forward. It keeps the messages, the text in the composer, the reply in progress and every earlier version of an edited message. When the user sends, the runtime calls your backend and turns the answer into a message as it streams in.

Components never call your backend. They read state from the runtime and send it actions such as send, edit, regenerate and cancel. That split is why you can restyle the whole chat without touching how it talks to the model, or swap the backend without touching a component.

```mermaid
graph LR
  A[Components] -- actions --> B[Runtime]
  B -- state --> A
  B -- request --> C[Your backend]
  C -- stream --> B
```

## One runtime per chat

You create a runtime with a hook and hand it to `AssistantRuntimeProvider`. Every component inside the provider reads from that runtime.

The smallest runtime is `useLocalRuntime`. You give it one function that calls your API with the thread's messages and returns the reply:

`app/MyRuntimeProvider.tsx`

```
"use client";

import type { ReactNode } from "react";
import {
  AssistantRuntimeProvider,
  useLocalRuntime,
  type ChatModelAdapter,
} from "@assistant-ui/react";

const MyModelAdapter: ChatModelAdapter = {
  async run({ messages, abortSignal }) {
    const result = await fetch("<YOUR_API_ENDPOINT>", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ messages }),
      signal: abortSignal,
    });
    const data = await result.json();
    return {
      content: [{ type: "text", text: data.text }],
    };
  },
};

export function MyRuntimeProvider({
  children,
}: Readonly<{ children: ReactNode }>) {
  const runtime = useLocalRuntime(MyModelAdapter);
  return (
    <AssistantRuntimeProvider runtime={runtime}>
      {children}
    </AssistantRuntimeProvider>
  );
}
```

Most apps use a runtime made for their backend instead, such as `useChatRuntime` for the Vercel AI SDK or `useLangGraphRuntime` for LangGraph. They all expose the same state to the components, so the rest of your app doesn't change when you switch. [Picking a runtime](/docs/runtimes/pick-a-runtime) lists them.

## A turn, step by step

1. The user sends from the composer. The runtime adds their [message](/docs/concepts/messages) to the thread and clears the composer.
2. The runtime starts a run. It adds an empty assistant message with the status `running` and sends your backend the messages so far, along with the system instructions and tools your app has registered.
3. Your backend streams its answer. The runtime turns each piece into a part of the assistant message: text, reasoning, a tool call. Components re-render as the parts grow.
4. The run ends. The status becomes `complete`, `incomplete` with a reason such as `cancelled` or `error`, or `requires-action` when a tool call is waiting for a result or for the user.

Try it below. The chat is a real `Thread` on `useLocalRuntime`, and you are its model adapter: when you send, the runtime calls `run()` and waits for you. Stream parts into the reply, then return or throw. The log shows each call with its arguments, everything you yield and the status the runtime gives the message. Press Stop in the composer to see the abort signal fire, or regenerate a reply to see the runtime call `run()` again with the messages before it.

Live demo: A real Thread on useLocalRuntime where you are the model adapter: send a message, stream parts into the reply, then return or throw, and watch the log of each run() call.

With `ExternalStoreRuntime`, and the framework runtimes built on it, your store or the framework does the adding and streaming, and the runtime renders the messages the store holds. The turn looks the same on screen.

Editing a message or regenerating a reply starts a new run from that point. The earlier reply is kept as a branch, so the user can switch back to it.

Your components see this through state. A stop button, for example, reads whether a run is in progress and calls `cancelRun`:

```
import { useAui, useAuiState } from "@assistant-ui/react";

function StopButton() {
  const aui = useAui();
  const isRunning = useAuiState((s) => s.thread.isRunning);
  if (!isRunning) return null;
  return <button onClick={() => aui.thread.cancelRun()}>Stop</button>;
}
```

[The Context API](/docs/concepts/context-api) covers every scope you can read this way.

## When a turn doesn't finish on its own

A run can end before the reply is finished, for three reasons.

**The user stops it.** The stop button in the registry's `Thread` is `ComposerPrimitive.Cancel`, which in the main composer calls `cancelRun` like the button above. The runtime aborts the request: `LocalRuntime` fires the `abortSignal` your adapter received, the AI SDK runtime stops its chat stream, and `ExternalStoreRuntime` calls your `onCancel`. Without `onCancel` nothing can be stopped: the registry's `Thread` shows no stop button, and a bare `ComposerPrimitive.Cancel` is disabled. Whatever already streamed stays in the thread, and the message ends `incomplete` with the reason `cancelled`. Your server sees the request close; pass that signal on to the model call, as `abortSignal: req.signal` does in an AI SDK route, so the model stops generating too.

**The backend fails.** The message keeps the parts that arrived and ends `incomplete` with the reason `error`, carrying the error on its status. Nothing shows it until your message component renders `MessagePrimitive.Error`, which renders its children only for such a message; inside it, `ErrorPrimitive.Message` prints the error. The registry's `Thread` already includes both. The text comes from your backend. An AI SDK route sends a generic "An error occurred." so server details don't leak, unless you pass `onError` to `toUIMessageStreamResponse`; with `LocalRuntime`, whatever your adapter throws becomes the error. The user retries by regenerating the reply.

**A tool call needs an answer.** The message ends `requires-action` while a [tool](/docs/tools) call waits for a result or for the user to approve it. The run picks up again once every waiting call has its answer. `LocalRuntime` continues on its own. The AI SDK runtime sends the results back only when you pass `useChatRuntime` a `sendAutomaticallyWhen` check, such as `lastAssistantMessageIsCompleteWithToolCalls` from `ai`; without it, the turn stays where it is.

## What the runtime owns, and what you own

| The runtime owns                                                                      | You own                                                                                                   |
| ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| The messages of the open thread, their parts and status                               | The backend that calls the model, and its API keys                                                        |
| The composer's text and attachments                                                   | Server-side tools and what they can reach                                                                 |
| The run in progress, and cancelling it                                                | Where threads are stored, through an [adapter](/docs/concepts/adapters) or [Assistant Cloud](/docs/cloud) |
| Branches from edits and regenerations                                                 | The components, which you can restyle or replace                                                          |
| The thread list, once you give it somewhere to keep [threads](/docs/concepts/threads) |                                                                                                           |

## Two cores: the runtime keeps the messages, or you do

Every runtime is built on one of two core runtimes. Which one sits under yours decides who holds the messages.

`LocalRuntime` keeps the messages itself. You write the call to your backend, as in the example above, and editing, regenerating and branching work without more code.

`ExternalStoreRuntime` reads the messages from a store you already have, such as Redux or Zustand, and calls your handlers when the user sends, edits or regenerates. Features turn on as you provide the handlers for them.

|                        | LocalRuntime               | ExternalStoreRuntime                        |
| ---------------------- | -------------------------- | ------------------------------------------- |
| Who holds the messages | The runtime                | Your store                                  |
| Setup                  | One `run` function         | Messages plus a handler per action          |
| Editing and branching  | Built in                   | Turned on by `onEdit` and `setMessages`     |
| Best fit               | A new app with its own API | An app whose state already lives in a store |

The runtimes for frameworks build on these. `useChatRuntime`, `useLangGraphRuntime` and the other framework runtimes sit on `ExternalStoreRuntime`, because the framework already keeps the messages. Two wire protocols sit in between for backends you write yourself: the [data stream](/docs/runtimes/custom/data-stream) protocol streams message parts on top of `LocalRuntime`, and [assistant transport](/docs/runtimes/custom/assistant-transport) streams your agent's whole state on top of `ExternalStoreRuntime`.

```mermaid
graph TD
  subgraph Framework["Framework runtimes"]
    A1[ai-sdk]
    A2[react-langgraph]
    A3[react-langchain]
    A4[react-google-adk]
    A5[react-a2a]
    A6[react-ag-ui]
    A7[react-opencode]
    A8[eve]
  end
  subgraph Protocol["Protocols"]
    P1[DataStream]
    P2[AssistantTransport]
  end
  subgraph Core["Core runtimes"]
    C1[LocalRuntime]
    C2[ExternalStoreRuntime]
  end
  A1 --> C2
  A2 --> C2
  A3 --> C2
  A4 --> C2
  A5 --> C2
  A6 --> C2
  A7 --> C2
  A8 --> C2
  P1 --> C1
  P2 --> C2
```

Start at the top and go down a layer only when the one above can't do what you need. Features such as attachments, speech and feedback plug in through [adapters](/docs/concepts/adapters), which work the same way in every runtime that supports them; the adapters page lists which runtime supports which.

## Next

- [Messages and parts](/docs/concepts/messages) — What a message is made of, and how each part reaches the screen.
- [Picking a runtime](/docs/runtimes/pick-a-runtime) — The runtime for your framework, or the path for a backend of your own.