---
title: "Agent Tracing"
description: "Monitor AI agents with token usage, latency, tool execution, and error tracking."
url: https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing/
---

# Set Up Agent Tracing | Sentry for Next.js

With [Sentry Agent Tracing](https://docs.sentry.io/product/agents/dashboards.md), you can monitor and debug your AI systems with full-stack context. You'll be able to track key insights like token usage, latency, tool usage, and error rates — and group multi-turn chats in [Conversations](https://docs.sentry.io/product/agents/conversations.md). Agent Tracing data is fully connected to your other Sentry data like logs, errors, and traces.

## [Getting Started](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#getting-started)

Enable [tracing](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing.md) and pass `dataCollection` so generative AI content follows the SDK defaults (including prompts and responses when left unconfigured).

`dataCollection: {}` also opts into other **permissive** defaults (bodies, cookies, user info, and more). Tighten via [`dataCollection`](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/options.md#dataCollection).

```javascript
import * as Sentry from "<sdk-package-name>";

Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tracesSampleRate: 1.0,
  dataCollection: {},
});
```

## [Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#instrumentation)

Pick your AI stack. Some libraries auto-instrument; others need a short setup — each page has the details.

* [Vercel AI SDK](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/vercelai.md)
* [OpenAI](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/openai.md)
* [Anthropic](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/anthropic.md)
* [Google Gen AI SDK](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/google-genai.md)
* [LangChain](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/langchain.md)
* [LangGraph](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/integrations/langgraph.md)

## [Tracking Conversations](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#tracking-conversations)

Tracking Conversations has **beta** stability. Configuration options and behavior may change.

[Conversations](https://docs.sentry.io/product/agents/conversations.md) groups multi-turn AI activity into a single replay of messages and tool calls. Use `setConversationId()` so every AI span in a chat session shares the same `gen_ai.conversation.id`.

Some integrations infer a conversation ID automatically. For everything else (including Workers AI), set it yourself at the start of **every request or operation that makes AI calls**, before those calls run. Reuse the same session ID across messages in the chat. The ID is applied to AI-related spans on the current isolation scope (which is typically request-scoped). Pass `null` to unset it.

```javascript
Sentry.setConversationId("conv_abc123");
```

### [Identifying Users in Conversations](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#identifying-users-in-conversations)

The [Conversations](https://docs.sentry.io/product/agents/conversations.md) view includes a **User** column. To populate it, call `setUser` once per request or session, before any AI calls:

```javascript
Sentry.setUser({
  id: "user_123",
  email: "jane@example.com",
  username: "jane",
});
```

Any of `id`, `email`, or `username` is sufficient — Conversations displays whichever fields are present. See the [setUser API reference](https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/apis.md#setUser) for the full list of supported fields.

## [Options](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#options)

### [Privacy Controls](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#privacy-controls)

With `dataCollection: {}` (or any `dataCollection` config), generative AI inputs and outputs default to **on**. To turn them off:

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  dataCollection: {
    genAI: { inputs: false, outputs: false },
  },
});
```

You can also set `recordInputs` / `recordOutputs` on a specific AI integration. See each integration page for the API that applies on your platform.

### [Streaming Gen AI Spans](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#streaming-gen-ai-spans)

From SDK `10.61.0`, `gen_ai` spans are sent as standalone envelope items (avoids large-payload drops; required for [Conversations](https://docs.sentry.io/product/agents/conversations.md)).

Self-hosted Sentry users should set `streamGenAiSpans: false` if standalone `gen_ai` spans may not be ingested.

```javascript
Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  streamGenAiSpans: false,
});
```

## [Manual Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#manual-instrumentation)

You can also instrument agent spans yourself. See [manual instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing/manual-instrumentation.md).

## [Framework Exporters](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#framework-exporters)

If you're using an AI framework with a Sentry exporter, you can send traces to Sentry:

* [Mastra Sentry Exporter](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing/mastra.md)

## [MCP Server Monitoring](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing.md#mcp-server-monitoring)

If you're building MCP (Model Context Protocol) servers, Sentry can also track tool executions, prompt retrievals, and resource access. See [Instrument MCP Servers](https://docs.sentry.io/platforms/javascript/guides/nextjs/tracing/instrumentation/mcp-module.md) for setup instructions.

## Pages in this section

- [Manual Instrumentation](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing/manual-instrumentation.md)
- [Mastra](https://docs.sentry.io/platforms/javascript/guides/nextjs/agent-tracing/mastra.md)
