clever-optician-10225
10/22/2025, 8:47 PMtall-airline-16756
10/22/2025, 8:47 PMtall-airline-16756
10/22/2025, 8:49 PMmetrics_collected event from your LLM or RealtimeModel instance. This event will provide you with metrics objects like LLMMetrics or RealtimeModelMetrics.
• For typical LLM interactions, the LLMMetrics object includes helpful fields such as prompt_tokens, completion_tokens, and total_tokens.
• When working with real-time models, the RealtimeModelMetrics object offers input_tokens, output_tokens, and total_tokens, along with detailed breakdowns for both input and output tokens.
Here’s a simple example to show you how to handle these metrics:
# Listen for metrics events
llm_instance.on("metrics_collected", handle_metrics)
def handle_metrics(metrics):
if isinstance(metrics, LLMMetrics):
print(f"Prompt tokens: {metrics.prompt_tokens}")
print(f"Completion tokens: {metrics.completion_tokens}")
print(f"Total tokens: {metrics.total_tokens}")
elif isinstance(metrics, RealtimeModelMetrics):
print(f"Input tokens: {metrics.input_tokens}")
print(f"Output tokens: {metrics.output_tokens}")
print(f"Total tokens: {metrics.total_tokens}")
Also, if you want to make things even easier, you might want to try the UsageCollector utility. It helps you automatically gather metrics across many interactions, saving you time and effort.
Sources: Logs, metrics, and telemetry | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...clever-optician-10225
10/23/2025, 2:05 AMtall-airline-16756
10/23/2025, 2:06 AMupdate_chat_ctx while the agent is speaking. The great news is that doing so won’t interrupt or break the ongoing speech. The chat context updates happen independently from the speech synthesis and playback pipeline, so the current speech keeps going smoothly without any interruptions.
Let me walk you through how LiveKit manages this:
• Thread Safety: We use a lock to protect the operation, which helps prevent any race conditions and keeps updates safe even when multiple tasks are running at the same time.
• Speech Coordination: While the agent is speaking, LiveKit keeps track of the current speech state but still allows the context update to happen, so future interactions have the latest info.
• Realtime Sync: For realtime models, the updated context is synced with the remote session seamlessly, without disturbing the speech that’s already happening.
• Function Call Handling: If your update includes function calls, LiveKit makes sure everything happens in the right order by waiting for speech or function execution to finish before applying the outputs.
So, in a nutshell, you can confidently call update_chat_ctx anytime without worrying about breaking the pipeline or interrupting the agent’s current speech. We’re here to make sure everything runs smoothly for you!
Sources: examples/voice_agents/restaurant_agent.py | Agent Session & Client Integration | livekit/livekit_composite | DeepWiki