Vercel AI SDK
Send OpenTelemetry GenAI spans from the AI SDK with its OpenTelemetry integration, then add an owner and approvals with telumin-otel.
TypeScript
Package: @ai-sdk/otel (tested with 1.0.177)
- Upstream docs: AI SDK telemetry
- Upstream docs: @ai-sdk/otel (npm)
bash
npm install --save-exact ai@7.0.177 @ai-sdk/otel@1.0.177 @opentelemetry/api@1.9.1 @opentelemetry/sdk-trace-node@2.12.0Setup (from examples/instrumentation/ts-vercel-ai)
import { trace } from "@opentelemetry/api"
import { resourceFromAttributes } from "@opentelemetry/resources"
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base"
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"
import { OpenTelemetry } from "@ai-sdk/otel"
import { ownerResource, recordApproval } from "@telumin/otel"
import { generateText, registerTelemetry, stepCountIs, tool } from "ai"
import { z } from "zod"
const AGENT_NAME = "refund-agent"
// 1. Tracer provider: the owner goes on the resource (C2), spans go to a file.
// register() also installs the async context manager, so the tool runs inside its span.
const provider = new NodeTracerProvider({
resource: resourceFromAttributes({ "service.name": "refund-service", ...ownerResource("payments-team") }),
// This example writes a file; in production use BatchSpanProcessor(new OTLPTraceExporter()).
spanProcessors: [new SimpleSpanProcessor(new OtlpJsonFileExporter(process.argv[2]))],
})
provider.register()
// 2. AI SDK telemetry. The agent name comes from telemetry.functionId; the AI SDK has no
// agent id or version, so add yours to the invoke_agent span with enrichSpan (C1).
registerTelemetry(
new OpenTelemetry({
enrichSpan: ({ spanType }) =>
spanType === "operation" ? { "gen_ai.agent.id": "refund-agent-v1", "gen_ai.agent.version": "1.0.0" } : undefined,
}),
)Record the approval in the tool (from examples/instrumentation/ts-vercel-ai)
function askHuman(amount: number): { decision: "approved" | "rejected"; reason: string } {
// Stand-in for your approval step (a review queue, a chat button, ...).
return amount <= 100 ? { decision: "approved", reason: "refund under the team limit" } : { decision: "rejected", reason: "over the limit" }
}
const issueRefund = tool({
description: "Refund an order. A person must approve every refund.",
inputSchema: z.object({ orderId: z.string(), amount: z.number() }),
execute: async ({ orderId, amount }, { toolCallId }) => {
const { decision, reason } = askHuman(amount)
// The tool runs inside its execute_tool span, so the decision lands on that span (C4).
const span = trace.getActiveSpan()
if (span) recordApproval(span, { decision, approver: "alice@example.com", reason, toolCallId })
return `refund ${decision} for ${orderId}`
},
})Run (from examples/instrumentation/ts-vercel-ai)
const result = await generateText({
model,
prompt: "Please refund order A-1001, 40 EUR.",
tools: { issueRefund },
stopWhen: stepCountIs(3),
// No prompt or response content is recorded.
telemetry: { functionId: AGENT_NAME, recordInputs: false, recordOutputs: false },
})- telemetry.functionId becomes gen_ai.agent.name on the invoke_agent span.
- recordInputs: false and recordOutputs: false keep prompts and responses out of the spans.
- Requires Node 22 or later (the AI SDK's own requirement).
Runnable example, tested in CI
examples/instrumentation/ts-vercel-ai runs offline with a mocked model, writes OTLP JSON, and goes through the check on every change. Result: C1 green, C2 green, C3 green, C4 green (one approval recorded with recordApproval).
Owner and approvals
No instrumentation package records who owns an agent or who approved a tool call. Add both with telumin-otel, as the examples do.
Check the result
To check the result, export the spans as OTLP JSON (or send them through the collector tap) and drop the file on the free check. The check runs in your browser tab.