Anthropic tracing with Future AGI
Trace Anthropic Messages API calls in Future AGI with native OpenTelemetry tracing in the Python and TypeScript SDKs.
Trace Anthropic Messages API calls in Future AGI, including request timing, model details, token usage and, when you enable it, message content.
1. Install
If your JavaScript app already uses @traceai/anthropic, remove its instrumentation setup and run npm uninstall @traceai/anthropic before installing the packages below. The SDK traces calls itself; don’t instrument the same client twice.
pip install "anthropic>=1.14.0" fi-instrumentation-otelnpm install @anthropic-ai/sdk@1.4.0 @traceai/fi-core The Python example uses the Anthropic SDK’s native OpenTelemetry tracing, available in SDK 1.14.0 and later, so it doesn’t need traceAI-anthropic. To keep an existing AnthropicInstrumentor setup, see Keep AnthropicInstrumentor in Python.
The JavaScript/TypeScript example uses native tracing in SDK 1.4.0. It doesn’t need @traceai/anthropic or an older Anthropic SDK.
2. Set environment variables
Export credentials before running either example. Set ANTHROPIC_MODEL to a Messages API model available to your account.
export FI_API_KEY="YOUR_FI_API_KEY"
export FI_SECRET_KEY="YOUR_FI_SECRET_KEY"
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
export ANTHROPIC_MODEL="YOUR_MODEL_ID"
Both examples use metadata_only: spans include model, timing and token usage without prompts or responses. Enable message content only when your application’s data-handling policy allows it, using the SDK’s content mode in step 4.
TraceConfig and FI_HIDE_* settings don’t apply to these native SDK spans. Metadata, custom attributes and error text can still contain sensitive data; metadata-only tracing is not a guarantee that every error or custom field is redacted.
3. Initialize tracing
Use the same project name for the provider and the project you open in Observe.
from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType
trace_provider = register(
project_type=ProjectType.OBSERVE,
project_name="anthropic_project",
)import { register, ProjectType } from "@traceai/fi-core";
const tracerProvider = register({
projectType: ProjectType.OBSERVE,
projectName: "anthropic_project",
}); 4. Trace the client
Pass the provider to the Anthropic client. Neither example needs a separate instrumentor or a module-loading hook.
import anthropic
client = anthropic.Anthropic(
open_telemetry={
"tracer_provider": trace_provider,
"traces": {"content_mode": "metadata_only"},
}
)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
openTelemetry: {
tracerProvider,
traces: { contentMode: "metadata_only" },
},
}); To export prompts and responses, change "metadata_only" to "content" in Python’s content_mode or TypeScript’s contentMode. Pass the same open_telemetry option to anthropic.AsyncAnthropic for async Python code.
Native SDK spans don’t inherit the session, user, metadata or tags context set with context helpers such as using_session or using_attributes. Python applications that rely on these helpers can keep AnthropicInstrumentor instead.
5. Make a request
Run these snippets in the same file as the setup above. Shutdown flushes pending spans before this short script exits.
import os
try:
message = client.messages.create(
model=os.environ["ANTHROPIC_MODEL"],
max_tokens=128,
messages=[{"role": "user", "content": "Explain what an API is in one sentence."}],
)
print(message.content)
finally:
trace_provider.shutdown()const model = process.env.ANTHROPIC_MODEL;
if (!model) throw new Error("Set ANTHROPIC_MODEL");
try {
const message = await client.messages.create({
model,
max_tokens: 128,
messages: [{ role: "user", content: "Explain what an API is in one sentence." }],
});
console.log(message.content);
} finally {
await tracerProvider.shutdown();
} View the trace
In Future AGI, open Observe, select anthropic_project, and open the trace to inspect the model call. Message content depends on the capture controls you configured.
The native SDK spans are named anthropic.messages.create. Their input token count already includes cache reads and cache writes, so don’t add those counts again.
Keep AnthropicInstrumentor in Python
Applications that already use traceAI-anthropic can keep it, for example to keep its TraceConfig and FI_HIDE_* controls, the session, user, metadata and tags from context helpers, or its Protect spans. Trace each call with one owner: on Anthropic SDK 1.14.0 and later, create the client with open_telemetry=False so that only the instrumentor records its calls.
import anthropic
from traceai_anthropic import AnthropicInstrumentor
AnthropicInstrumentor().instrument(tracer_provider=trace_provider)
client = anthropic.Anthropic(open_telemetry=False)
Install it with pip install traceAI-anthropic anthropic. SDK versions before 1.14.0 don’t accept open_telemetry; create the client without it.
Troubleshooting
- Two model spans for each request: a separate instrumentor and the SDK both trace the client. Remove the separate instrumentor when using native tracing. For an existing Python instrumentor setup, create the client with
open_telemetry=Falseinstead. - No spans: check that the client receives the provider in
open_telemetry(Python) oropenTelemetry(TypeScript), and that the script shuts down the provider before it exits. - JavaScript install fails with a peer dependency conflict: remove
@traceai/anthropicwithnpm uninstall @traceai/anthropic, then install the native SDK packages in step 1. Don’t use--forceor--legacy-peer-depsto combine the old adapter with SDK 1.x. - Missing streaming spans: read the stream to the end, or close it, before shutting down the provider.
Questions & Discussion