Documentation

Anthropic Example

Track Anthropic messages with token extraction and request telemetry.

Same pattern as OpenAI — extract model, tokens, and latency from Anthropic responses.

anthropic.ts typescript
import Anthropic from '@anthropic-ai/sdk'
import { PromptLayer } from '@promptlayerapp/js'

const anthropic = new Anthropic()
const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY!,
  projectId: process.env.PROMPTLAYER_PROJECT_ID!,
})

const startedAt = Date.now()

const message = await anthropic.messages.create({
  model: 'claude-haiku-4-5-20251001',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Draft a reply to the customer' }],
})

await promptlayer.trackRequest({
  provider: 'anthropic',
  model: 'claude-haiku-4-5-20251001',
  feature: 'customer_reply',
  requestId: message.id,
  inputTokens: message.usage.input_tokens,
  outputTokens: message.usage.output_tokens,
  latencyMs: Date.now() - startedAt,
  status: 'success',
})

await promptlayer.flush()

Usage mapping

Anthropic field PromptLayer field
usage.input_tokensinputTokens
usage.output_tokensoutputTokens
idrequestId