Skip to main content
wrapVercelSdk instruments the Vercel AI SDK module. Wrapped functions behave exactly like the originals; each call is captured as an LLM event, and tools executed during the call are captured as tool events on the same trace.

Quick setup

Unlike the OpenAI and Anthropic wrappers, wrapVercelSdk does not mutate the module you pass in — it returns a new wrapped object. Import functions from the wrapped object, not from ai directly.
Deploying to a serverless platform — Next.js, Vercel, AWS Lambda? Flush buffered events before the function exits so none are lost. See Serverless.

What gets captured

The wrapper forces AI SDK telemetry on for wrapped calls and uses its lifecycle callbacks to build events. Existing telemetry configuration is preserved.

Capturing cost

Calls through the Vercel AI Gateway — a bare id like "openai/gpt-5" — are priced automatically: Carbon records the gateway as the provider and ingest computes the cost from it, with no setup. Everything below is only needed when you construct a provider yourself. When you build a provider (createOpenAI, createAnthropic, …), the Vercel AI SDK hides the request URL — so Carbon can’t see which platform served the call, and without the provider it can’t price it. Pass carbon.fetch so Carbon captures the endpoint and resolves the provider and cost from it:
carbon.fetch is a drop-in fetch: it records the request URL and otherwise behaves normally. It works with every AI SDK provider — createAnthropic, createAmazonBedrock, createOpenAICompatible, a custom baseURL, and so on. If you already pass your own fetch, compose it instead:

Attach metadata

Add a carbon field to the call arguments to set context identifiers or custom properties:
Tool executions inside the call are recorded automatically — more on that under Tool calls. Each call gets its own trace automatically; pass a traceId only when one trace should span several calls. See Context for what each field powers in the dashboard.

Streaming

streamText events are recorded after the stream finishes, so consume the stream to completion:
A stream that is abandoned before it completes produces no event.

Tool calls

Every tool the AI SDK runs is instrumented automatically. generateText, streamText, and ToolLoopAgent (both generate and stream) capture each tool execution as a tool event on the same trace as the call — no wrapTool, no extra setup. Carbon reads them straight from the SDK’s tool telemetry, so single calls and multi-step tool loops are covered alike. The get_weather tool in Attach metadata above is captured this way.
Only tools the SDK runs are captured here. A tool defined without an execute function is handed back to your code to run rather than executed by the SDK — if you run a tool yourself, wrap it with wrapTool to capture it.