Hi team. How can I access Realtime Modal usage met...
# agents
h
Hi team. How can I access Realtime Modal usage metrics?
@refined-appointment-81829 @bland-addition-5518 @jolly-air-155
This is my code: usage_collector = metrics.UsageCollector() @session.on("metrics_collected") def _on_metrics_collected(ev: MetricsCollectedEvent): logger.info(f"Entered Metrics collected") if hasattr(ev.metrics, 'type') and ev.metrics.type == "realtime_model_metrics": realtime_metrics = ev.metrics logger.info(f"Realtime tokens - Input: {realtime_metrics.input_tokens}, " f"Output: {realtime_metrics.output_tokens}, " f"Total: {realtime_metrics.total_tokens}") logger.info(f"Token details - Audio input: {realtime_metrics.input_token_details.audio_tokens}, " f"Text input: {realtime_metrics.input_token_details.text_tokens}, " f"Cached: {realtime_metrics.input_token_details.cached_tokens}") # Still log other metrics metrics.log_metrics(ev.metrics) async def log_usage(): summary = usage_collector.get_summary() logger.info(f"Usage: {summary}") if not shutdown_future.done(): shutdown_future.set_result(None) ctx.add_shutdown_callback(log_usage) however this is the only thing that gets printed in my terminal: Usage: UsageSummary(llm_prompt_tokens=0, llm_prompt_cached_tokens=0, llm_completion_tokens=0, tts_characters_count=0, stt_audio_duration=0.0) it appears as if i never enter _on_metrics_collected. any guidance would be appreciated
w
This is working for me:
Copy code
@session.on("metrics_collected")
    def _on_metrics_collected(ev):
        metrics.log_metrics(ev.metrics)
        if isinstance(ev.metrics, metrics.RealtimeModelMetrics):
            metrics_tracker.collect_metrics(ev.metrics)
(I am using my own custom tracker though)
h
@wonderful-holiday-7370 Thank you for sharing. Based on my logs, I think my code never enters _on_metrics_collected. can you share some other details maybe that might help me debug this. your livekit version etc?
w
I am using LiveKit 1.2.2 (latest version). My function is inside
entrypoint
just after session is intantiated. The problem I had initially is that my type check if statement was incorrect, so that code was never entered.
if isinstance(ev.metrics, metrics.RealtimeModelMetrics):
was what fixed it for me.
You also need to set the proper log level to get the statements to print:
python agent.py dev --log-level DEBUG
Claude Code read the LiveKit docs and figured out most of that for me.
h
can you share the link to the relevant page in livekit docs? I'll try taking help from claude although so far it also has no clue
I'll implement the steps you mentioned as well
Updating all the packages was the key. Thankyou for the help 🙌 @wonderful-holiday-7370