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

# Set Up Agent Tracing | Sentry for Node.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. Agent Tracing data will be fully connected to your other Sentry data like logs, errors, and traces.

## [Prerequisites](https://docs.sentry.io/platforms/javascript/guides/node/agent-tracing.md#prerequisites)

Before setting up Agent Tracing, ensure you have [tracing enabled](https://docs.sentry.io/platforms/javascript/guides/node/tracing.md) in your Sentry configuration.

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

The JavaScript SDK supports automatic instrumentation for AI libraries. Add the integration for your AI library to your Sentry configuration:

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

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

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

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

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

All AI integrations support `recordInputs` and `recordOutputs` options to control whether prompts and responses are captured. Both default to `true`.

Set these to `false` if your prompts or responses contain sensitive data you don't want sent to Sentry.

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

Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tracesSampleRate: 1.0,
  integrations: [
    openAIIntegration({
      recordInputs: false, // Don't capture prompts
      recordOutputs: false, // Don't capture responses
    }),
  ],
});
```

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

`gen_ai` spans are sent as standalone envelope items instead of being bundled in the transaction by default (since SDK version `10.61.0`). This prevents AI spans with large inputs and outputs from hitting transaction payload size limits and being dropped, and is required for [Conversations](https://docs.sentry.io/product/agents/conversations.md) to work.

To send `gen_ai` spans as part of the transaction instead, set `streamGenAiSpans` to `false`. Self-hosted Sentry users should set this to `false`, as standalone `gen_ai` spans may not be ingested by their Sentry instance.

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

Sentry.init({
  dsn: "https://<key>@o<orgId>.ingest.sentry.io/<projectId>",
  tracesSampleRate: 1.0,
  streamGenAiSpans: false, // opt out of streaming gen_ai spans
});
```

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

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

When building AI applications with multi-turn conversations, you can use `setConversationId()` to link all AI spans from the same conversation together. This allows you to analyze entire conversation flows in Sentry.

The conversation ID is automatically applied as the `gen_ai.conversation.id` attribute to all AI-related spans within the current scope. To unset the conversation ID, pass `null`.

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

// Set conversation ID at the start of a conversation
Sentry.setConversationId("conv_abc123");

// All subsequent AI calls will be linked to this conversation
await openai.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: "Hello" }],
});

// Later in the conversation
await openai.chat.completions.create({
  model: "gpt-4",
  messages: [
    { role: "user", content: "Hello" },
    { role: "assistant", content: "Hi there!" },
    { role: "user", content: "What's the weather?" },
  ],
});

// Both calls will have gen_ai.conversation.id: "conv_abc123"

// To unset it
Sentry.setConversationId(null);
```

## [Identifying Users in Conversations](https://docs.sentry.io/platforms/javascript/guides/node/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
import * as Sentry from "<sdk-package-name>";

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/configuration/apis.md#setUser) for the full list of supported fields.

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

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

## [Framework Exporters](https://docs.sentry.io/platforms/javascript/guides/node/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/node/agent-tracing/mastra.md)

## [MCP Server Monitoring](https://docs.sentry.io/platforms/javascript/guides/node/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/node/tracing/instrumentation/mcp-module.md) for setup instructions.

## Pages in this section

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