Observability for LangChain with Opik

Opik provides seamless integration with LangChain, allowing you to easily log and trace your LangChain-based applications. By using the OpikTracer callback, you can automatically capture detailed information about your LangChain runs, including inputs, outputs, metadata, and cost tracking for each step in your chain.

Key Features

  • Automatic cost tracking for supported LLM providers (OpenAI, Anthropic, Google AI, AWS Bedrock, and more)
  • Full compatibility with the @opik.track decorator for hybrid tracing approaches
  • Thread support for conversational applications with thread_id parameter
  • Distributed tracing support for multi-service applications
  • LangGraph compatibility for complex graph-based workflows
  • Evaluation and testing support for automated LLM application testing

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

To use the OpikTracer with LangChain, you’ll need to have both the opik and langchain packages installed. You can install them using pip:

$pip install opik langchain langchain_openai

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Using OpikTracer

Here’s a basic example of how to use the OpikTracer callback with a LangChain chain:

1from langchain_openai import ChatOpenAI
2from langchain_core.prompts import ChatPromptTemplate
3from opik.integrations.langchain import OpikTracer
4
5# Initialize the tracer
6opik_tracer = OpikTracer(project_name="langchain-examples")
7
8llm = ChatOpenAI(model="gpt-4o", temperature=0)
9prompt = ChatPromptTemplate.from_messages([
10 ("human", "Translate the following text to French: {text}")
11])
12chain = prompt | llm
13
14result = chain.invoke(
15 {"text": "Hello, how are you?"},
16 config={"callbacks": [opik_tracer]}
17)
18print(result.content)

The OpikTracer will automatically log the run and its details to Opik, including the input prompt, the output, and metadata for each step in the chain.

For detailed parameter information, see the OpikTracer SDK reference.

Practical Example: Text-to-SQL with Evaluation

Let’s walk through a real-world example of using LangChain with Opik for a text-to-SQL query generation task. This example demonstrates how to create synthetic datasets, build LangChain chains, and evaluate your application.

Setting up the Environment

First, let’s set up our environment with the necessary dependencies:

1import os
2import getpass
3import opik
4from opik.integrations.openai import track_openai
5from openai import OpenAI
6
7# Configure Opik
8opik.configure(use_local=False)
9os.environ["OPIK_PROJECT_NAME"] = "langchain-integration-demo"
10
11# Set up API keys
12if "OPENAI_API_KEY" not in os.environ:
13 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Creating a Synthetic Dataset

We’ll create a synthetic dataset of questions for our text-to-SQL task:

1import json
2from langchain_community.utilities import SQLDatabase
3
4# Download and set up the Chinook database
5import requests
6
7url = "https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite"
8filename = "./data/chinook/Chinook_Sqlite.sqlite"
9
10folder = os.path.dirname(filename)
11if not os.path.exists(folder):
12 os.makedirs(folder)
13
14if not os.path.exists(filename):
15 response = requests.get(url)
16 with open(filename, "wb") as file:
17 file.write(response.content)
18 print("Chinook database downloaded")
19
20db = SQLDatabase.from_uri(f"sqlite:///{filename}")
21
22# Create synthetic questions using OpenAI
23client = OpenAI()
24openai_client = track_openai(client)
25
26prompt = """
27Create 20 different example questions a user might ask based on the Chinook Database.
28These questions should be complex and require the model to think. They should include complex joins and window functions to answer.
29Return the response as a json object with a "result" key and an array of strings with the question.
30"""
31
32completion = openai_client.chat.completions.create(
33 model="gpt-3.5-turbo",
34 messages=[{"role": "user", "content": prompt}]
35)
36
37synthetic_questions = json.loads(completion.choices[0].message.content)["result"]
38
39# Create dataset in Opik
40opik_client = opik.Opik()
41dataset = opik_client.get_or_create_dataset(name="synthetic_questions")
42dataset.insert([{"question": question} for question in synthetic_questions])

Building the LangChain Chain

Now let’s create a LangChain chain for SQL query generation:

1from langchain.chains import create_sql_query_chain
2from langchain_openai import ChatOpenAI
3from opik.integrations.langchain import OpikTracer
4
5# Create the LangChain chain with OpikTracer
6opik_tracer = OpikTracer(tags=["sql_generation"])
7
8llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
9chain = create_sql_query_chain(llm, db).with_config({"callbacks": [opik_tracer]})
10
11# Test the chain
12response = chain.invoke({"question": "How many employees are there?"})
13print(response)

Evaluating the Application

Let’s create a custom evaluation metric and test our application:

1from opik import track
2from opik.evaluation import evaluate
3from opik.evaluation.metrics import base_metric, score_result
4from typing import Any
5
6class ValidSQLQuery(base_metric.BaseMetric):
7 def __init__(self, name: str, db: Any):
8 self.name = name
9 self.db = db
10
11 def score(self, output: str, **ignored_kwargs: Any):
12 try:
13 db.run(output)
14 return score_result.ScoreResult(
15 name=self.name, value=1, reason="Query ran successfully"
16 )
17 except Exception as e:
18 return score_result.ScoreResult(name=self.name, value=0, reason=str(e))
19
20# Set up evaluation
21valid_sql_query = ValidSQLQuery(name="valid_sql_query", db=db)
22dataset = opik_client.get_dataset("synthetic_questions")
23
24@track()
25def llm_chain(input: str) -> str:
26 response = chain.invoke({"question": input})
27 return response
28
29def evaluation_task(item):
30 response = llm_chain(item["question"])
31 return {"output": response}
32
33# Run evaluation
34res = evaluate(
35 experiment_name="SQL question answering",
36 dataset=dataset,
37 task=evaluation_task,
38 scoring_metrics=[valid_sql_query],
39 nb_samples=20,
40)

The evaluation results are now uploaded to the Opik platform and can be viewed in the UI.

Cost Tracking

The OpikTracer automatically tracks token usage and cost for all supported LLM models used within LangChain applications.

Cost information is automatically captured and displayed in the Opik UI, including:

  • Token usage details
  • Cost per request based on model pricing
  • Total trace cost

View the complete list of supported models and providers on the Supported Models page.

For streaming with cost tracking, ensure stream_usage=True is set:

1from langchain_openai import ChatOpenAI
2from opik.integrations.langchain import OpikTracer
3
4llm = ChatOpenAI(
5 model="gpt-4o",
6 streaming=True,
7 stream_usage=True, # Required for cost tracking with streaming
8)
9
10opik_tracer = OpikTracer()
11
12for chunk in llm.stream("Hello", config={"callbacks": [opik_tracer]}):
13 print(chunk.content, end="")

View the complete list of supported models and providers on the Supported Models page.

Settings tags and metadata

You can customize the OpikTracer callback to include additional metadata, logging options, and conversation threading:

1from opik.integrations.langchain import OpikTracer
2
3opik_tracer = OpikTracer(
4 tags=["langchain", "production"],
5 metadata={"use-case": "customer-support", "version": "1.0"},
6 thread_id="conversation-123", # For conversational applications
7 project_name="my-langchain-project"
8)

Accessing logged traces

You can use the created_traces method to access the traces collected by the OpikTracer callback:

1from opik.integrations.langchain import OpikTracer
2
3opik_tracer = OpikTracer()
4
5# Calling Langchain object
6traces = opik_tracer.created_traces()
7print([trace.id for trace in traces])

The traces returned by the created_traces method are instances of the Trace class, which you can use to update the metadata, feedback scores and tags for the traces.

Accessing the content of logged traces

In order to access the content of logged traces you will need to use the Opik.get_trace_content method:

1import opik
2from opik.integrations.langchain import OpikTracer
3opik_client = opik.Opik()
4
5opik_tracer = OpikTracer()
6
7
8# Calling Langchain object
9
10# Getting the content of the logged traces
11traces = opik_tracer.created_traces()
12for trace in traces:
13 content = opik_client.get_trace_content(trace.id)
14 print(content)

Updating and scoring logged traces

You can update the metadata, feedback scores and tags for traces after they are created. For this you can use the created_traces method to access the traces and then update them using the update method and the log_feedback_score method:

1from opik.integrations.langchain import OpikTracer
2
3opik_tracer = OpikTracer(project_name="langchain-examples")
4
5# ... calling Langchain object
6
7traces = opik_tracer.created_traces()
8
9for trace in traces:
10 trace.update(tags=["my-tag"])
11 trace.log_feedback_score(name="user-feedback", value=0.5)

Compatibility with @track Decorator

The OpikTracer is fully compatible with the @track decorator, allowing you to create hybrid tracing approaches:

1import opik
2from langchain_openai import ChatOpenAI
3from opik.integrations.langchain import OpikTracer
4
5@opik.track
6def my_langchain_workflow(user_input: str) -> str:
7 llm = ChatOpenAI(model="gpt-4o")
8 opik_tracer = OpikTracer()
9
10 # The LangChain call will create spans within the existing trace
11 response = llm.invoke(user_input, config={"callbacks": [opik_tracer]})
12 return response.content
13
14result = my_langchain_workflow("What is machine learning?")

Thread Support

Use the thread_id parameter to group related conversations or interactions:

1from opik.integrations.langchain import OpikTracer
2
3# All traces with the same thread_id will be grouped together
4opik_tracer = OpikTracer(thread_id="user-session-123")

Distributed Tracing

For multi-service/thread/process applications, you can use distributed tracing headers to connect traces across services:

1from opik import opik_context
2from opik.integrations.langchain import OpikTracer
3from opik.types import DistributedTraceHeadersDict
4
5# In your service that receives distributed trace headers.
6# The distributed_headers dict can be obtained in the "parent" service via `opik_context.get_distributed_trace_headers()`
7distributed_headers = DistributedTraceHeadersDict(
8 opik_trace_id="trace-id-from-upstream",
9 opik_parent_span_id="parent-span-id-from-upstream"
10)
11
12opik_tracer = OpikTracer(distributed_headers=distributed_headers)
13
14# LangChain operations will be attached to the existing distributed trace
15chain.invoke(input_data, config={"callbacks": [opik_tracer]})
Learn more about distributed tracing in the Distributed Tracing guide.

LangGraph Integration

For LangGraph applications, Opik provides specialized support. The OpikTracer works seamlessly with LangGraph, and you can also visualize graph definitions:

1from langgraph.graph import StateGraph
2from opik.integrations.langchain import OpikTracer
3
4# Your LangGraph setup
5graph = StateGraph(...)
6compiled_graph = graph.compile()
7
8opik_tracer = OpikTracer()
9result = compiled_graph.invoke(
10 input_data,
11 config={"callbacks": [opik_tracer]}
12)
For detailed LangGraph integration examples, see the LangGraph Integration guide.

Advanced usage

The OpikTracer object has a flush method that can be used to make sure that all traces are logged to the Opik platform before you exit a script. This method will return once all traces have been logged or if the timeout is reach, whichever comes first.

1from opik.integrations.langchain import OpikTracer
2
3opik_tracer = OpikTracer()
4opik_tracer.flush()

Important notes

  1. Asynchronous streaming: If you are using asynchronous streaming mode (calling .astream() method), the input field in the trace UI may be empty due to a LangChain limitation for this mode. However, you can find the input data inside the nested spans of this chain.

  2. Streaming with cost tracking: If you are planning to use streaming with LLM calls and want to calculate LLM call tokens/cost, you need to explicitly set stream_usage=True: