{"id":7471,"library":"openinference-instrumentation-anthropic","title":"OpenInference Anthropic Instrumentation","description":"openinference-instrumentation-anthropic is a Python auto-instrumentation library for the Anthropic package. It captures traces for Anthropic client calls (e.g., Messages, Completions, AsyncMessages, AsyncCompletions, BetaMessagesParse) and aligns them with OpenInference semantic conventions. These traces are fully OpenTelemetry compatible and can be sent to any OpenTelemetry collector, such as Arize Phoenix, for observability and evaluation. The library is currently at version 1.0.0 and is actively maintained with frequent updates to the broader OpenInference project.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-anthropic","tags":["observability","telemetry","LLM","Anthropic","OpenTelemetry","AI","tracing"],"install":[{"cmd":"pip install openinference-instrumentation-anthropic anthropic opentelemetry-sdk opentelemetry-exporter-otlp","lang":"bash","label":"Install core and dependencies"}],"dependencies":[{"reason":"Required for making calls to the Anthropic API that this library instruments.","package":"anthropic"},{"reason":"Core OpenTelemetry SDK for creating and managing traces.","package":"opentelemetry-sdk"},{"reason":"Required to export OpenTelemetry traces to an OTLP-compatible collector (e.g., Arize Phoenix).","package":"opentelemetry-exporter-otlp"}],"imports":[{"symbol":"AnthropicInstrumentor","correct":"from openinference.instrumentation.anthropic import AnthropicInstrumentor"}],"quickstart":{"code":"import os\nfrom anthropic import Anthropic\nfrom openinference.instrumentation.anthropic import AnthropicInstrumentor\nfrom opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\nfrom opentelemetry.sdk import trace as trace_sdk\nfrom opentelemetry.sdk.trace.export import SimpleSpanProcessor\n\n# Set your Anthropic API key (replace with actual key or use environment variable)\nos.environ['ANTHROPIC_API_KEY'] = os.environ.get('ANTHROPIC_API_KEY', 'your_anthropic_api_key_here')\n\n# Configure OpenTelemetry TracerProvider to send traces to an OTLP endpoint (e.g., Arize Phoenix)\n# Default Phoenix endpoint: http://localhost:6006/v1/traces\nendpoint = os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://127.0.0.1:6006/v1/traces')\n\ntracer_provider = trace_sdk.TracerProvider()\ntracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))\n\n# Instrument the Anthropic client\nAnthropicInstrumentor().instrument(tracer_provider=tracer_provider)\n\n# Create an Anthropic client and make a request\nclient = Anthropic()\n\ntry:\n    print(\"Making Anthropic API call...\")\n    message = client.messages.create(\n        model=\"claude-3-5-sonnet-20240620\",\n        max_tokens=1000,\n        temperature=0,\n        messages=[\n            {\"role\": \"user\", \"content\": \"Explain the concept of quantum entanglement in simple terms.\"}\n        ]\n    )\n    print(\"Anthropic Response:\")\n    print(message.content[0].text)\n    print(\"Traces should now be visible in your OpenTelemetry collector (e.g., Phoenix).\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Ensure ANTHROPIC_API_KEY is set and your OpenTelemetry collector is running.\")\n","lang":"python","description":"This quickstart demonstrates how to instrument Anthropic API calls using `openinference-instrumentation-anthropic` and send the traces to an OpenTelemetry collector. It configures a `TracerProvider` with an OTLP HTTP exporter, instruments the Anthropic library, and then makes a sample API call. You can run a local collector like Arize Phoenix (<code>python -m phoenix.server.main serve</code>) to view the traces."},"warnings":[{"fix":"Upgrade `openinference-instrumentation-anthropic` and `anthropic` to compatible versions as indicated in the release notes.","message":"Future updates to the underlying `anthropic` SDK (e.g., new API types, changed response structures) may require corresponding updates to `openinference-instrumentation-anthropic` to maintain full compatibility and accurate tracing. Always check release notes for `anthropic` and `openinference` for potential version conflicts or required upgrades.","severity":"breaking","affected_versions":"<1.0.0 (and potentially future versions with new Anthropic SDKs)"},{"fix":"Ensure `AnthropicInstrumentor().instrument()` is called early in your application's lifecycle, typically at the start of your main script or entry point.","message":"The `AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)` call must occur *before* any Anthropic client instances are created or API calls are made to ensure proper instrumentation. If instrumentation is initialized too late, calls may not be traced.","severity":"gotcha","affected_versions":"All"},{"fix":"Verify that the `endpoint` provided to `OTLPSpanExporter` matches the listening address of your OpenTelemetry collector or observability platform.","message":"Incorrectly configured OpenTelemetry exporter endpoint will result in traces not being sent or visible in your observability backend. The default for many local collectors like Phoenix is `http://127.0.0.1:6006/v1/traces`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install openinference-instrumentation-anthropic`.","cause":"The `openinference-instrumentation-anthropic` package has not been installed.","error":"ModuleNotFoundError: No module named 'openinference.instrumentation.anthropic'"},{"fix":"Set the `ANTHROPIC_API_KEY` environment variable with a valid API key, e.g., `export ANTHROPIC_API_KEY='sk-...'`, or pass it directly to the `Anthropic` client constructor.","cause":"The `ANTHROPIC_API_KEY` environment variable is either not set or contains an invalid key, or the key has expired/been revoked.","error":"anthropic.APIStatusError: 401: Invalid Authentication"},{"fix":"Ensure `AnthropicInstrumentor().instrument(tracer_provider=...)` is called early, `tracer_provider` is correctly set up with `OTLPSpanExporter`, and your OpenTelemetry collector (like Phoenix) is actively running and accessible at the configured endpoint.","cause":"The Anthropic client was not successfully instrumented, or the OpenTelemetry `TracerProvider` and `SpanProcessor` are not correctly configured/initialized, or the collector is not running.","error":"No traces appear in my OpenTelemetry collector (e.g., Phoenix) despite running the application."}]}