Vercel AI SDK tracing
Braintrust traces Vercel AI SDK text generation, object generation, embeddings, reranking, streaming, tool calls, and agent calls from the Vercel AI SDK.Setup
Install the Braintrust SDK alongside the Vercel AI SDK. The Braintrust SDK supports Vercel AI SDK v3, v4, v5, v6, and v7.Starting with Braintrust SDK v2.0.0,
zod is a peer dependency and must be installed separately when using the Vercel AI SDK integration.Auto-instrumentation
To trace Vercel AI SDK calls without wrapping individual functions, initialize Braintrust normally, then run your app with Braintrust’s import hook. The hook patches AI SDK calls at runtime, including AI SDK v7 telemetry dispatchers.1
Initialize Braintrust and call the AI SDK
trace-vercel-ai-sdk-auto.js
2
Run with the import hook
node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.If you’re using a bundler, see Trace LLM calls for plugin and loader setup.
wrapNextjsConfigWithBraintrust instead of using the Node.js import hook directly. Braintrust uses the wrapper to apply the right instrumentation for the active Next.js build, including webpack and Turbopack builds.next.config.mjs
Manual instrumentation (telemetry registration)
For AI SDK v7, callai.registerTelemetry(braintrustAISDKTelemetry()) once at startup before making AI SDK calls. Use this path when you cannot run with the import hook or need to instrument a specific call path yourself.trace-vercel-ai-sdk-v7.ts
For AI SDK v7, define tool schemas with
inputSchema. Braintrust traces requests, streams, and tool calls, but provider-level cache metrics and deny-output overrides from the earlier provider-wrapping integration are not available through v7 telemetry.Manual instrumentation (wrap functions)
For AI SDK v3 to v6, usewrapAISDK to wrap the Vercel AI SDK functions when you cannot run with the import hook or need to instrument a specific call path yourself.trace-vercel-ai-sdk.ts
Trace tool calls
To trace tool calls, add tools to the same instrumentedgenerateText call from the setup above. Braintrust records the tool definitions in span metadata and traces tool execution as child spans.TypeScript
Stream tool responses
To trace streaming tool responses, pass your tools tostreamText. Braintrust creates doStream child spans for streaming LLM calls.TypeScript
Add metadata
To attach custom metadata to your AI SDK traces, wrap your AI calls in a parent span usingtraced. Auto-instrumentation and wrapAISDK create child spans for AI SDK calls, and you attach your metadata to the parent span.TypeScript
- Add custom metadata like user IDs, session IDs, or feature flags.
- Compute metadata based on async operations, such as fetching user context.
- Add metadata conditionally based on the response.
- Group multiple AI calls under a single parent span with shared metadata.
If you’re using the Vercel AI SDK with OpenTelemetry (not
wrapAISDK), you can use the native experimental_telemetry.metadata parameter instead. See the OpenTelemetry integration guide for details.Multi-round tool interactions
When using tools, the AI SDK often makes multiple LLM calls to complete the task. Braintrust automatically creates nested spans to give you visibility into each step:- Parent span:
generateText,streamText,generateObject, orstreamObjectrepresents the overall operation. - Child spans:
doGenerateordoStreamrepresent each individual LLM call during the operation. - Tool spans: Tool executions appear as separate spans showing inputs and outputs.
- How many LLM calls were needed to complete the task
- What each LLM call received and returned
- The complete flow of tool calls and responses
generateText call that uses tools might produce this span hierarchy:generateText(parent)doGenerate(first LLM call, decides to use tool)multiply(tool execution)doGenerate(second LLM call, uses tool result to form response)
Trace agents
The AI SDK’s Agent classes (Agent, Experimental_Agent, ToolLoopAgent, and WorkflowAgent) are automatically traced when you run with the import hook. They are also wrapped and traced when using wrapAISDK.When using AI SDK v5 or v6, individual tool calls within agentic loops are captured as separate child spans with
span_type: "function", giving you full visibility into which tools were called, in what order, and with what inputs and outputs. This granular tool call tracing is specific to AI SDK v5 and v6 and is not available in v3 or v4.TypeScript
Configure for serverless
Braintrust automatically uses Vercel’swaitUntil functionality when available, allowing you to use asyncFlush: true (the default) without blocking requests.The SDK automatically detects Vercel’s environment and uses waitUntil to flush logs in the background. No special configuration is needed for Vercel deployments; use initLogger as shown in the examples above.If you’re using a serverless environment without
waitUntil support (like AWS Lambda), set asyncFlush: false to ensure logs are flushed before the function terminates. See Advanced tracing patterns for more details.What Braintrust traces
Braintrust captures the main AI SDK operation, provider calls, and nested tool or agent work as a span hierarchy:- AI SDK operation spans (
generateText,streamText,generateObject, andstreamObject), with prompt-like inputs, final outputs, provider and model metadata, and operation metadata. - Provider call spans (
doGenerateanddoStream), with model-call inputs, provider outputs, token metrics, and time-to-first-token for streaming calls. - Embedding spans (
embed,embedMany, anddoEmbed), with input values, embedding counts or dimensions, and token metrics when the provider reports them. - Rerank spans (
rerankanddoRerank), with query, documents, top-N metadata, and ranked outputs. - Tool execution spans (
<tool name>), with tool inputs, outputs, and errors. - Agent spans (
Agent.generate,Agent.stream,ToolLoopAgent.generate,ToolLoopAgent.stream, andWorkflowAgent.stream), with agent inputs, outputs, model-call child spans, and time-to-first-token for streaming agent calls.
Vercel AI SDK tracing resources
Vercel Marketplace tracing
The Vercel Marketplace integration collects traces for AI calls in your Vercel applications with minimal setup. No package installation required.Setup
Set up the marketplace integration by installing it from Vercel, linking Braintrust, choosing a plan, and adding a drain.1
Install the integration
Visit the Vercel Marketplace listing and select Install.
2
Create or link your account
Create a Braintrust account or link your existing account.
3
Select a plan and project
Select a plan and create a project name:
- Starter: Free with basic limits.
- Starter (pay-as-you-go): No monthly fee, but charges for usage beyond the free tier. Requires a payment method.
- Pro: Higher limits with a monthly fee.
4
Add a drain
Select Add Drain to configure trace collection.
Configure log drain
Choose which trace data Vercel sends to Braintrust and which projects the drain covers.1
Select trace data
In the Add Drain panel, select Traces and Next.
2
Choose projects
Choose which Vercel projects to trace (All Projects or specific projects).
3
Set sampling
Set the sampling rate for trace collection.
Enable OpenTelemetry
In your Next.js project, create aninstrumentation.ts file and call registerOtel to emit traces to the marketplace drain.