# Track Vercel AI SDK costs and LLM calls in Next.js

> Add Sentry's Vercel AI integration to your Next.js app to track LLM token usage, per-model costs, tool calls, and agent traces with full-stack trace context.

**URL:** https://sentry.io/cookbook/monitor-ai-agent-costs-nextjs/

---

Add Sentry to your Next.js app to see Vercel AI SDK token usage, LLM costs, tool calls, and agent traces with full-stack trace context. Spot which models drive spend without leaving Sentry.

**Time:** 15–20 minutes | **Difficulty:** Beginner

## What You'll Learn

- Configured Sentry tracing in your Next.js app

- Added the Vercel AI integration for automatic LLM instrumentation

- Enabled telemetry on AI SDK calls to capture prompts and outputs

- Explored agent traces, token usage, and tool calls in Insights → Agents

- Viewed per-model cost and token breakdowns

**Topics:** AI Observability, Tracing, Agent Tracing

**SDKs:** Next.js

## Steps

### 1. Install the Sentry Next.js SDK

Add the Sentry SDK to your project using the wizard, which handles the instrumentation files automatically. Run this in your project root.

### 2. Enable tracing and add the Vercel AI integration

Open `sentry.server.config.ts` and make sure `tracesSampleRate` is greater than zero. Then import `vercelAIIntegration` from `@sentry/nextjs` and add it to the `integrations` array.

### 3. Enable telemetry on your AI SDK calls

In any Route Handler that uses the Vercel AI SDK, add the `experimental_telemetry` option to your `streamText` or `generateText` call. Set `isEnabled: true` and provide a `functionId` — this label appears as the span name in Sentry traces.

You can also set `recordInputs` and `recordOutputs` to capture the full prompts and completions. Disable these on routes that handle sensitive user data.

### 4. Explore agent traces in Insights

Trigger a request in your app, then head to [Insights → Agents](https://sentry.io/orgredirect/organizations/:orgslug/insights/ai-agents/) in Sentry. You'll see pre-built widgets for LLM calls, token usage, and tool calls. Click into any trace to see the full agent workflow — the system prompt, model output, and each tool call nested in sequence.

### 5. Check LLM costs and model breakdown

From the Agents dashboard, click **Models** to get a breakdown of costs, token usage, and token types (input vs. output vs. cached) by model. This view makes it easy to spot which models are driving the most spend and how effectively cached tokens are being used.

You can also build [custom dashboards](https://sentry.io/orgredirect/organizations/:orgslug/dashboards/) to combine this data with other application metrics like error rates or latency.

### 6. Drill into the full trace

From any agent trace, click **View Full Trace** to open the [Trace Explorer](https://sentry.io/orgredirect/organizations/:orgslug/explore/traces/). Here you'll see the entire request lifecycle — from the page load or API call all the way down to each LLM request and tool execution. This gives you the full-stack context to understand whether a slow tool call is a database issue, a network problem, or an AI provider timeout.

## FAQ

**Which AI providers are supported automatically?**

The Vercel AI SDK integration automatically instruments all providers supported by the AI SDK, including OpenAI, Anthropic, Google Gen AI, and others. For unsupported providers, you can add custom spans using the `gen_ai.request` operation.

**Do I need to use the Vercel AI SDK?**

No. If you're using OpenAI, Anthropic, LangChain, or another supported library directly, Sentry has dedicated integrations for each. The `experimental_telemetry` option is specific to the Vercel AI SDK.

**Will this affect my app's performance?**

No. Sentry uses asynchronous, non-blocking transport. Spans are batched and sent in the background with negligible overhead on your request latency.

**How much does it cost in Sentry?**

Traces are billed based on volume. Sentry's free tier includes enough spans to get started — and you can lower `tracesSampleRate` to control exactly how many traces you send.

**Can I capture the actual prompts and responses?**

Yes — set `recordInputs: true` and `recordOutputs: true` in the `experimental_telemetry` config. For privacy-sensitive routes, set these to `false` to capture only metadata like token counts and model names.

---

*Source: [sentry.io/cookbook/monitor-ai-agent-costs-nextjs/](https://sentry.io/cookbook/monitor-ai-agent-costs-nextjs/)*
