Global Instrumentation
The recommended approach is to use global instrumentation withinstrument(). This patches SDK prototypes at startup, automatically tracking all client instances.
instrument() Options
Return Value
Per-Instance Instrumentation
For more control, usemakeMeteredOpenAI() to wrap individual client instances:
Checking Instrumentation Status
Removing Instrumentation
Provider-Specific Instrumentation
Instrument providers individually for fine-grained control:Fetch Instrumentation
For frameworks like Vercel AI SDK that use fetch directly:Lifecycle Management
1
Startup
Call
instrument() before creating any SDK clients:2
Runtime
Instrumentation persists for the application lifetime. New client instances are automatically tracked.
3
Shutdown
Clean up in your shutdown handler:
Framework Integration
Express.js
Next.js
Best Practices
Do's and Don'ts
Do's and Don'ts
Do:
- Call
instrument()once at application startup - Pass SDK classes directly:
sdks: { OpenAI } - Clean up with
uninstrument()on shutdown - Use
getContextIdfor per-user tracking
- Call
instrument()multiple times - Create SDK clients before calling
instrument() - Import SDK clients in the same file as
instrument()(use dynamic imports)