This page documents an older method of tracing AI SDK runs. For a simpler and more general method that does not require OTEL setup, see the new guide.
Many popular OpenTelemetry implementations in JavaScript are currently experimental,
and may behave erratically in production, especially when instrumenting LangSmith alongside other providers. If you are on AI SDK 5,
we strongly suggest using our recommended approach for tracing AI SDK runs.
0. Installation
Install the Vercel AI SDK and required OTEL packages. We use their OpenAI integration for the code snippets below, but you can use any of their other options as well.1. Configure your environment
2. Log a trace
Node.js
To start tracing, you will need to import and call theinitializeOTEL
method at the start of your code:
experimental_telemetry
argument to your AI SDK calls that you want to trace.
Do not forget to call
await DEFAULT_LANGSMITH_SPAN_PROCESSOR.shutdown();
before your application shuts down in order to flush any remaining traces to LangSmith.With traceable
You can wrap traceable
calls around or within AI SDK tool calls. If you do so, we recommend you initialize a LangSmith client
instance that you pass into each traceable
, then call client.awaitPendingTraceBatches();
to ensure all traces flush. If you do this, you do not need to manually call shutdown()
or forceFlush()
on the DEFAULT_LANGSMITH_SPAN_PROCESSOR
. Here’s an example:
Next.js
First, install the@vercel/otel
package:
instrumentation.ts
file in your root directory.
Call initializeOTEL
and pass the resulting DEFAULT_LANGSMITH_SPAN_PROCESSOR
into the spanProcessors
field into your registerOTEL(...)
call.
It should look something like this:
initializeOTEL
as well and add an experimental_telemetry
field to your AI SDK calls:
traceables
for more granularity.
Sentry
If you’re using Sentry, you can attach the LangSmith trace exporter to Sentry’s default OpenTelemetry instrumentation as show in the example below.At time of writing, Sentry only supports OTEL v1 packages. LangSmith supports both v1 and v2, but you must make sure you install OTEL v1 packages in order to make instrumentation work.
Add other metadata
You can add other metadata to your traces to help organize and filter them in the LangSmith UI:Customize run name
You can customize the run name by passing a metadata key namedls_run_name
into experimental_telemetry
.