---
title: Anthropic / Claude
description: Auto-instrument the Anthropic Claude SDK with OpenTelemetry and send GenAI spans to Coralogix AI Center.
url: https://coralogix.com/docs/user-guides/ai/otel-integration/code-examples/anthropic-claude/
product: ai
feature: AI App Catalog
feature_id: ai-app-catalog
feature_identifier: ai/ai-app-catalog
last_updated: '2026-09-03'
---

# Anthropic / Claude

Auto-instrument the Anthropic SDK with the OpenLLMetry instrumentor and send GenAI spans to Coralogix AI Center.

Before you start

These examples export to a local OpenTelemetry Collector over OTLP/gRPC. Deploy the collector first. See [Code examples → Deploy an OpenTelemetry Collector](https://coralogix.com/docs/user-guides/ai/otel-integration/code-examples.md#prerequisites-deploy-an-opentelemetry-collector).

## Install

```
pip install anthropic opentelemetry-instrumentation-anthropic opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc
```

## Environment variables

```
export ANTHROPIC_API_KEY="<YOUR_ANTHROPIC_API_KEY>"

export OTEL_EXPORTER_OTLP_ENDPOINT="http://<COLLECTOR_HOST>:4317"

export OTEL_EXPORTER_OTLP_INSECURE="true"

export OTEL_RESOURCE_ATTRIBUTES="cx.application.name=my-genai-app,cx.subsystem.name=my-service"

export OTEL_SEMCONV_STABILITY_OPT_IN="gen_ai_latest_experimental"

export TRACELOOP_TRACE_CONTENT="true"
```

## Script

```
import anthropic



# --- OTel imports ---

from opentelemetry import trace

from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor

from opentelemetry.sdk.resources import Resource

from opentelemetry.sdk.trace import TracerProvider

from opentelemetry.sdk.trace.export import BatchSpanProcessor





# --- OTel setup: configure tracer provider and OTLP exporter ---

def configure_otel() -> TracerProvider:

    resource = Resource.create()

    provider = TracerProvider(resource=resource)

    exporter = OTLPSpanExporter()  # reads OTEL_EXPORTER_OTLP_ENDPOINT from env

    provider.add_span_processor(BatchSpanProcessor(exporter))

    trace.set_tracer_provider(provider)

    return provider





def main():

    # OTel: initialize tracing and auto-instrument the Anthropic SDK

    provider = configure_otel()

    AnthropicInstrumentor().instrument(tracer_provider=provider)



    # Your app logic

    client = anthropic.Anthropic()

    message = client.messages.create(

        model="claude-sonnet-4-20250514",

        max_tokens=256,

        messages=[

            {"role": "user", "content": "What is OpenTelemetry in one sentence?"}

        ],

    )

    print(f"Claude response: {message.content[0].text}")



    # OTel: flush and shut down the tracer provider

    provider.force_flush()

    provider.shutdown()





if __name__ == "__main__":

    main()
```

## Expected span attributes

* `gen_ai.system` = `"anthropic"`
* `gen_ai.request.model` = `"claude-sonnet-4-20250514"`
* `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`
* `gen_ai.response.finish_reasons` = `["end_turn"]`

Warning

`opentelemetry-instrumentation-anthropic` on PyPI is from OpenLLMetry (Traceloop), not official OTel contrib. Semconv migration from legacy to new is in progress. Content capture uses `TRACELOOP_TRACE_CONTENT=true` (not the standard `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`).

## Next steps

Look up which open-source library to use for your provider in [Compatibility matrix](https://coralogix.com/docs/user-guides/ai/otel-integration/providers.md).
