TypeScript Integration Guide
LangWatch library is the easiest way to integrate your TypeScript application with LangWatch, the messages are synced on the background so it doesn’t intercept or block your LLM calls.
Prerequisites
- Obtain your
LANGWATCH_API_KEY
from the LangWatch dashboard.
Installation
Configuration
Ensure LANGWATCH_API_KEY
is set:
Initialize LangWatch client:
Capturing Messages
- 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
Create a Trace
Capture an LLM Span
To capture your LLM calls, you can start an LLM span inside the trace with the input about to be sent to the LLM.
First, define the model and the messages you are going to use for your LLM call separately, so you can capture them:
Then, start the LLM span from the trace, giving it the model and input messages:
This will capture the LLM input and register the time the call started. Now, continue with the LLM call normally, using the same parameters:
Finally, after the OpenAI call is done, end the span to get the finish timestamp to be registered, and capture the output and the token metrics, which will be used for cost calculation:
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:
Then, after doing the retrieval, you can end the RAG span with the contexts that were retrieved and will be used by the LLM:
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:
You can also nest spans one inside the other, capturing your pipeline structure, for example:
Both LLM and RAG spans can also be nested like any arbritary span.
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:
The evaluation 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.