user_bot_latency_observer
Observer for tracking user-to-bot response latency.
This module provides an observer that monitors the time between when a user
stops speaking and when the bot starts speaking, emitting events when latency
is measured. When enable_metrics=True it also collects a breakdown of the
interval: per-service metrics (TTFB, text aggregation), and contributions that
name each part of the timeline, including the parts no service measures.
- class pipecat.observers.user_bot_latency_observer.TTFBBreakdownMetrics(*, processor: str, model: str | None = None, start_time: float, duration_secs: float)[source]
Bases:
BaseModelTTFB measurement with timestamp for timeline placement.
- Parameters:
processor – Name of the processor that reported the TTFB.
model – Optional model name associated with the metric.
start_time – Unix timestamp when the TTFB measurement started.
duration_secs – TTFB duration in seconds.
- class pipecat.observers.user_bot_latency_observer.TextAggregationBreakdownMetrics(*, processor: str, start_time: float, duration_secs: float)[source]
Bases:
BaseModelText aggregation measurement with timestamp for timeline placement.
- Parameters:
processor – Name of the processor that reported the metric.
start_time – Unix timestamp when text aggregation started.
duration_secs – Aggregation duration in seconds.
- class pipecat.observers.user_bot_latency_observer.FunctionCallMetrics(*, function_name: str, start_time: float, duration_secs: float)[source]
Bases:
BaseModelLatency for a single function call execution.
- Parameters:
function_name – Name of the function that was called.
start_time – Unix timestamp when execution started.
duration_secs – Time in seconds from execution start to result.
- class pipecat.observers.user_bot_latency_observer.MeasuredFrom(*values)[source]
Bases:
StrEnumWhere a measured interval was anchored.
A greeting is timed from the client connecting, a turn from the user falling silent, so a partial interval is never mistaken for a whole one.
- USER_SILENCE = 'user_silence'
- CLIENT_CONNECTED = 'client_connected'
- class pipecat.observers.user_bot_latency_observer.LatencyOwnerKind(*values)[source]
Bases:
StrEnumWhat kind of thing a contribution’s owner is.
Grouping on this answers where a turn’s time went without matching on the owner’s name: a service that could be swapped, a setting the bot chose, the bot’s own code, or the pipeline between them.
- SERVICE = 'service'
- SETTING = 'setting'
- BOT = 'bot'
- PIPELINE = 'pipeline'
- class pipecat.observers.user_bot_latency_observer.LatencyContribution(*, key: str, label: str, owner: str, owner_kind: LatencyOwnerKind, start_time: float, duration_secs: float)[source]
Bases:
BaseModelOne named part of the user-to-bot interval.
Contributions account for the whole interval, so their durations sum to the measured latency. Unlike a TTFB, which reports what a service spent once it was asked, a contribution can also name time no service is measuring: the silence a VAD waits out, the tokens a turn-completion marker occupies before any speakable text, or a hold while the pipeline waits for the user to finish a sentence.
- Parameters:
key – Stable identifier for this part of a turn. Unlike
label, it is safe to group on: it survives the label being reworded.label – What the time was spent on.
owner – What spent it — a processor name, or a
config:tag naming the setting that governs it.owner_kind – What kind of thing the owner is.
start_time – Unix timestamp when it started.
duration_secs – How long it took, in seconds.
- class pipecat.observers.user_bot_latency_observer.LatencyBreakdown(*, ttfb: list[TTFBBreakdownMetrics] = <factory>, contributions: list[LatencyContribution] = <factory>, measured_from: MeasuredFrom | None = None, total_secs: float = 0.0, text_aggregation: TextAggregationBreakdownMetrics | None = None, user_turn_start_time: float | None = None, user_turn_secs: float | None = None, function_calls: list[FunctionCallMetrics] = <factory>)[source]
Bases:
BaseModelPer-service latency breakdown for a single user-to-bot cycle.
Collected between
VADUserStoppedSpeakingFrameandBotStartedSpeakingFramewhenenable_metrics=TrueinPipelineParams.- Parameters:
ttfb – Time-to-first-byte metrics from each service in the pipeline.
text_aggregation – First text aggregation measurement, representing the latency cost of sentence aggregation in the TTS pipeline.
user_turn_start_time – Unix timestamp when the user turn started (actual user silence, adjusted for VAD stop_secs).
Noneif noVADUserStoppedSpeakingFramewas observed.user_turn_secs – Duration in seconds of the user’s turn, measured from when the user actually stopped speaking to when the turn was released (
UserStoppedSpeakingFrame). This includes VAD silence detection, STT finalization, and any turn analyzer wait.Noneif noUserStoppedSpeakingFramewas observed (e.g. no turn analyzer configured).function_calls – Latency for each function call executed during this cycle. Empty if no function calls occurred.
contributions – Named parts of the interval, in chronological order, summing to the measured latency. A part is listed only if it happened, so a bot without turn completion has no marker or wait entries.
measured_from – Where the interval was anchored, so a greeting is not compared with a turn.
total_secs – The measured interval, which the contributions sum to.
- chronological_events() list[str][source]
Return human-readable event labels sorted by start time.
Deprecated since version 1.9.0: Use
turn_contribution_lines()instead, which names every part of the interval rather than the services that reported a metric. Will be removed in 2.0.0.Collects all sub-metrics into a flat list, sorts by
start_time, and returns formatted strings suitable for logging.- Returns:
List of formatted strings, one per event, in chronological order.
- turn_contribution_lines(*, by_cost: bool = False) list[str][source]
Format the contributions for logging, one per line plus a total.
One breakdown covers one user-to-bot cycle, so these lines describe a single turn — or, for the first thing the bot says, the wait before it from the pipeline being ready with a client on it, which the total line names.
- Parameters:
by_cost – Order by duration, largest first, rather than in the order things happened. Useful when the question is what to optimize rather than what the turn did.
- Returns:
One formatted line per contribution, then a total line. Empty if no contributions were collected.
- class pipecat.observers.user_bot_latency_observer.UserBotLatencyObserver(*, max_frames: int | None = None, min_contribution_secs: float = 0.005, time_source: Callable[[], float]=<built-in function time>, **kwargs)[source]
Bases:
BaseObserverObserver that tracks user-to-bot response latency.
Measures the time between when a user stops speaking (VADUserStoppedSpeakingFrame) and when the bot starts speaking (BotStartedSpeakingFrame). Emits events when latency is measured, allowing consumers to log, trace, or otherwise process the latency data.
When
enable_metrics=Truein pipeline params, also collects a latency breakdown and emits anon_latency_breakdownevent alongside the existing latency measurement. The breakdown carries per-service metrics (TTFB, text aggregation) and, incontributions, the whole interval named part by part:for line in breakdown.turn_contribution_lines(): logger.info(line) # 0.200s endpointing wait [config: VAD stop_secs] # 0.125s transcription [DeepgramSTTService#0] # 0.336s LLM inference [OpenAILLMService#0] # 0.359s speech synthesis [CartesiaTTSService#0] # 1.020s TOTAL
This observer follows the composition pattern used by TurnTrackingObserver, acting as a reusable component for latency measurement.
- Events:
- on_latency_measured(observer, latency_seconds): Emitted when
time-to-first-bot-speech is calculated. Measures the time from when the user stopped speaking to when the bot starts speaking.
- on_latency_breakdown(observer, breakdown): Emitted at each
BotStartedSpeakingFramewith aLatencyBreakdowncontaining per-service metrics collected during the user→bot cycle.- on_first_bot_speech_latency(observer, latency_seconds): Emitted once,
the first time
BotStartedSpeakingFramearrives afterClientConnectedFrame. Measures the time from client connection to the first bot speech.
- __init__(*, max_frames: int | None = None, min_contribution_secs: float = 0.005, time_source: Callable[[], float]=<built-in function time>, **kwargs)[source]
Initialize the user-bot latency observer.
- Parameters:
max_frames –
Unused.
Deprecated since version 1.12.0: No replacement. The observer receives each frame once. Will be removed in 2.0.0.
min_contribution_secs – Contributions shorter than this are rolled into the single pipeline entry rather than listed. Pass 0 to list every one, including individual frame hops.
time_source – Reads the current time in seconds. Supplying one lets a test drive a cycle without waiting out the intervals it describes.
**kwargs – Additional arguments passed to parent class.
- async on_pipeline_started()[source]
Record that the pipeline finished starting.
The
StartFramehas reached every processor by now, which is whereStartupTimingObserverstops measuring. A greeting is timed from here so the two reports meet rather than overlap.
- async on_push_frame(data: FramePushed)[source]
Process frames to track speech timing and calculate latency.
Tracks VAD events and bot speaking events to measure the time between user stopping speech and bot starting speech. Also accumulates metrics from MetricsFrame for the latency breakdown.
- Parameters:
data – Frame push event containing the frame and direction information.