Vercel AI SDK
Test AI SDK agents (generateText and streamText with tools) in TypeScript, with automatic capture on AI SDK 7 and a scripted language model.
Adapter status: built and tested in TypeScript against ai 7.0. AI SDK 6 works through a middleware. The AI
SDK is TypeScript only, so there is no Python counterpart.
What is captured
On AI SDK 7 the first trace() or YAML case run registers a telemetry integration with registerTelemetry. It is
global and additive, and it works across duplicate copies of ai in one install.
| AI SDK hook | Event |
|---|---|
onLanguageModelCallEnd | model_call with usage (cache reads and writes), model and response id |
onToolExecutionStart / onToolExecutionEnd | tool_call with input and output; a tool-error output is a failed call |
onStepEnd, for calls the SDK rejected before running (unknown tool, invalid input) | tool_call with error type invalid_tool_call |
Two cases need one line:
- A call that passes its own
telemetry: { integrations: [...] }replaces the global integrations for that call. AddviewportTelemetry()from@viewportai/evals/ai-sdkto the list. - AI SDK 6 has no telemetry registry. Wrap the model:
wrapLanguageModel({ model, middleware: captureMiddleware() }). The middleware records model calls; tool calls come from your tools' own capture.
Test an agent
script(...).aiSdk() is a language model that plays the script (it implements the model interface directly), so
generateText runs its real tool loop.
import { generateText, type LanguageModel, stepCountIs, tool } from "ai";
import { z } from "zod";
const tools = {
lookup_order: tool({
description: "Look up an order by id",
inputSchema: z.object({ order_id: z.string() }),
execute: async ({ order_id }) => ({ order_id, status: "delivered", total: 42.5 }),
}),
issue_refund: tool({
description: "Refund an order",
inputSchema: z.object({ order_id: z.string(), amount: z.number() }),
execute: async () => ({ refund_id: "R-1001" }),
}),
};
export async function supportAgent(model: LanguageModel, prompt: string): Promise<string> {
const { text } = await generateText({ model, tools, prompt, stopWhen: stepCountIs(5) });
return text;
}
import { lte, trace } from "@viewportai/evals";
import { reply, script, toolCall } from "@viewportai/evals/testing";
import { expect, test } from "vitest";
import { supportAgent } from "../src/agent.js";
test("looks up the order, refunds it, and stays in budget", async () => {
const model = script(
toolCall("lookup_order", { order_id: "A-1001" }),
toolCall("issue_refund", { order_id: "A-1001", amount: 42.5 }),
reply("Refund R-1001 for $42.50 is on its way."),
{ model: "claude-sonnet-4-6" },
);
const t = await trace(() => supportAgent(model.aiSdk(), "Order A-1001 arrived damaged."));
expect(t.toolNames).toEqual(["lookup_order", "issue_refund"]);
expect(t).toHaveCalledTool("issue_refund", { order_id: "A-1001", amount: lte(100) });
expect(t).not.toHaveCalledTool("delete_account");
expect(t.calls("lookup_order")[0]?.result).toEqual({ order_id: "A-1001", status: "delivered", total: 42.5 });
expect(t).toHaveModelCalls(3);
expect(t).toBeWithinBudget({ tokens: 2000, cost_usd: 0.01 });
expect(t.output).toContain("R-1001");
model.assertConsumed();
});
t.calls("lookup_order")[0].result is the tool's return value, so you can assert on what the agent saw as well as
what it asked for.
With a real model
Pass your provider model (anthropic("claude-sonnet-4-6"), openai("gpt-5")). Provider packages call the API with
fetch, which replay intercepts, so the first local run records and CI replays. See
modes and fixtures.