Anthropic Integration
The TypeScript SDK provides automatic instrumentation for Anthropic’s Claude API using OpenTelemetry.How It Works
When you initialize withinstrumentModules: { anthropic: Anthropic }
:
- The SDK instruments the Anthropic client using OpenTelemetry
- Every API call is automatically captured as an Event
- Token usage and costs are calculated automatically
- Both standard and streaming responses are tracked
Setup
What Gets Captured
For every Anthropic API call:- Input: Messages and system prompts
- Model: Claude model used (claude-3-opus, claude-3-5-sonnet, etc.)
- Output: Complete response content
- Token Usage: Input and output token counts
- Cost: Calculated based on current pricing
- Timing: Request duration
- Images: Automatically handled for vision requests
Known Limitation: Streaming
Due to a limitation in the@traceloop/instrumentation-anthropic
library, streaming responses show as “Response received” instead of the actual content.
Workaround for streaming:
Examples
Basic Message
With System Prompt
Vision Requests
Multi-turn Conversation
Minimal Example
Cost Tracking
The SDK includes built-in pricing for all Claude models:Model | Input Cost | Output Cost |
---|---|---|
Claude 3 Opus | $0.015/1K | $0.075/1K |
Claude 3.5 Sonnet | $0.003/1K | $0.015/1K |
Claude 3 Haiku | $0.00025/1K | $0.00125/1K |
Claude 4 | $0.032/1K | $0.096/1K |
Advanced Usage
Temperature and Other Parameters
Error Handling
Using with Steps
Troubleshooting
Events Not Appearing
- Check import order - Anthropic must be imported AFTER
lai.init()
- Verify initialization - Ensure
instrumentModules
included the Anthropic module
Streaming Content Missing
This is a known limitation. Use the manual workaround shown above for critical streaming use cases.Custom Models
For fine-tuned or custom models:See Also
- OpenAI Integration - GPT tracking
- Advanced Features - Multimodal, masking
- API Reference - Complete documentation