Audio in Traces
Add audio to your messages and play it back on each message in a trace
Overview
A transcript tells you what was said on a call, but not how it sounded. With audio on your traces, you can listen to every turn of a call right where you debug it:
- Hear what the transcript hides. Catch speech-to-text mishearing the caller, the agent talking over them, or a reply that sounded wrong.
- Listen to each turn on its own. Every user and agent turn has its own player, so you can jump straight to the moment that went wrong instead of scrubbing through the whole call.
- Find where a turn went wrong. Each clip sits on the same trace as the model calls and tools behind it, so you can tell whether a bad answer started with what the agent heard or with what it decided.
- Replay the whole call. Press play on any message, and playback carries on through the rest of the conversation.
You can also run evals on live calls directly on their audio, not just the transcript, with workflows.
Add Audio to Messages
Wrap Each Clip in Media
Wrap each turn's audio in
Media. Typically that's:- User turn: the audio you sent to speech-to-text.
- Agent turn: the audio that came out of text-to-speech.
main.py from confident_trace import Media user_audio = Media.from_bytes(user_clip, "audio/wav") agent_audio = Media.from_bytes(agent_clip, "audio/mpeg")src/index.ts import { Media } from "confident-trace"; const userAudio = Media.fromBytes(userClip, "audio/wav"); const agentAudio = Media.fromBytes(agentClip, "audio/mpeg");When the trace is sent, Confident AI uploads each clip to its own storage and keeps a link to it on the message, so there's nothing for you to host.
A few rules:
- Always pass the media type. Bytes have no file extension to read it from.
- Convert raw PCM first. Write it into a WAV file, such as with Python's
wavemodule, or browsers can't play it. - Files work too.
Media.from_file(path)/Media.fromFile(path)reads the media type from the extension.
Attach It to a Message
Put each
Mediaon its message, under any key such asaudio,voiceorrecording. Confident AI finds it by its media type.main.py from confident_trace import Media, init, span, update_span, shutdown init() @span(type="agent") def voice_turn(user_clip: bytes, agent_clip: bytes): ... update_span( input={ "role": "user", "content": "What's my balance?", "audio": Media.from_bytes(user_clip, "audio/wav"), }, output={ "role": "assistant", "content": "Your balance is $42.", "audio": Media.from_bytes(agent_clip, "audio/mpeg"), }, ) try: voice_turn(user_clip, agent_clip) finally: shutdown()src/index.ts import { Media, init, span, updateSpan } from "confident-trace"; const runtime = init(); const voiceTurn = span({ name: "voice_turn", type: "agent" }, (userClip: Uint8Array, agentClip: Uint8Array) => { ... updateSpan({ input: { role: "user", content: "What's my balance?", audio: Media.fromBytes(userClip, "audio/wav"), }, output: { role: "assistant", content: "Your balance is $42.", audio: Media.fromBytes(agentClip, "audio/mpeg"), }, }); }); try { voiceTurn(userClip, agentClip); } finally { await runtime.shutdown(); }Keep each message's text in
content, so evaluations still read it as usual.Play It in the Trace
Open the trace in the Observatory, where each message with audio has its own player.
How Audio Is Stored
When a trace arrives, Confident AI uploads each clip and replaces it with a link, under the same key:
{
"role": "user",
"content": "What's my balance?",
"audio": { "mimeType": "audio/wav", "url": "https://…/audio/7c1e….wav" }
}- Only
mimeTypeandurlare kept. Other fields on the audio object, such as a transcript, are dropped. - Links to your own storage aren't copied. They're kept as they are.
- Players only appear on messages, meaning objects with a
roleandcontent. Audio anywhere else is still stored, but shows as aurlin the JSON view.
Add Audio to a Conversation
In Confident AI, a trace usually represents one turn of a conversation, and a thread groups those traces into the whole conversation. If that's how you trace, add audio to each trace's messages as in Add Audio to Messages.
Integrations emit traces differently, though, and some send an entire call as one trace. LiveKit and Pipecat both do; see Voice Framework Examples.
To put a whole conversation on one trace yourself, set a list of messages as the trace input. Each message with audio gets its own player:
from confident_trace import Media, init, span, update_trace, shutdown
init()
@span(type="agent")
def support_call():
...
update_trace(
input=[
{
"role": "user",
"content": "What's my balance?",
"audio": Media.from_file("turn-1-user.wav"),
},
{
"role": "assistant",
"content": "Your balance is $42.",
"audio": Media.from_file("turn-1-agent.mp3"),
},
{
"role": "user",
"content": "When is my next payment due?",
"audio": Media.from_file("turn-2-user.wav"),
},
],
output={
"role": "assistant",
"content": "Your next payment is due on the 1st.",
"audio": Media.from_file("turn-2-agent.mp3"),
},
)
try:
support_call()
finally:
shutdown()import { Media, init, span, updateTrace } from "confident-trace";
const runtime = init();
const supportCall = span({ name: "support_call", type: "agent" }, () => {
...
updateTrace({
input: [
{
role: "user",
content: "What's my balance?",
audio: Media.fromFile("turn-1-user.wav"),
},
{
role: "assistant",
content: "Your balance is $42.",
audio: Media.fromFile("turn-1-agent.mp3"),
},
{
role: "user",
content: "When is my next payment due?",
audio: Media.fromFile("turn-2-user.wav"),
},
],
output: {
role: "assistant",
content: "Your next payment is due on the 1st.",
audio: Media.fromFile("turn-2-agent.mp3"),
},
});
});
try {
supportCall();
} finally {
await runtime.shutdown();
}Pressing play on a message plays the rest of the conversation in order, so you can listen to it in one go.
- Pause with the player or the space bar.
- Only one player plays at a time.
- Messages on an LLM span also get players in the trace's Messages View.
Send Audio Without the SDK
Without confident-trace, use the same message shape. Send the audio as base64 under dataBase64, or as a link under url:
[
{
"role": "user",
"content": "What's my balance?",
"audio": { "mimeType": "audio/wav", "dataBase64": "UklGRiQAAABXQVZFZm10..." }
},
{
"role": "assistant",
"content": "Your balance is $42.",
"audio": { "mimeType": "audio/mpeg", "url": "https://example.com/answer.mp3" }
}
]- Any key works. Confident AI finds audio by its
audio/*media type, in trace and span inputs and outputs. dataBase64is uploaded and replaced with aurl. If you send both,dataBase64wins.
Audio Sent to a Model
If your agent sends audio straight to a model, such as an OpenAI input_audio part, its LLM span won't have a player. The integration doesn't keep the audio itself, only a note in its place, such as <inline_data: audio/wav, 12345 bytes>.
To hear that audio in the trace, attach it to a message yourself, as in Add Audio to Messages.
Voice Framework Examples
With LiveKit or Pipecat, you don't need to collect clips yourself. Turn on recording, and the integration uploads the whole call to its trace when the call ends. Each turn then plays its own part of the recording, so you get a player per turn with no extra code.
LiveKit
Pass record=True to session.start():
from confident_trace import init
from livekit.agents import AgentServer, JobContext, cli
init()
server = AgentServer()
@server.rtc_session()
async def entrypoint(ctx: JobContext):
await ctx.connect()
await session.start(room=ctx.room, agent=agent, record=True)
if __name__ == "__main__":
cli.run_app(server)import { fileURLToPath } from "node:url";
import { init } from "confident-trace";
import { cli, defineAgent, ServerOptions } from "@livekit/agents";
import type { JobContext } from "@livekit/agents";
init();
export default defineAgent({
entry: async (ctx: JobContext) => {
await ctx.connect();
await session.start({ room: ctx.room, agent, record: true });
},
});
cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url) }));session and agent are your existing AgentSession and agent. For size limits and when recordings are skipped, see LiveKit's Record Call Audio.
Pipecat
Add an AudioBufferProcessor after the transport output, so it hears both sides of the call:
from confident_trace import init
from pipecat.processors.audio.audio_buffer_processor import AudioBufferProcessor
from pipecat.pipeline.pipeline import Pipeline
from pipecat.pipeline.worker import PipelineParams, PipelineWorker
from pipecat.workers.runner import WorkerRunner
init()
async def bot(runner_args):
audio = AudioBufferProcessor(num_channels=2, auto_start_recording=True)
pipeline = Pipeline([
transport.input(), stt, user_aggregator, llm, tts,
transport.output(),
audio,
assistant_aggregator,
])
worker = PipelineWorker(
pipeline,
params=PipelineParams(enable_metrics=True),
conversation_id=runner_args.session_id,
)
runner = WorkerRunner(handle_sigint=runner_args.handle_sigint)
await runner.add_workers(worker)
await runner.run()
if __name__ == "__main__":
from pipecat.runner.run import main
main()transport,stt,llm,ttsand the aggregators are your existing services.num_channels=2puts the user on the left channel and the bot on the right.
For long calls, size limits and sample rates, see Pipecat's Record Call Audio.
Next Steps
Trace your voice agents end to end, or connect their turns into conversations.
LiveKit
Trace LiveKit voice agents, with call transcripts and recordings on every trace.
Pipecat
Trace Pipecat voice bots, with call transcripts and recordings on every trace.
Thread Traces
Group traces into threads to track multi-turn conversations and evaluate entire workflows.
Last updated on
