Anthropic (JS)
Instrument Anthropic in Node.js/TypeScript.
Auto-instrument the Anthropic Node.js SDK.
Installation
npm install risicare @anthropic-ai/sdkQuick 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({...}); // TracedCaptured Attributes
| Attribute | Description |
|---|---|
gen_ai.system | "anthropic" |
gen_ai.request.model | Model name (claude-sonnet-4-20250514, etc.) |
gen_ai.response.model | Actual model used |
gen_ai.usage.prompt_tokens | Input tokens (from Anthropic's input_tokens) |
gen_ai.usage.completion_tokens | Output tokens (from Anthropic's output_tokens) |
gen_ai.usage.total_tokens | Total tokens |
llm.cost.total_usd | Calculated 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.