Opik TypeScript and JavaScript SDK integration with LangChain
npm install opik-langchain

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
``bashnpm
npm install opik-langchain
$3
- Node.js ≥ 18
- LangChain (
@langchain/core ≥ 0.3.42)
- Opik SDK (automatically installed as a dependency)Quick Start
`typescript
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();
`Advanced Configuration
The
OpikCallbackHandler constructor accepts the following options:`typescript
interface OpikCallbackHandlerOptions {
// Optional array of tags to apply to all traces
tags?: string[]; // Optional metadata to include with all traces
metadata?: Record;
// Optional project name for Opik
projectName?: string;
// Optional pre-configured Opik client
client?: Opik;
}
`$3
You can pass custom metadata when invoking your chains:
`typescript
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:
1. Sign in to your Comet account
2. Navigate to the Opik section
3. Select your project to view all traces
4. Click on a specific trace to see the detailed execution flow
- Opik Documentation
- LangChain Documentation
- Opik TypeScript SDK
Apache 2.0