Skip to content

Latest commit

 

History

History
169 lines (123 loc) · 6.38 KB

File metadata and controls

169 lines (123 loc) · 6.38 KB
title Anthropic
description Adds instrumentation for the Anthropic SDK.
supported
javascript.node
javascript.aws-lambda
javascript.azure-functions
javascript.connect
javascript.express
javascript.fastify
javascript.gcp-functions
javascript.hapi
javascript.hono
javascript.koa
javascript.nestjs
javascript.electron
javascript.nextjs
javascript.nuxt
javascript.solidstart
javascript.sveltekit
javascript.react-router
javascript.remix
javascript.astro
javascript.bun
javascript.tanstackstart-react
javascript.cloudflare
javascript
javascript.react
javascript.angular
javascript.vue
javascript.svelte
javascript.solid
javascript.ember
javascript.gatsby
javascript.react-native

<PlatformSection supported={["javascript.nextjs", "javascript.nuxt", "javascript.solidstart", "javascript.sveltekit", "javascript.react-router", "javascript.remix", "javascript.astro", "javascript.tanstackstart-react"]}> For meta-framework applications running on both client and server, we recommend setting up the integration manually using the instrumentAnthropicAiClient wrapper to ensure consistent instrumentation across all runtimes.

<PlatformSection supported={["javascript.node", "javascript.aws-lambda", "javascript.azure-functions", "javascript.connect", "javascript.express", "javascript.fastify", "javascript.gcp-functions", "javascript.hapi", "javascript.hono", "javascript.koa", "javascript.nestjs", "javascript.nextjs", "javascript.nuxt", "javascript.solidstart", "javascript.sveltekit", "javascript.react-router", "javascript.remix", "javascript.astro", "javascript.tanstackstart-react", "javascript.electron", "javascript.cordova", "javascript.capacitor", "javascript.deno", "javascript.wasm"]}>

Automatic Instrumentation

Import name: Sentry.anthropicAIIntegration

<PlatformSection notSupported={["javascript.node", "javascript.connect", "javascript.hapi", "javascript.koa"]}> If you are using a different runtime (like Bun, Cloudflare Workers or a Browser) or experiencing missing spans, you need to use Manual Instrumentation to explicitly wrap your AI client instance instead.

The anthropicAIIntegration adds instrumentation for the @anthropic-ai/sdk API to capture spans by wrapping Anthropic SDK calls and recording LLM interactions.

In Node.js runtimes, this integration is enabled by default and automatically captures spans for Anthropic SDK calls (requires Sentry SDK version 10.28.0 or higher).

To customize what data is captured (such as inputs and outputs), see the Options in the Configuration section.

<PlatformSection notSupported={["javascript.node", "javascript.connect", "javascript.hapi", "javascript.koa"]}>

Manual Instrumentation

Import name: Sentry.instrumentAnthropicAiClient

The instrumentAnthropicAiClient helper adds instrumentation for the @anthropic-ai/sdk API to capture spans by wrapping Anthropic SDK calls and recording LLM interactions with configurable input/output recording. You need to manually wrap your Anthropic client instance with this helper.

See the example below:

import Anthropic from "@anthropic-ai/sdk";

const anthropic = new Anthropic({
apiKey: "your-api-key", // Warning: API key will be exposed in browser!
});

const client = Sentry.instrumentAnthropicAiClient(anthropic, {
recordInputs: true,
recordOutputs: true,
});

// Use the wrapped client instead of the original anthropic instance
const response = await client.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello!" }],
});

To customize what data is captured (such as inputs and outputs), see the Options in the Configuration section.

Configuration

Options

The following options control what data is captured from Anthropic SDK calls:

recordInputs

Type: boolean (optional)

Records inputs to Anthropic SDK calls (such as prompts and messages).

Defaults to true if sendDefaultPii is true.

recordOutputs

Type: boolean (optional)

Records outputs from Anthropic SDK calls (such as generated text and responses).

Defaults to true if sendDefaultPii is true.

Usage

<PlatformSection supported={["javascript.node", "javascript.aws-lambda", "javascript.azure-functions", "javascript.connect", "javascript.express", "javascript.fastify", "javascript.gcp-functions", "javascript.hapi", "javascript.hono", "javascript.koa", "javascript.nestjs", "javascript.nextjs", "javascript.nuxt", "javascript.solidstart", "javascript.sveltekit", "javascript.react-router", "javascript.remix", "javascript.astro", "javascript.tanstackstart-react", "javascript.electron", "javascript.cordova", "javascript.capacitor", "javascript.deno", "javascript.wasm"]}>

Using the anthropicAIIntegration integration for automatic instrumentation:

Sentry.init({
dsn: "____PUBLIC_DSN____",
// Tracing must be enabled for agent monitoring to work
tracesSampleRate: 1.0,
integrations: [
Sentry.anthropicAIIntegration({
// your options here
}),
],
});

<PlatformSection notSupported={["javascript.node", "javascript.connect", "javascript.hapi", "javascript.koa"]}>

Using the instrumentAnthropicAiClient wrapper for manual instrumentation:

const client = Sentry.instrumentAnthropicAiClient(anthropic, {
// your options here
});

Supported Operations

By default, tracing support is added to the following Anthropic SDK calls:

  • messages.create() - Create messages with Claude models
  • messages.stream() - Stream messages with Claude models
  • messages.countTokens() - Count tokens for messages
  • models.get() - Get model information
  • completions.create() - Create completions (legacy)
  • models.retrieve() - Retrieve model details
  • beta.messages.create() - Beta messages API

Streaming and non-streaming requests are automatically detected and handled appropriately.

Supported Versions

  • @anthropic-ai/sdk: >=0.19.2 <1.0.0