Skip to main content
GitHub

Anthropic (JS)

Instrument Anthropic in Node.js/TypeScript.

Auto-instrument the Anthropic Node.js SDK.

Installation

npm install risicare @anthropic-ai/sdk

Quick Start

import { init } from 'risicare';
import { patchAnthropic } from 'risicare/anthropic';
import Anthropic from '@anthropic-ai/sdk';
 
// Initialize Risicare
init();
 
// Wrap the Anthropic client
const anthropic = patchAnthropic(new Anthropic());
 
// All calls are now traced
const response = await anthropic.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello!' }],
});

How It Works

patchAnthropic() intercepts messages.create calls:

import { patchAnthropic } from 'risicare/anthropic';
 
const original = new Anthropic();
const traced = patchAnthropic(original);
 
// Only traced client creates spans
await traced.messages.create({...}); // Traced

Captured Attributes

AttributeDescription
gen_ai.system"anthropic"
gen_ai.request.modelModel name (claude-sonnet-4-20250514, etc.)
gen_ai.response.modelActual model used
gen_ai.usage.input_tokensInput tokens
gen_ai.usage.output_tokensOutput tokens
gen_ai.request.max_tokensMax tokens setting

Streaming Support

Streaming via messages.create

Risicare instruments messages.create. When you pass stream: true, the response is an async iterable that is fully traced including accumulated token counts.

const stream = await anthropic.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Write a poem' }],
  stream: true,
});
 
for await (const event of stream) {
  if (event.type === 'content_block_delta') {
    process.stdout.write(event.delta.text);
  }
}

Tool Use

Tool use is automatically traced:

const response = await anthropic.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  tools: [
    {
      name: 'get_weather',
      description: 'Get weather for a location',
      input_schema: {
        type: 'object',
        properties: {
          location: { type: 'string' },
        },
        required: ['location'],
      },
    },
  ],
  messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
});
 
// Tool calls appear as events on the span

Extended Thinking

Extended thinking blocks are captured:

const response = await anthropic.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 16000,
  thinking: {
    type: 'enabled',
    budget_tokens: 10000,
  },
  messages: [{ role: 'user', content: 'Solve this complex problem...' }],
});
 
// Span includes thinking content in a separate attribute

Next Steps