Skip to main content
LiveKit Agents is a framework for building real-time voice and video AI applications. Braintrust traces LiveKit Agents applications to capture voice interactions, agent sessions, and realtime model usage.
Braintrust’s TypeScript SDK traces LiveKit Agents with auto-instrumentation, manual instrumentation, or OpenTelemetry.

Auto-instrumentation

Auto-instrumentation and manual instrumentation require Braintrust v3.43.0 or later and @livekit/agents v1.9.1 to v1.9.x.
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.

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.
Audio recording is off by default. Without these two variables, traces include spans and metadata but no 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 entry function:
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:
--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.
After a call ends, its trace appears on the Logs page of your project. The 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
The auto-instrumentation example uses plain JavaScript so 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.
To trace LiveKit without the import hook, wrap the @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
Register the same shutdown callback as in Setup so the session’s last spans are sent before the job process exits.
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 and wrapLiveKit() 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.
LiveKit withholds personal data from its telemetry when either of these is true:
  • LIVEKIT_TELEMETRY_ALLOW_PII is set to false.
  • LiveKit Cloud project redaction is on.
Braintrust then omits the same data, such as transcripts, messages, and tool arguments and results, and redacts exception details.

Audio attachments

Early development notice: LiveKit Agents audio capture and @braintrust/audio are in early development (alpha). Expect backwards-incompatible changes between releases.
Braintrust can record each call and attach the recording to the session span. Recording is off by default.
Each side of the call has its own environment variable, which takes 1 or true:
  • Caller’s side: BRAINTRUST_CAPTURE_USER_AUDIO_ATTACHMENTS records the caller’s audio that the session receives.
  • Agent’s side: BRAINTRUST_CAPTURE_AGENT_AUDIO_ATTACHMENTS records the agent’s audio that played.
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/audio package compresses recordings to Opus audio in Ogg files. It requires Node.js 20 or later. Braintrust finds and loads the package from node_modules automatically, 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 your braintrust version, or the audio options 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, await session.close().
The default settings work without any 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/audio is installed.
  • codec.options.bitrate: Target bitrate in whole bits per second, from 500 to 512000. Defaults to 64000.
  • codec.options.complexity: Encoder effort, from 0 (fastest) to 10. Defaults to 5.
  • 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.
Attachment size. To change the size of each attachment, set streamingAttachmentBufferBytes:
configure.mjs
Load the file before the hook:
Bundled deployments. If you deploy a bundle without node_modules, import the package in your app so your bundler includes it:

Tracing resources

OpenTelemetry

Braintrust attaches to LiveKit’s built-in OpenTelemetry pipeline by registering a span processor with LiveKit’s tracer provider.
Audio attachments aren’t supported with OpenTelemetry. To record call audio, use auto-instrumentation or manual instrumentation.

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. Keep filterAISpans 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