Protip: wanna to get started even faster? Copy our llms.txt and ask an AI to do this integration
Prerequisites
- Obtain your
LANGWATCH_API_KEYfrom the LangWatch dashboard.
Installation
Configuration
EnsureLANGWATCH_API_KEY is set:
- Environment variable
- Client parameters
.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_idin their metadata, making the individual messages become part of a conversation.- It is also recommended to provide the
user_idmetadata to track user analytics.
- It is also recommended to provide the
Installation
Usage
The LangWatch API key is configured by default via the
LANGWATCH_API_KEY environment variable.withActiveSpan to capture your OpenAI calls:
withActiveSpan method automatically creates the span, handles errors, and ends the span when the function completes.
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
- Capturing RAG - Learn how to capture RAG data from retrievers and tools
- Capturing Metadata and Attributes - Add custom metadata and attributes to your traces and spans
- Capturing Evaluations & Guardrails - Log evaluations and implement guardrails in your OpenAI applications