is there a way to track usage of each message sepa...
# ask-ai
e
is there a way to track usage of each message separately in conversation e.g: Message 1 - LLM Time, TTS Time, STT Time, Duration
t
Thanks for reaching out, Aman. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you track usage metrics like LLM Time, TTS Time, STT Time, and Duration for each message in a conversation using LiveKit's built-in metrics system. These metrics are emitted for each operation (STT, LLM, TTS) and you can capture them in real time with event listeners. The
speech_id
field is especially useful, as it helps you connect processing times to specific conversation turns. Here’s a simple way to get started: • Set up event listeners to collect metrics using the
metrics_collected
event. • Use the
speech_id
to organize metrics for each message. • Keep track of important metrics like STT duration, LLM generation time, TTS synthesis time, and audio durations. • If you want, you can also aggregate usage across the whole session with the
UsageCollector
or export these metrics to external tools for deeper analysis. If you’d like, I’d be more than happy to provide detailed code examples or guide you through the process step by step—just let me know! Sources: metrics/metrics_llm.py | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs | Logs, metrics, and telemetry | LiveKit docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real...
e
Here's payload for conversation item and metrics collected i'm not able to identify any common field between these 2 payloads, how to map? Metrics ------- type='metrics_collected' metrics=TTSMetrics( type='tts_metrics', label='livekit.plugins.elevenlabs.tts.TTS', request_id='65e7d02b0e1b', timestamp=1762927375.1260436, ttfb=0.5067559580002126, duration=0.9501827919993957, audio_duration=5.485714285714285, cancelled=False, characters_count=87, streamed=True, segment_id='65e7d02b0e1b', speech_id='speech_a4c16b0858b0', metadata=Metadata( model_name='eleven_turbo_v2_5', model_provider='ElevenLabs' ) ) created_at=1762927375.126792 Conversation Item ----------------- type='conversation_item_added' item=ChatMessage( id='item_76b22990ea80', type='message', role='assistant', content=['Hi there! I’m Mica from Maica24. Thanks for getting in touch! How can I help you today?'], interrupted=False, transcript_confidence=None, hash=None, created_at=1762927380.1710165 ) created_at=1762927380.1712735
t
To help you map metrics payloads like
TTSMetrics
to conversation items, a great approach is to use shared identifiers or timestamps. Here’s a simple way to do it: • Look at the
speech_id
field in `TTSMetrics`—this uniquely matches the speech generated for a specific message, so you can link it to the right conversation turn. • Another option is to compare the
created_at
timestamps in both the conversation item and the metrics payload to find when they align. • For other metrics such as
LLMMetrics
, the
request_id
often corresponds directly to the
id
of a conversation item. By putting these pieces together, you’ll be able to connect metrics to conversation items smoothly for better tracking and analysis. If you’d like, I’m here to provide more details or examples—just let me know! Sources: tests/test_agent_session.py | metrics/send-metrics-to-3p/send_metrics_to_3p.py
e
can you share an example
t
Here’s a helpful example to guide you on how to map a
TTSMetrics
payload to a conversation item: Let’s consider the following: • Metrics Payload:
speech_id='speech_a4c16b0858b0'
,
timestamp=1762927375.1260436
• Conversation Item:
id='item_76b22990ea80'
,
created_at=1762927380.1710165
Here are the steps to help you correlate them: 1. Start by using the
speech_id
from
TTSMetrics
to find the matching conversation turn. If the conversation item’s metadata includes a
speech_id
, that’s a direct and easy match. 2. If you don’t see a
speech_id
in the conversation item, then compare the
timestamp
from
TTSMetrics
with the
created_at
timestamp of the conversation item. Just make sure the timestamps are close enough to suggest they’re part of the same event. For instance, if the
speech_id
in
TTSMetrics
matches the conversation item’s metadata, or if the timestamps are aligned closely (say, within a few seconds), you can feel confident mapping the metric to that conversation item. If you’d like, I’d be more than happy to provide a code snippet to make this even clearer—just let me know!
e
are there any event that sends usage for each message
t
Thanks for reaching out! I understand you're looking to connect usage metrics with individual messages in LiveKit. While LiveKit doesn't emit usage metrics directly tied to each message, it does emit metrics per operation (like LLM, TTS, STT) through the
metrics_collected
event. Here's a friendly way to approach this: • Keep an eye on both the
conversation_item_added
and
metrics_collected
events. • Try using shared identifiers such as
speech_id
or match up timestamps (
created_at
) between these events. • Create some custom logic to link metrics with conversation items by looking at their timing and context. If you're interested in more advanced tracking, integrating OpenTelemetry can offer trace-based observability throughout the agent's lifecycle. Please feel free to ask if you'd like me to share more details or some examples—I'm here to help! Sources: Logs, metrics, and telemetry | LiveKit docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Events and error handling | LiveKit docs