Installation

You can install the opik package using your favorite package manager.

$npm install opik

Opik Configuration

You can configure the Opik client using environment variables.

$export OPIK_API_KEY="your-api-key"
>
># If running on Opik Cloud
>export OPIK_URL_OVERRIDE="https://www.comet.com/opik/api"
>
># If running locally
>export OPIK_URL_OVERRIDE="http://localhost:5173/api"
>
>export OPIK_PROJECT_NAME="your-project-name"
>export OPIK_WORKSPACE_NAME="your-workspace-name"

Or you can pass the configuration to the Opik client constructor.

1import { Opik } from "opik";
2
3const client = new Opik({
4 apiKey: "<your-api-key>",
5 apiUrl: "https://www.comet.com/opik/api",
6 projectName: "<your-project-name>",
7 workspaceName: "<your-workspace-name>",
8});

Usage

You can find the full Typescript reference documentation here.

1import { Opik } from "opik";
2
3// Create a new Opik client with your configuration
4const client = new Opik();
5
6// Log 10 traces
7for (let i = 0; i < 10; i++) {
8 const someTrace = client.trace({
9 name: `Trace ${i}`,
10 input: {
11 prompt: `Hello, world! ${i}`,
12 },
13 output: {
14 response: `Hello, world! ${i}`,
15 },
16 });
17
18 // For each trace, log 10 spans
19 for (let j = 0; j < 10; j++) {
20 const someSpan = someTrace.span({
21 name: `Span ${i}-${j}`,
22 type: "llm",
23 input: {
24 prompt: `Hello, world! ${i}:${j}`,
25 },
26 output: {
27 response: `Hello, world! ${i}:${j}`,
28 },
29 });
30
31 // Some LLM work
32 await new Promise((resolve) => setTimeout(resolve, 100));
33
34 // Mark the span as ended
35 someSpan.end();
36 }
37
38 // Mark the trace as ended
39 someTrace.end();
40}
41
42// Flush the client to send all traces and spans
43await client.flush();

Vercel AI SDK Integration

Opik provides seamless integration with the Vercel AI SDK through OpenTelemetry instrumentation.

Installation

Install the required dependencies:

$npm install opik ai @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

Usage

1import { openai } from "@ai-sdk/openai";
2import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
3import { NodeSDK } from "@opentelemetry/sdk-node";
4import { generateText } from "ai";
5import { OpikExporter } from "opik/vercel";
6
7const sdk = new NodeSDK({
8 traceExporter: new OpikExporter(),
9 instrumentations: [getNodeAutoInstrumentations()],
10});
11
12sdk.start();
13
14const { text } = await generateText({
15 model: openai("gpt-4o-mini"),
16 prompt: "What is love? Describe it in 10 words or less.",
17 experimental_telemetry: OpikExporter.getSettings({
18 name: "ai-sdk-integration",
19 }),
20});
21
22await sdk.shutdown();

This integration automatically captures:

  • Input prompts and messages
  • Model responses
  • Token usage statistics
  • Tool calls and their results
  • Timing information
  • Error states

All this telemetry data is automatically sent to your Opik project for analysis and monitoring.

Built with