Streaming Multimodal Interaction › Querying the Trace: The Five Streaming Metrics
Language AI HandbookAll chapters A query selects events by kind and, where applicable, turn or generation. An absent event yields no observed duration. Duplicate matches are an ambiguous trace, and a negative duration indicates a broken pairing or clock alignment. The code raises on those last two cases.
The availability of a measurement depends on the endpoint. If a response was canceled before useful audio, its useful-response latency is undefined. Keep the cancellation in the denominator when reporting completion and report latency among episodes with observed useful audio. For a fixed service target, count episodes that supplied useful audio on time alongside those still waiting at the deadline. A table that drops cancelled episodes can look faster while withholding useful responses more often.
The example reports a server first-token time, two listener-facing response times, an audible interruption time, and a stale-audio span. The interval from cancel request to acknowledgment is recorded as an extra diagnostic. The stale-audio span assumes generation 7 is continuously audible between player invalidation and stop. With gaps, sum the old-generation playback intervals instead of treating the whole span as sound.
PythonIn [5]Expand
def find_event(trace, kind, *, turn=None, generation=None):
matches = [
e
for e in trace
if e.kind == kind
and (turn is None or e.turn == turn)
and (generation is None or e.generation == generation)
]
if len(matches) > 1:
raise ValueError(f"Ambiguous event: {kind}")
return matches[0] if matches else None
def event_time(trace, kind, *, turn=None, generation=None):
event = find_event(trace, kind, turn=turn, generation=generation)
return None if event is None else event.t_ms
def elapsed(start, end):
if start is None or end is None:
return None
if end < start:
raise ValueError("Endpoint precedes start")
return end - start
end = event_time(trace, "speech_end_annotated", turn=1)
started = event_time(trace, "prefill_started", generation=7)
first = event_time(trace, "first_token", generation=7)
heard = event_time(trace, "audio_started", generation=7)
useful = event_time(trace, "useful_audio_completed", generation=7)
interruption = event_time(trace, "speech_onset", turn=2)
invalidation = event_time(trace, "generation_invalidated", turn=2, generation=7)
request = event_time(trace, "cancel_requested", generation=7)
ack = event_time(trace, "cancel_ack", generation=7)
stopped = event_time(trace, "audio_stopped", generation=7)
metrics = {
"model TTFT": elapsed(started, first),
"end to first audible output": elapsed(end, heard),
"end to first useful audio": elapsed(end, useful),
"cancel request to acknowledgment": elapsed(request, ack),
"audible barge-in": elapsed(interruption, stopped),
"stale audible output after invalidation": elapsed(invalidation, stopped),
}ConsoleOut [6]Expand
model TTFT: 290 ms end to first audible output: 730 ms end to first useful audio: 900 ms cancel request to acknowledgment: 60 ms audible barge-in: 140 ms stale audible output after invalidation: 110 ms
The first decoded token occurs 290 ms after prefill starts. From the end of the user turn, the first sound comes after 730 ms and the useful phrase finishes after 900 ms. The 170 ms difference is defined by playback, even when some of that time is silence. It does not claim that filling that time with words makes the material useful sooner.
VisualizationOut [8]Expand

The trace records 60 ms from cancel request to acknowledgment. It does not distinguish transmission from server processing. The user hears the old generation for 140 ms after interruption onset and 110 ms after the player invalidates generation 7. The renderer discards 2,400 queued samples at 24 kHz. Their 100 ms duration is recorded separately from what was heard. The lookup uses the interrupted turn and generation. This fixture records one aggregate discard event per cancellation. Log multiple device discard batches separately, then combine their sample counts at a common rate before recording such a summary. A missing summary leaves the discarded duration unobserved.
PythonIn [9]Expand
def allow_chunk(chunk_generation, current_generation):
return (
current_generation is not None
and chunk_generation == current_generation
)
def sample_duration_ms(count, sample_rate_hz):
if count < 0 or sample_rate_hz <= 0:
raise ValueError("Samples must be nonnegative and sample rate positive")
return 1000 * count / sample_rate_hz
discarded_event = find_event(
trace, "queued_samples_discarded", turn=2, generation=7
)
if discarded_event is None:
discarded_ms = None
else:
if type(discarded_event.payload) is not int:
raise ValueError("Expected an integer count of discarded samples")
discarded_ms = sample_duration_ms(discarded_event.payload, 24_000)
assert allow_chunk(7, 7) and not allow_chunk(7, 8) and allow_chunk(8, 8)
assert not allow_chunk(None, None) and not allow_chunk(7, None)ConsoleOut [10]Expand
Discarded queued audio at 24 kHz: 100 ms
A Latency Distribution
A percentile is an observed threshold indexed by rank in the sample. In the nearest-rank convention, select element in a sorted list of values, indexed from one. A shifted gamma draw provides 2,000 reproducible right-skewed values. Those values are a separate synthetic cohort and do not come from rerunning the single trace.
PythonIn [11]Expand
def nearest_rank(latencies_ms, q):
if not latencies_ms or not 0 < q <= 1:
raise ValueError("Provide observations and a quantile in (0, 1]")
return sorted(latencies_ms)[ceil(q * len(latencies_ms)) - 1]
rng = np.random.default_rng(7)
ttft_samples = (
(rng.gamma(shape=2.0, scale=180.0, size=2_000) + 400).astype(int).tolist()
)ConsoleOut [12]Expand
Sample count: 2000 q=0.50: 687 ms q=0.95: 1251 ms q=0.99: 1603 ms q=1.00: 2188 ms
VisualizationOut [14]Expand

Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!