npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@reaatech/otel-genai-semconv-anthropic

v0.1.0

Published

Anthropic SDK instrumentation with OTel GenAI semantic conventions

Downloads

160

Readme

@reaatech/otel-genai-semconv-anthropic

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Transparent instrumentation for the Anthropic Node.js SDK. Wraps client.messages.create() to emit OpenTelemetry GenAI semantic convention spans with request metadata, token usage including prompt caching, cost tracking, and streaming metrics for message delta events.

Installation

npm install @reaatech/otel-genai-semconv-anthropic
# or
pnpm add @reaatech/otel-genai-semconv-anthropic

Feature Overview

  • Zero-config instrumentation — call instrument(client) once, every messages.create() call is traced
  • Prompt caching awareness — tracks cache_read_input_tokens and cache_creation_input_tokens from Anthropic's usage metadata
  • Streaming delta aggregation — merges message_start, content_block_delta, and message_delta events into a final Message with accumulated token counts
  • Tool use events — tool call content blocks emit gen_ai.tool_call span events with name and input
  • Double-instrumentation guard — calling instrument() twice is a safe no-op
  • Lifecycle hooksonStart and onEnd callbacks for custom span attributes
  • Safe uninstrument — restores the original create method
  • Dual ESM/CJS output — works with import and require

Quick Start

import { AnthropicInstrumentation } from "@reaatech/otel-genai-semconv-anthropic";
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

new AnthropicInstrumentation({ trackCosts: true }).instrument(client);

const response = await client.messages.create({
  model: "claude-3-opus-20240229",
  max_tokens: 200,
  messages: [{ role: "user", content: "What are the benefits of OpenTelemetry?" }],
});
// Each call now emits OTel spans with gen_ai.* attributes

Captured Attributes

Request Attributes

| Attribute | Source | Description | |-----------|--------|-------------| | gen_ai.request.model | request.model | Requested model name | | gen_ai.request.max_tokens | request.max_tokens | Max tokens limit | | gen_ai.request.temperature | request.temperature | Sampling temperature | | gen_ai.request.top_p | request.top_p | Top-p sampling | | gen_ai.request.top_k | request.top_k | Top-k sampling | | gen_ai.request.streaming | request.stream | Streaming flag | | gen_ai.request.stop_sequences | request.stop_sequences | Stop sequences | | gen_ai.request.tool_names | request.tools | Tool names | | gen_ai.provider.name | hardcoded | "anthropic" |

Response Attributes

| Attribute | Source | Description | |-----------|--------|-------------| | gen_ai.response.model | response.model | Actual model used | | gen_ai.response.id | response.id | Response identifier | | gen_ai.response.finish_reasons | response.stop_reason (mapped) | Mapped to OTel finish reason | | gen_ai.usage.input_tokens | response.usage.input_tokens | Input token count | | gen_ai.usage.output_tokens | response.usage.output_tokens | Output token count |

Stop Reason Mapping

Anthropic's stop_reason values are mapped to OTel finish_reason:

| Anthropic | OTel | |-----------|------| | end_turn | stop | | stop_sequence | stop | | max_tokens | length | | tool_use | tool_calls |

Streaming Attributes

| Attribute | Description | |-----------|-------------| | gen_ai.streaming.time_to_first_token_ms | Latency to first chunk | | gen_ai.streaming.total_duration_ms | Total streaming duration | | gen_ai.streaming.chunk_count | Number of chunks received |

Cost Attributes (when trackCosts: true)

| Attribute | Description | |-----------|-------------| | llm.cost.total | Total cost in USD | | llm.cost.input | Input token cost | | llm.cost.output | Output token cost | | llm.cost.currency | Currency code (always "USD") |

Span Events

| Event | When | |-------|------| | gen_ai.system.message | System prompt in the request | | gen_ai.user.message | User messages in the request | | gen_ai.assistant.message | Text content blocks in the response | | gen_ai.tool_call | Tool use content blocks (with tool_name, tool_input) |

API Reference

AnthropicInstrumentation (class)

Constructor

new AnthropicInstrumentation({
  captureRequestHeaders?: boolean;
  captureResponseHeaders?: boolean;
  trackCosts?: boolean;
  pricing?: Record<string, PricingInfo>;
  onStart?: (span: Span, request: MessageCreateParams) => void;
  onEnd?: (span: Span, response: Message) => void;
})

Methods

| Method | Description | |--------|-------------| | instrument(client) | Wrap client.messages.create() with instrumentation | | uninstrument(client) | Restore the original create() method |

AnthropicTokenCounter (class)

Character-based token estimation for Anthropic models:

const counter = new AnthropicTokenCounter();
counter.countTokens("Hello, world!", "claude-3-opus-20240229");
counter.countMessagesTokens(messages, "claude-3-opus-20240229");
counter.clearCache();

Attribute Mappers

import { mapAnthropicRequest, mapAnthropicResponse, mapAnthropicError } from "@reaatech/otel-genai-semconv-anthropic";

const requestAttrs = mapAnthropicRequest(messageParams);
const responseAttrs = mapAnthropicResponse(messageObject);
const errorAttrs = mapAnthropicError(apiError);

Configuration

Custom Pricing

new AnthropicInstrumentation({
  trackCosts: true,
  pricing: {
    "claude-3-opus": { input: 0.015, output: 0.075 },
    "claude-3-sonnet": { input: 0.003, output: 0.015 },
  },
}).instrument(client);

Lifecycle Hooks

new AnthropicInstrumentation({
  onStart: (span, request) => {
    if (request.metadata?.user_id) {
      span.setAttribute("enduser.id", request.metadata.user_id);
    }
  },
  onEnd: (span, response) => {
    span.setAttribute("response.stop_reason", response.stop_reason);
  },
}).instrument(client);

Usage Patterns

Streaming with Delta Aggregation

The instrumentation automatically aggregates streaming delta events into a final Message:

const stream = await client.messages.create({
  model: "claude-3-opus-20240229",
  max_tokens: 500,
  messages: [{ role: "user", content: "Write a haiku" }],
  stream: true,
});

for await (const event of stream) {
  // Each event type (message_start, content_block_delta, message_delta) is tracked
}
// Span auto-finalizes with aggregated response attributes, tokens, and cost

Multi-Client

const instrumentation = new AnthropicInstrumentation({ trackCosts: true });

const client1 = new Anthropic({ apiKey: "...", baseURL: "..." });
const client2 = new Anthropic({ apiKey: "...", baseURL: "..." });

instrumentation.instrument(client1);
instrumentation.instrument(client2);

Related Packages

License

MIT