Skip to main content

Install

pip install anthropic openinference-instrumentation-anthropic \
            opentelemetry-sdk opentelemetry-exporter-otlp-proto-http

Setup

Anthropic traces can include input messages, output messages, tool names, and tool parameters. Review Data Governance before enabling production traffic.
import os
from anthropic import Anthropic
from openinference.instrumentation.anthropic import AnthropicInstrumentor
from openinference.instrumentation import using_attributes
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter

os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = "https://otel.agnost.ai"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = "X-Agnost-Org-ID=<your-org-id>"

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
trace.set_tracer_provider(provider)
AnthropicInstrumentor().instrument(tracer_provider=provider)

client = Anthropic()
with using_attributes(session_id="sess-123", user_id="u-42"):
    client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hi"}],
    )
Tool-use blocks are unpacked. You get llm.input_messages.*, llm.output_messages.*, tool.name, tool.parameters, and llm.token_count.{prompt,completion}.

Alternative

The Traceloop alternative (opentelemetry-instrumentation-anthropic) emits gen_ai.* and traceloop.*: also recognized by Agnost.

Verify

Run one client.messages.create call, then open Raw logs in Agnost. Confirm message and tool-use attributes appear as expected.

Troubleshooting

  • Confirm AnthropicInstrumentor().instrument(...) runs before Anthropic calls.
  • Confirm OTEL_EXPORTER_OTLP_HEADERS contains X-Agnost-Org-ID=<your-org-id>.
  • Confirm using_attributes wraps the call you want grouped.

References