Latency and Usage Metrics
The server times every turn of a call and counts what it consumes, with nothing to configure. A Metrics event tells you, turn by turn, how long the transcription, the agent and the voice each took, and the End event sums the call up. Measuring this way costs a few numbers per turn, so it stays on in production. The tracing tutorial reads these numbers in a tracing backend.
Listen to each turn
import { MicdropServer } from '@micdrop/server'
const server = new MicdropServer(socket, { stt, agent, tts })
server.on('Metrics', (turn) => { console.log(`Turn ${turn.index}: ${turn.latency.total} ms of silence`, { stt: turn.latency.stt, llm: turn.latency.llm, tts: turn.latency.tts, interrupted: turn.interrupted, tokens: turn.usage.inputTokens, })})A turn is over when its answer has been heard, skipped or cut short, and that is when Metrics fires. The server estimates when the voice ends from the length of the audio it sent, so the event comes a moment after the assistant stops talking. A turn the user speaks over is reported as soon as they start.
Where the time goes
turn.latency splits the silence the user hears into the steps that make it, in ms. A step the pipeline did not run is left out.
| Field | From | To |
|---|---|---|
stt | The end of the user speech | The final transcript. 0 when a streaming speech to text had it ready before the turn closed |
llm | The agent being asked for an answer | Its first piece of text |
tts | That first piece of text | The first chunk of voice |
total | The end of the user speech | The first chunk of voice sent to the far end |
total is the number to watch. It holds more than the three steps above it: the wait of a classifier with waitBeforeAnswer, for instance, sits between the transcript and the agent.
Each step also has its timestamp in turn.timestamps, in ms since the epoch: start, userStart, userStop, transcript, answerStart, firstToken, answerEnd, firstAudio, lastAudio and end. They place a turn on a timeline, which is how @micdrop/otel draws its spans.
With a realtime model, one model hears and speaks, so only total is measured.
What a turn consumed
turn.usage counts what the providers are billed on:
| Field | What it counts |
|---|---|
inputAudioSeconds | User audio sent to the speech to text, or to the realtime model |
outputAudioSeconds | Assistant voice sent to the far end |
ttsCharacters | Characters of text sent to the text to speech |
inputTokens, outputTokens, cachedInputTokens | Tokens of the model, when the agent reports them |
The agents of @micdrop/openai, @micdrop/mistral, @micdrop/gemini and @micdrop/ai-sdk report their tokens, and so do OpenAI Realtime and Gemini Live. turn.model then names the model that answered. A custom agent reports its own with reportUsage().
The other fields
trigger says who opened the turn: user when they spoke, assistant for the first message or an answer your code asked for with answer() or speak(). Only the turns of the user have a total, since nobody waited for the others.
interrupted is true when the user spoke again, or muted, before the answer was over. A turn that ends with the call while the assistant still speaks counts as interrupted too.
contextLength is the number of conversation items the agent was given for the answer, the system prompt included.
What the agent read and wrote
The Metrics event carries a second argument when the agent generated the answer: input, the conversation items it was given, and output, what it added, its messages, tool calls and their results. Both hold the objects of the conversation of the agent that answered, so they stay right after an agent handoff changed the conversation of the call. They stay on the server: forwardMetrics() sends the metrics alone.
server.on('Metrics', (turn, content) => { if (!content) return console.log(`Turn ${turn.index} read ${content.input.length} items`) console.log('It wrote', content.output)})The whole call
The End event carries the metrics of the call next to its conversation:
server.on('End', ({ conversation, metrics }) => { console.log(`${metrics.turns} turns in ${metrics.duration} ms`) console.log(`Median silence: ${metrics.latency.total?.median} ms`) console.log(`p95 silence: ${metrics.latency.total?.p95} ms`) console.log(`Interrupted ${metrics.interruptions} times`) console.log(`Tokens: ${metrics.usage.inputTokens} in, ${metrics.usage.outputTokens} out`)})Each latency of metrics.latency holds the count of turns that measured it, with their average, median, p95 and max. metrics.usage adds up everything the call consumed, outside the turns included.
Show the metrics in the client
The metrics stay on the server unless you send them. forwardMetrics() sends those of every turn to the client:
import { forwardMetrics, MicdropServer } from '@micdrop/server'
const server = new MicdropServer(socket, { stt, agent, tts })forwardMetrics(server)The client then emits them as Metrics:
import { Micdrop } from '@micdrop/client'
Micdrop.on('Metrics', (turn) => { showLatency(turn.latency.total)})In React, useMicdropMetrics subscribes to them. The devtools panel draws them as a timeline of the call, a waterfall per turn. A client older than this event ignores the message, so forwarding is safe whatever version your users run. Keep it for your own dashboards and debugging pages: what a call consumes has no place in the client of a public app.