Protip: wanna to get started even faster? Copy our llms.txt and ask an AI to do this integration
Prerequisites
- Obtain your
LANGWATCH_API_KEY
from the LangWatch dashboard.
Installation
Configuration
EnsureLANGWATCH_API_KEY
is set:
.env
Basic Concepts
- Each message triggering your LLM pipeline as a whole is captured with a Trace.
- A Trace contains multiple Spans, which are the steps inside your pipeline.
- Traces can be grouped together on LangWatch Dashboard by having the same
thread_id
in their metadata, making the individual messages become part of a conversation.- It is also recommended to provide the
user_id
metadata to track user analytics.
- It is also recommended to provide the
Integration
Start by setting up observability and initializing the LangWatch tracer:withActiveSpan
method to create an LLM span with automatic lifecycle management:
withActiveSpan
method automatically:
- Creates the span with the specified name
- Handles errors and sets appropriate span status
- Ends the span when the function completes
- Returns the result of your async function
Community Auto-Instrumentation
For automatic instrumentation without manual span creation, you can use the OpenInference instrumentation for OpenAI:1
Install the OpenInference instrumentation
2
Register the instrumentation
3
Use OpenAI normally
The OpenInference instrumentation automatically captures:
- Input messages and model configuration
- Output responses and token usage
- Error handling and status codes
- Request/response timing
When using auto-instrumentation, you may need to configure data capture settings to control what information is sent to LangWatch.
On short-live environments like Lambdas or Serverless Functions, be sure to call
await trace.sendSpans();
to wait for all pending requests to be sent before the runtime is destroyed.Capture a RAG Span
Appart from LLM spans, another very used type of span is the RAG span. This is used to capture the retrieved contexts from a RAG that will be used by the LLM, and enables a whole new set of RAG-based features evaluations for RAG quality on LangWatch. To capture a RAG, you can simply start a RAG span inside the trace, giving it the input query being used:On LangChain.js, RAG spans are captured automatically by the LangWatch callback when using LangChain Retrievers, with
source
as the documentId.Capture an arbritary Span
You can also use generic spans to capture any type of operation, its inputs and outputs, for example for a function call:Capturing Exceptions
To capture also when your code throws an exception, you can simply wrap your code around a try/catch, and update or end the span with the exception:Capturing custom evaluation results
LangWatch Evaluators can run automatically on your traces, but if you have an in-house custom evaluator, you can also capture the evaluation results of your custom evaluator on the current trace or span by using the.addEvaluation
method:
name
is required and must be a string. The other fields are optional, but at least one of passed
, score
or label
must be provided.
Related Documentation
For more advanced OpenAI integration patterns and best practices:- Integration Guide - Basic setup and core concepts
- Manual Instrumentation - Advanced span management for OpenAI calls
- Semantic Conventions - OpenAI-specific attributes and conventions
- Debugging and Troubleshooting - Debug OpenAI integration issues
- Capturing Metadata - Adding custom metadata to OpenAI calls
For production OpenAI applications, combine manual instrumentation with Semantic Conventions for consistent observability and better analytics.