OpenTelemetry instrumentation for Anthropic
Actively maintained. Last release 3 weeks ago, commits in 16 of the last 26 weeks. A safe choice.
pip install traceai-anthropicREADME as published on PyPI with version 0.1.14.
Anthropic OpenTelemetry Integration
Overview
This integration provides support for using OpenTelemetry with the Anthropic framework. It enables tracing and monitoring of applications built with Anthropic.
Installation
- Install traceAI Anthropic
pip install traceAI-anthropic
Set Environment Variables
Set up your environment variables to authenticate with FutureAGI
import os
os.environ["FI_API_KEY"] = FI_API_KEY
os.environ["FI_SECRET_KEY"] = FI_SECRET_KEY
os.environ["ANTHROPIC_API_KEY"] = ANTHROPIC_API_KEY
Quickstart
Register Tracer Provider
Set up the trace provider to establish the observability pipeline. The trace provider:
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
trace_provider = register(
project_type=ProjectType.OBSERVE,
project_name="anthropic_app"
)
Configure Anthropic Instrumentation
Instrument the Anthropic client to enable telemetry collection. This step ensures that all interactions with the Anthropic SDK are tracked and monitored.
from traceai_anthropic import AnthropicInstrumentor
AnthropicInstrumentor().instrument(tracer_provider=trace_provider)
Anthropic SDK native tracing
Anthropic SDK 1.14.0 and later can trace Messages calls themselves. To send those spans to Future AGI without this instrumentor, pass the provider to the client:
client = anthropic.Anthropic(open_telemetry={"tracer_provider": trace_provider})
The SDK's spans include model, timing and token usage, and include prompts and responses only with "traces": {"content_mode": "content"}. TraceConfig, FI_HIDE_* settings and the using_session / using_attributes context (session, user, metadata, tags) don't apply to them.
When AnthropicInstrumentor is installed and the SDK's spans would reach the same tracer provider (because you pass it to the client, or because it is the global provider), the instrumentor records messages.create (sync and async) and sync messages.count_tokens calls itself and turns the SDK's span off for each of those calls, so each call is recorded once with the Future AGI context and privacy controls. Your client keeps its own open_telemetry option. To skip this per-call step, create the client with open_telemetry=False. messages.stream() and async messages.count_tokens are not wrapped by the instrumentor, so they keep the SDK's span.
Create Anthropic Components
Set up your Anthropic client with built-in observability.
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The unit of temperature, either 'celsius' or 'fahrenheit'",
},
},
"required": ["location"],
},
},
{
"name": "get_time",
"description": "Get the current time in a given time zone",
"input_schema": {
"type": "object",
"properties": {
"timezone": {
"type": "string",
"description": "The IANA time zone name, e.g. America/Los_Angeles",
}
},
"required": ["timezone"],
},
},
],
messages=[
{
"role": "user",
"content": "What is the weather like right now in New York?"
" Also what time is it there? Use necessary tools simultaneously.",
}
],
)
print(response)