Braintrust’s TypeScript SDK traces LiveKit Agents with auto-instrumentation, manual instrumentation, or OpenTelemetry.To trace LiveKit Agents without modifying your application code, run your agent with Braintrust’s import hook. Braintrust records LiveKit’s session, turn, and model activity directly through the Braintrust SDK, without requiring an OpenTelemetry pipeline. If your app also sets a LiveKit tracer provider that exports somewhere other than Braintrust, Braintrust leaves it in place.Auto-instrumentation and manual instrumentation trace sessions that use an STT-LLM-TTS pipeline.After a call ends, its trace appears on the Logs page of your project. The The auto-instrumentation example uses plain JavaScript so
Auto-instrumentation
Auto-instrumentation and manual instrumentation require Braintrust v3.43.0 or later and
@livekit/agents v1.9.1 to v1.9.x.Setup
These steps add Braintrust to an existing LiveKit Agents app. They trace each session and record its audio.1
Install Braintrust
Add
braintrust and @braintrust/audio to your agent’s project:braintrust: The Braintrust SDK, which traces your agent.@braintrust/audio: Compresses recordings and reduces upload size. We strongly recommend it.
2
Set your environment variables
Add your Braintrust API key and the two audio recording flags to your agent’s
.env file:.env
BRAINTRUST_API_KEY: Your Braintrust API key.BRAINTRUST_CAPTURE_USER_AUDIO_ATTACHMENTS: Records the caller’s audio.BRAINTRUST_CAPTURE_AGENT_AUDIO_ATTACHMENTS: Records the agent’s audio.
3
Initialize a logger and run your agent with the import hook
Initialize a Braintrust logger in your agent’s entry file, and flush it in a shutdown callback in your agent’s When a job ends, LiveKit Agents closes the session, which waits for the recording to finish uploading, and then exits the job process. Without the flush, the session’s last spans can be lost when the process exits.Then run the agent with Braintrust’s import hook:
entry function:--env-file loads your .env file and requires Node.js 20.6.0 or later. LiveKit runs each job in a child process that inherits these flags, so the hook also applies to your sessions.session root span holds the call recording, with turn, model, and speech spans nested under it. See What Braintrust traces for the full list.Example agent
This minimal pipeline agent uses separate speech-to-text, LLM, and text-to-speech models and is traced with the setup above. It uses the@livekit/agents-plugin-openai plugin and the LiveKit and OpenAI credentials from your .env file.livekit_agent.js
node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects. Compile your TypeScript to JavaScript and run the compiled file with the import hook, or run TypeScript directly with a TypeScript loader.If you’re using a bundler, see Trace LLM calls for plugin and loader setup.
Set up manually
Set up manually
To trace LiveKit without the import hook, wrap the Register the same shutdown callback as in Setup so the session’s last spans are sent before the job process exits.
@livekit/agents module with wrapLiveKit() before you create sessions. Sessions created through the wrapped module’s voice.AgentSession are fully traced, including speech-to-text spans, and call audio when audio recording is on.TypeScript
Opt out of LiveKit Agents tracing
Opt out of LiveKit Agents tracing
If you use auto-instrumentation for other libraries but want to exclude LiveKit Agents, add
livekit to the BRAINTRUST_DISABLE_INSTRUMENTATION environment variable. This also disables wrapLiveKit().What Braintrust traces
Auto-instrumentation andwrapLiveKit() produce the same spans:Other LiveKit span attributes are recorded in
metadata, with lk.* keys renamed to contrib.livekit.*. LLM and text-to-speech calls that LiveKit makes inside a traced session are recorded in the llm_response and tts spans, not as separate provider spans such as OpenAI spans.When LiveKit withholds personal data
When LiveKit withholds personal data
LiveKit withholds personal data from its telemetry when either of these is true:
LIVEKIT_TELEMETRY_ALLOW_PIIis set tofalse.- LiveKit Cloud project redaction is on.
Audio attachments
Braintrust can record each call and attach the recording to thesession span. Recording is off by default.Turn on recording
Turn on recording
Each side of the call has its own environment variable, which takes
1 or true:- Caller’s side:
BRAINTRUST_CAPTURE_USER_AUDIO_ATTACHMENTSrecords the caller’s audio that the session receives. - Agent’s side:
BRAINTRUST_CAPTURE_AGENT_AUDIO_ATTACHMENTSrecords the agent’s audio that played.
How recordings are stored
How recordings are stored
Braintrust attaches the recording to the
session span as 24 kHz, two-channel audio:- Channels: The caller is on the first channel and the agent is on the second. If you record only one side, the other channel is silent.
- Attachments: Braintrust splits the recording into attachments that each hold about 4 MiB of raw PCM, about 44 seconds of audio. Compressed attachments cover the same duration in fewer bytes.
- Format: The
@braintrust/audiopackage compresses recordings to Opus audio in Ogg files. It requires Node.js 20 or later. Braintrust finds and loads the package fromnode_modulesautomatically, so you don’t import it in your code. Without the package, recordings upload as raw PCM (audio/pcm, 16-bit little-endian interleaved samples, without a WAV header). Recordings also fall back to raw PCM, with a warning, when the package fails to load, doesn’t match yourbraintrustversion, or theaudiooptions contain an unsupported value. - Speech spans: Speech spans link to their place in the recording.
- End of the call:
session.close()resolves only after the recording finishes uploading. If you close the session yourself, awaitsession.close().
Customize recording
Customize recording
The default settings work without any Load the file before the hook:Bundled deployments. If you deploy a bundle without
configureInstrumentation() call. To change them, call configureInstrumentation() from braintrust/instrumentation in a separate file, and load that file before the hook. A call from a file that imports braintrust has no effect.Encoding. The encoding settings are optional tuning. To change them, pass audio options for the livekit integration:configure.mjs
codec.name:"opus", the default, or"pcm"to always record raw PCM, even when@braintrust/audiois installed.codec.options.bitrate: Target bitrate in whole bits per second, from500to512000. Defaults to64000.codec.options.complexity: Encoder effort, from0(fastest) to10. Defaults to5.codec.options.application: Encoder tuning:"audio", the default,"voip"(favors speech), or"lowdelay".container.name: File format for Opus audio."ogg", the default, is the only value.
streamingAttachmentBufferBytes:configure.mjs
node_modules, import the package in your app so your bundler includes it:Tracing resources
- LiveKit Agents overview: LiveKit Agents documentation
- Braintrust package on npm
@braintrust/audiopackage on npm
OpenTelemetry
Braintrust attaches to LiveKit’s built-in OpenTelemetry pipeline by registering a span processor with LiveKit’s tracer provider.Setup
1
Install dependencies
@livekit/agents is built against OpenTelemetry JS 1.x. Keep every OpenTelemetry package on its 1.x-compatible version, as pinned above. Upgrading any of them to 2.x breaks LiveKit at build or run time. The OTLP exporter packages use a separate 0.x version sequence that moves in step with the stable 1.x packages, so @opentelemetry/exporter-trace-otlp-http pins to ^0.57 rather than ^1.2
Set your environment variables
.env
Configure tracing
Configure Braintrust’s span processor and set it as LiveKit’s tracer provider. KeepfilterAISpans set to false so LiveKit’s session, speech, and turn spans are included.livekit_agent.ts
What Braintrust traces
These spans come from LiveKit’s native OpenTelemetry instrumentation. With the OpenAI realtime model, the LLM, speech-to-text, and text-to-speech work happens inside a single realtime model call rather than as separate spans.- Session spans (
agent_session), the root span covering the full agent session - Agent turn spans (
agent_turn), with the model request, token usage, and realtime model metrics (gen_ai.*,lk.realtime_model_metrics) - User speaking spans (
user_speaking), with participant and speech detection details - Agent speaking spans (
agent_speaking), covering the agent’s spoken response - Activity lifecycle spans (
start_agent_activity,on_enter,on_exit,drain_agent_activity)
Tracing resources
- OpenTelemetry Integration: Braintrust’s OpenTelemetry support
- Trace application logic: Add custom spans and attributes to your traces
- LiveKit Agents overview: LiveKit Agents documentation