Skip to main content
This cookbook provides practical examples for using the ABV JS/TS SDK.
JS/TS applications can be traced via the JS/TS SDK. In this notebook, we will walk you through a simple end-to-end example that:
  • Uses the core features of the ABV JS/TS SDK
  • Shows how to log any LLM call via the low-level SDK
For this guide, we assume that you are already familiar with the ABV data model (traces, spans, generations, etc.). If not, please read the conceptual introduction to tracing.

Set Up Environment

Get your ABV API key by signing up for ABV. You’ll also need your OpenAI API key. Note: This cookbook uses Deno.js for execution, which requires different syntax for importing packages and setting environment variables. For Node.js applications, the setup process is similar but uses standard npm packages and process.env. Add your ABV credentials to your environment variables. Make sure that you have a .env file in your project root and a package like dotenv to load the variables.
.env
With the environment variables set, we can now initialize the abvSpanProcessor which is passed to the main OpenTelemetry SDK that orchestrates tracing.
instrumentation.ts
The ABVClient provides additional functionality beyond OpenTelemetry tracing, such as scoring, prompt management, and data retrieval. It automatically uses the same environment variables we set earlier.

Log LLM Calls

You can use the SDK to log any LLM call or any of the integrations that are interoperable with it. In the following, we will demonstrate how to log LLM calls using the SDK, LangChain, Vercel AI SDK, and OpenAI integrations.

Option 1: Context Manager

To simplify nesting and context management, you can use startActiveObservation. These functions take a callback and automatically manage the observation’s lifecycle and the OpenTelemetry context. Any observation created inside the callback will automatically be nested under the active observation, and the observation will be ended when the callback finishes. This is the recommended approach for most use cases as it prevents context leakage and ensures observations are properly ended.
apps.ts
Public trace in the ABV UI

Option 2: observe Decorator

The observe wrapper is a powerful tool for tracing existing functions without modifying their internal logic. It acts as a decorator that automatically creates a span or generation around the function call. You can use the updateActiveObservation function to add attributes to the observation from within the wrapped function.
Public trace in the ABV UI

Option 3: Manual Spans

This part shows how to log any LLM call by passing the model in and outputs via the ABV SDK. Steps:
  1. Create span to contain this section within the trace
  2. Create generation, log input and model name as it is already known
  3. Call the LLM SDK and log the output
  4. End generation and span
Teams typically wrap their LLM SDK calls in a helper function that manages tracing internally. This implementation occurs once and is then reused for all LLM calls.
Public trace in the ABV UI

View the Trace in ABV

After ingesting your spans, you can view them in your ABV dashboard. in the ABV UI.

Learn More