Observability for OpenAI (TypeScript) with Opik
Opik provides seamless integration with the official OpenAI Node.js SDK through the opik-openai package, allowing you to trace, monitor, and debug your OpenAI API calls.
Features
- Comprehensive Tracing: Automatically trace OpenAI API calls, including chat completions, embeddings, images, and more
- Hierarchical Visualization: View your OpenAI requests as structured traces with parent-child relationships
- Detailed Metadata Capture: Record model names, prompts, completions, token usage, and custom metadata
- Error Handling: Capture and visualize errors encountered during OpenAI API interactions
- Custom Tagging: Add custom tags to organize and filter your traces
- Streaming Support: Full support for streamed responses with token-by-token tracing
Installation
Option 1: Using npm
Option 2: Using yarn
Requirements
- Node.js ≥ 18
- OpenAI SDK (
openai≥ 6.0.1) - Opik SDK (
opikpeer dependency)
Basic Usage
Using with OpenAI Client
To trace your OpenAI API calls, you need to wrap your OpenAI client instance with the trackOpenAI function:
Using with Streaming Responses
The integration fully supports OpenAI’s streaming responses:
Using with the Decisions API
Calls to OpenAI’s Decisions API (openai ≥ 7.30.0) are traced too. Opik logs the input and questions as the span input, the returned answers as the span output, and the token usage.
Advanced Configuration
The trackOpenAI function accepts an optional configuration object to customize the integration:
Troubleshooting
Missing Traces: Ensure your OpenAI and Opik API keys are correct and that you’re calling await trackedOpenAI.flush() before your application exits.
Incomplete Data: For streaming responses, make sure you’re consuming the entire stream before ending your application.
Hierarchical Traces: To create proper parent-child relationships, use the parent option in the configuration when you want OpenAI calls to be children of another trace.
Performance Impact: The Opik integration adds minimal overhead to your OpenAI API calls.