> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/Arize-ai/openinference/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic

> Auto-instrumentation for the Anthropic SDK

This module provides automatic instrumentation for the [Anthropic SDK](https://www.anthropic.com/) which may be used in conjunction with OpenTelemetry.

## Installation

Install the instrumentation package:

<CodeGroup>
  ```bash npm theme={null}
  npm install @arizeai/openinference-instrumentation-anthropic
  ```

  ```bash yarn theme={null}
  yarn add @arizeai/openinference-instrumentation-anthropic
  ```

  ```bash pnpm theme={null}
  pnpm add @arizeai/openinference-instrumentation-anthropic
  ```
</CodeGroup>

## Quickstart

Install required packages:

```bash theme={null}
npm install @arizeai/openinference-instrumentation-anthropic @arizeai/openinference-semantic-conventions @anthropic-ai/sdk @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-proto @opentelemetry/sdk-trace-node @opentelemetry/semantic-conventions
```

Set up instrumentation in your application:

```typescript theme={null}
import { NodeSDK, resources } from "@opentelemetry/sdk-node";
import { AnthropicInstrumentation } from "@arizeai/openinference-instrumentation-anthropic";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-node";
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";

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

// Configure the SDK with Anthropic instrumentation
const instrumentation = new AnthropicInstrumentation({
  // Optional: configure trace settings
  traceConfig: {
    // hideInputs: true,
    // hideOutputs: true,
  },
});
// necessary when instrumenting in an ESM environment
instrumentation.manuallyInstrument(Anthropic);
const sdk = new NodeSDK({
  spanProcessors: [
    new SimpleSpanProcessor(
      new OTLPTraceExporter({
        url: "http://localhost:6006/v1/traces",
      }),
    ),
  ],
  resource: resources.resourceFromAttributes({
    [ATTR_SERVICE_NAME]: "anthropic-service",
    [SEMRESATTRS_PROJECT_NAME]: "anthropic-service",
  }),
  instrumentations: [instrumentation],
});

// Initialize the SDK
sdk.start();

// Now use Anthropic as normal

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

const message = await anthropic.messages.create({
  model: "claude-3-5-haiku-latest",
  max_tokens: 1000,
  messages: [{ role: "user", content: "Hello, Claude!" }],
});

console.log(message.content);

sdk.shutdown();
```

## Configuration

The `AnthropicInstrumentation` constructor accepts the following options:

* `instrumentationConfig`: Standard OpenTelemetry instrumentation configuration
* `traceConfig`: OpenInference-specific trace configuration for masking/redacting sensitive information
* `tracerProvider`: Optional custom tracer provider

## Supported Features

* **Messages API**: Full support for `anthropic.messages.create()`
* **Streaming**: Automatic handling of streaming responses
* **Tool Use**: Captures tool/function calling information
* **Token Usage**: Records input/output token counts when available
* **Error Handling**: Proper error recording and span status management

## Semantic Conventions

This instrumentation follows the [OpenInference semantic conventions](https://github.com/Arize-ai/openinference/blob/main/spec/semantic_conventions.md) for LLM observability, capturing:

* Model name and provider information
* Input/output messages and content
* Token usage statistics
* Tool calling details
* Invocation parameters

## Resources

* [npm package](https://www.npmjs.com/package/@arizeai/openinference-instrumentation-anthropic)
* [Source code](https://github.com/Arize-ai/openinference/tree/main/js/packages/openinference-instrumentation-anthropic)
* [OpenTelemetry Node.js SDK documentation](https://opentelemetry.io/docs/instrumentation/js/getting-started/nodejs/)
