little-magician-85414
08/04/2025, 8:58 AM@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
usage_collector.collect(ev.metrics)
async def log_usage():
summary = usage_collector.get_summary()
logger.info(f"Usage: {summary}")
room_name = assistant.config.room_name
)
ctx.add_shutdown_callback(log_usage)
output:
Usage: {"llm_prompt_tokens": 8501, "llm_prompt_cached_tokens": 0, "llm_completion_tokens": 291, "tts_characters_count": 0, "stt_audio_duration": 80.04999999999984}tall-airline-16756
08/04/2025, 8:58 AMtall-airline-16756
08/04/2025, 8:58 AMSpeechEvent stream.
• Counting the characters in the transcribed text for each FINAL_TRANSCRIPT or INTERIM_TRANSCRIPT event.
• Aggregating these counts separately from the built-in metrics.
Let me know if you'd like an example of how to implement this custom tracking.
Sources: agents/src/stt/stt.ts | livekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/stt.py | SpeechEventType | LiveKit Agents | UsageSummary | LiveKit Agentslittle-magician-85414
08/04/2025, 8:59 AMmetrics module also includes a UsageCollector helper class for aggregating usage metrics across a session. It tracks metrics such as LLM, TTS, and STT API usage, which can help estimate session cost.tall-airline-16756
08/04/2025, 9:00 AMUsageCollector in LiveKit—it keeps track of the duration of your STT audio, not the number of characters. So, when the docs mention STT API usage, they’re talking about how long the audio is that’s processed by the STT model, not the length of the transcribed text.
For TTS (Text-to-Speech), character counts are tracked, but with STT, it’s all about audio duration. If you want to keep an eye on character counts for STT, you’d need to set up a custom way to count the characters in the transcriptions. Hope that helps!
Sources: agents/src/stt/stt.ts | livekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/stt.py | Capturing metrics | LiveKit Docs | TTSMetrics | LiveKit Agentslittle-magician-85414
08/04/2025, 9:00 AMtall-airline-16756
08/04/2025, 9:01 AMcharactersCount property in the TTSMetrics isn’t being set correctly.
Here’s what you can check to fix this:
• Make sure you’re actually running TTS operations during your session.
• Double-check that the TTSMetrics object is properly updating the charactersCount with the number of characters processed.
If you’ve confirmed these and it’s still not working, just give me a shout—I’m here to help!
Sources: agents/src/metrics/usage_collector.ts | TTSMetrics | LiveKit Agentslittle-magician-85414
08/04/2025, 9:01 AMtall-airline-16756
08/04/2025, 9:02 AMcharactersCount field not updating as it should.
• Sometimes, an error during TTS processing can stop the metrics from being sent out.
• Or, there might be a bug in the TTS implementation where the character count isn’t being set in the metrics object.
Here’s how you can troubleshoot this:
1. Double-check that the input text is properly passed to the TTS methods.
2. Look out for any errors coming from the TTS instance.
3. Take a peek at the metrics collection process using logs or a debugger to spot any issues.
4. Make sure the TTS implementation is actually setting the charactersCount field in the metrics object.
If you’d like, feel free to share which TTS provider you’re using, a snippet of your code, or any error logs you’ve got. I’m here to help you figure this out!