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.prompt_tokensInput tokens (from Anthropic's input_tokens)
gen_ai.usage.completion_tokensOutput tokens (from Anthropic's output_tokens)
gen_ai.usage.total_tokensTotal tokens
llm.cost.total_usdCalculated cost

Streaming Support

Streaming captures limited data

Streaming calls create a span with model info, but response attributes (token counts, cost) are not captured. Use non-streaming calls for complete telemetry.

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 and extended thinking

The request is traced as a standard LLM span (model, tokens, cost, finish reason). Tool-use and extended-thinking blocks are not separately captured as of 0.4.2.

Next Steps