To instrument your application, use the register function from @arizeai/phoenix-otel and manually instrument the Anthropic SDK.Create the instrumentation.ts file:
import { register } from "@arizeai/phoenix-otel";import Anthropic from "@anthropic-ai/sdk";import { AnthropicInstrumentation } from "@arizeai/openinference-instrumentation-anthropic";// Initialize Phoenix tracingconst tracerProvider = register({ projectName: "anthropic-app", // If using Phoenix Cloud: // url: "https://app.phoenix.arize.com/s/your-space-name", // apiKey: process.env.PHOENIX_API_KEY, // If using self-hosted Phoenix: // url: "http://localhost:6006",});// Set up Anthropic instrumentationconst instrumentation = new AnthropicInstrumentation();instrumentation.manuallyInstrument(Anthropic);console.log("Anthropic instrumentation registered");
Import the instrumentation.ts file first, then use Anthropic as usual.
import "./instrumentation.js";import Anthropic from "@anthropic-ai/sdk";// set ANTHROPIC_API_KEY in environment, or pass it in argumentsconst anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY,});async function main() { const message = await anthropic.messages.create({ model: "claude-sonnet-4-20250514", max_tokens: 1024, messages: [{ role: "user", content: "Write a haiku about recursion." }], }); console.log(message.content);}main();
const instrumentation = new AnthropicInstrumentation();instrumentation.setTracerProvider(tracerProvider);instrumentation.manuallyInstrument(Anthropic);