---
title: AI SDK adapter
description: Run an agent with the Vercel AI SDK, bind its model through a profile, require approval for a tool, stream its text and test it offline.
pagination: false
sidebar:
  icon: sparkles
---

The AI SDK adapter runs agents that declare `runtime: aiSdk()`. Each model turn is one `streamText`
call of the [Vercel AI SDK](https://ai-sdk.dev). RelKit runs everything around it, as it does for
every adapter: tools go through the function engine and its access rules, approvals wait for a
person, the thread is stored in the application's `agent-state` profile, and the run streams to
clients as AG-UI events. See [Agent adapters](/docs/modules/ai/adapters) for what each adapter owns.

An application whose agents all use `aiSdk()` installs no LangChain package.

## Install the adapter

{/* Generated by apps/docs/scripts/generate.ts. Do not edit. */}

From this guide's application/package root, install only its selected requirements:

```sh
bun add @relkit/ai-sdk@0.12.2 @relkit/local@0.12.2 ai@7.0.153
```

Keep these owner-specific versions and the lockfile together. Runtime SDKs belong in `dependencies`; build/type tooling belongs in `devDependencies`. Installing an adapter does not supply its optional SDK peers or enable the feature's configuration.

## Bind a model profile

`aiSdkModel({ model })` binds a profile to a model ID. The AI SDK resolves the ID with its global
provider: the AI Gateway reads `provider/model` IDs such as `openai/gpt-5`, and an application that
sets `globalThis.AI_SDK_DEFAULT_PROVIDER` to a provider registry uses that registry's IDs instead.

```ts title="models.ts"
export const model = {
  default: aiSdkModel({ model: "openai/gpt-5" }),
};
```

Pass the object as `model` to `defineApp` in `relkit.config.ts`. An agent with `model: "default"`
then runs on `openai/gpt-5`, and `model: "default:gpt-5-mini"` keeps the provider and changes the
model name for one agent. Bind profiles of LangChain agents with `langchainModel` from
`@relkit/langchain`: each adapter reads the model IDs of its own SDK.

To use a model object instead of a profile, pass it to the adapter, as in
`aiSdk({ model: openai("gpt-5") })`. That ties the agent to the AI SDK adapter.

## Define the agent and its tool

The tool refunds an order. Its side effect is `write` and its approval is `always`, so every call
waits for a person to approve it.

```ts title="src/services/orders/tools/refund-order.tool.ts"
/** Lets the agent refund an order; every call waits for a person's approval. */
import { defineTool } from "relkit/tools";
import refundOrder from "./refund-order.function.js";

export default defineTool({
  id: "orders.refund",
  target: refundOrder,
  description: "Refund one order by its ID.",
  sideEffect: "write",
  approval: "always",
});
```

The agent selects the adapter with `runtime: aiSdk()`. Every other field is the same as under
`langchain()`.

```ts title="src/services/orders/agents/support.agent.ts"
export default defineAgent({
  id: "orders.support",
  runtime: aiSdk(),
  model: "default",
  input: z.object({ message: z.string().min(1) }),
  output: z.object({ answer: z.string() }),
  instructions: "Help with refunds. Refund an order only when the customer asks for it.",
  tools: [refundOrder],

  stateProfile: "default",
  client: { public: true },
  chat: { input: "message", output: "answer" },
  controls: ["stop", "approve"],

  limits: { maxSteps: 4, maxToolCalls: 2, timeoutMs: 30_000 },
});
```

## What a client sees

- **Text streams as the model produces it.** Each piece arrives as a `TEXT_MESSAGE_CONTENT` event
  between one `TEXT_MESSAGE_START` and one `TEXT_MESSAGE_END`.
- **An approval pauses the run.** The run's status becomes `approval-interrupted` and the tool's state
  `approval-required`. An `approve` control decides it; the tool then runs once and the agent
  continues in the same run, as described in [Approvals](/docs/modules/ai/approvals).
- **The thread continues after a restart.** Each run reads the thread's earlier messages from the
  `agent-state` profile, so a server that restarts between two runs loses nothing.
- **A `stop` control ends the run** while its model turn streams; the run ends as `cancelled`.

## Limits

- The adapter applies the `stop` and `approve` controls. A `steer` control is accepted but does not
  reach the model.
- An approval waits inside its run, so a run that waits does not survive a server restart. LangChain
  agents with a checkpointer and `interruptOn` can resume a waiting run after a restart.
- Graph workflows, DeepAgents and LangGraph persistence belong to the
  [LangChain adapter](/docs/modules/ai/native-runtime).

## Test it offline

The AI SDK's mock model answers with scripted turns. Register it under the profile's model ID as
the global provider, and the agent runs without a network or an API key. Each turn is the stream
the model would send: here a tool call, then text and the final answer.

```ts title="tests/support.agent.test.ts"
const scripted = new MockLanguageModelV4({
  doStream: [
    {
      stream: convertArrayToReadableStream([
        toolCall("call-1", "orders_refund", { orderId: "o1" }),
        finish,
      ]),
    },
    {
      stream: convertArrayToReadableStream([
        { type: "text-start", id: "t" },
        { type: "text-delta", id: "t", delta: "Order o1 " },
        { type: "text-delta", id: "t", delta: "is refunded." },
        { type: "text-end", id: "t" },
        toolCall("call-2", "relkit_output", { value: { answer: "Order o1 is refunded." } }),
        finish,
      ]),
    },
  ],
});
// Every model ID the app's profiles bind resolves to the scripted model; nothing leaves the process.
globalThis.AI_SDK_DEFAULT_PROVIDER = customProvider({
  languageModels: { "openai/gpt-5": scripted },
});
```

The final answer is a call to `relkit_output`, the tool RelKit gives every model to return the
agent's output. Reference: [AI SDK API](/docs/api/integrations/ai-sdk).
