Skip to main content

Overview

This guide shows you how to integrate Latitude Telemetry into an existing application that uses the official Anthropic SDK. After completing these steps:
  • Every Anthropic call (e.g. messages.create) can be captured as a log in Latitude.
  • Logs are attached to a specific prompt and version in Latitude.
  • You can annotate, evaluate, and debug your Anthropic-powered features from the Latitude dashboard.
You’ll keep calling Anthropic directly — Telemetry simply observes and enriches those calls.

Requirements

Before you start, make sure you have:
  • A Latitude account and API key.
  • At least one prompt created in Latitude (so you have a promptUuid and versionUuid to associate logs with).
  • A Node.js-based project that uses the Anthropic SDK (@anthropic-ai/sdk).

Steps

1

Install requirements

Add the Latitude Telemetry package to your project:
npm add @latitude-data/telemetry @opentelemetry/api
2

Initialize Latitude Telemetry with Anthropic

Create a LatitudeTelemetry instance and pass the Anthropic SDK as an instrumentation.
import { LatitudeTelemetry } from '@latitude-data/telemetry'
import * as Anthropic from '@anthropic-ai/sdk'

export const telemetry = new LatitudeTelemetry('your-latitude-api-key', {
  instrumentations: {
    anthropic: Anthropic, // Enables automatic tracing for the Anthropic SDK
  },
})
3

Wrap your Anthropic-powered feature

Wrap the code that calls Anthropic with a Telemetry prompt span, and execute your Anthropic call inside that span.
import { context } from '@opentelemetry/api'
import { BACKGROUND } from '@latitude-data/telemetry'
import Anthropic from "@anthropic-ai/sdk";

export async function generateSupportReply(input: string) {
  const $prompt = telemetry.prompt(BACKGROUND(), {
    promptUuid: 'your-prompt-uuid',
    versionUuid: 'your-version-uuid',
  })

  await context
    .with($prompt.context, async () => {
      const client = new Anthropic({
        apiKey: process.env.ANTHROPIC_API_KEY!,
      })

      const response = await client.messages.create({
        model: 'claude-3-5-sonnet-20240620',
        max_tokens: 1024,
        messages: [
          {
            role: 'user',
            content: input,
          },
        ],
      })

      // Use response here...
    })
    .then(() => $prompt.end())
    .catch((error) => $prompt.fail(error as Error))
    .finally(() => telemetry.flush())
}

Seeing your logs in Latitude

Once you’ve wrapped your Anthropic-powered feature, you can see your logs in Latitude.
  1. Go to the Traces section of your prompt in Latitude.
  2. You should see new entries every time your code is executed, including:
    • Input/output messages
    • Model name
    • Latency and error information