Seamlessly integrate Opik observability with your LangChain applications to trace, monitor, and debug your LLM chains, agents, and tools.
- 🔍 Comprehensive Tracing: Automatically trace LLM calls, chains, tools, retrievers, and agents
- 📊 Hierarchical Visualization: View your LangChain execution as a structured trace with parent-child relationships
- 📝 Detailed Metadata Capture: Record model names, prompts, completions, usage statistics, and custom metadata
- 🚨 Error Handling: Capture and visualize errors at every step of your LangChain execution
- 🏷️ Custom Tagging: Add custom tags to organize and filter your traces
# npm
npm install opik-langchain
# yarn
yarn add opik-langchain
# pnpm
pnpm add opik-langchain
- Node.js ≥ 18
- LangChain (
@langchain/core
≥ 0.3.42) - Opik SDK (automatically installed as a dependency)
import { OpikCallbackHandler } from "opik-langchain";
import { ChatOpenAI } from "@langchain/openai";
// Create the Opik callback handler
const opikHandler = new OpikCallbackHandler();
// Create your LangChain components with the handler
const llm = new ChatOpenAI({
callbacks: [opikHandler],
});
// Run LLM
const response = await llm.invoke("Hello, how can you help me today?", {
callbacks: [opikHandler],
});
// Optionally, ensure all traces are sent before your app terminates
await opikHandler.flushAsync();
The OpikCallbackHandler
constructor accepts the following options:
interface OpikCallbackHandlerOptions {
// Optional array of tags to apply to all traces
tags?: string[];
// Optional metadata to include with all traces
metadata?: Record<string, unknown>;
// Optional project name for Opik
projectName?: string;
// Optional pre-configured Opik client
client?: Opik;
}
You can pass custom metadata when invoking your chains:
const response = await chain.invoke(
{ input: "Tell me about AI" },
{
callbacks: [opikHandler],
metadata: {
userId: "user-123",
sessionId: "session-456",
requestId: "req-789",
},
}
);
To view your traces:
- Sign in to your Comet account
- Navigate to the Opik section
- Select your project to view all traces
- Click on a specific trace to see the detailed execution flow
Apache 2.0