Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fuser-guides%2Fai%2Fotel-integration%2Fcode-examples%2Fanthropic-claude.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fuser-guides%2Fai%2Fotel-integration%2Fcode-examples%2Fanthropic-claude.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# 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://docs-docusaurus.kinsta.page/user-guides/ai/otel-integration/code-examples/.md#prerequisites-deploy-an-opentelemetry-collector).

## Install[​](#install "Direct link to Install")

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

## Environment variables[​](#environment-variables "Direct link to 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[​](#script "Direct link to 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[​](#expected-span-attributes "Direct link to 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[​](#next-steps "Direct link to Next steps")

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