How can I find the token count per turn that was s...
# ask-ai
c
How can I find the token count per turn that was sent to the llm
t
Thanks for reaching out, dexter. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking into how to find the token count per turn sent to the LLM! You can easily get this information from the metrics emitted right after each LLM inference completes. These metrics give you a clear picture of the input, output, and total tokens used. Here’s a friendly guide on how to access them: • Keep an eye out for the
metrics_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:
Copy code
# 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...
c
What happens when I call update_chat_ctx when the agent is speaking. Basically will calling update_chat_ctx anytime through the pipeline break anything
t
I completely understand your concern about calling
update_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