AI SDK adapter
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.
The AI SDK adapter runs agents that declare runtime: aiSdk(). Each model turn is one streamText
call of the Vercel AI SDK. 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 for what each adapter owns.
An application whose agents all use aiSdk() installs no LangChain package.
Install the adapter
From this guide’s application/package root, install only its selected requirements:
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.
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.
/** 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().
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_CONTENTevent between oneTEXT_MESSAGE_STARTand oneTEXT_MESSAGE_END. - An approval pauses the run. The run’s status becomes
approval-interruptedand the tool’s stateapproval-required. Anapprovecontrol decides it; the tool then runs once and the agent continues in the same run, as described in Approvals. - The thread continues after a restart. Each run reads the thread’s earlier messages from the
agent-stateprofile, so a server that restarts between two runs loses nothing. - A
stopcontrol ends the run while its model turn streams; the run ends ascancelled.
Limits
- The adapter applies the
stopandapprovecontrols. Asteercontrol 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
interruptOncan resume a waiting run after a restart. - Graph workflows, DeepAgents and LangGraph persistence belong to the LangChain adapter.
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.
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.