creamy-tiger-14805
09/03/2025, 8:44 AMtall-airline-16756
09/03/2025, 8:44 AMtall-airline-16756
09/03/2025, 8:45 AMcreamy-tiger-14805
09/03/2025, 8:46 AM{
"llm_prompt_tokens": 17519,
"llm_prompt_cached_tokens": 15232,
"llm_completion_tokens": 1757,
"tts_characters_count": 0,
"tts_audio_duration": 0,
"stt_audio_duration": 0
}tall-airline-16756
09/03/2025, 8:47 AMUsageCollector class. Here’s a quick guide to get you started:
1. Start by initializing UsageCollector at the beginning of your session.
2. Subscribe to the metrics_collected event and feed those metrics into the collector.
3. When your session wraps up, just call get_summary() to see the aggregated results.
If you’d like a hand with setting this up or have any other questions, feel free to reach out—I’m here to help!
Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py | plugins/openai/src/realtime/realtime_model.ts | Logs, metrics, and telemetry | LiveKit Docscreamy-tiger-14805
09/03/2025, 8:49 AMtall-airline-16756
09/03/2025, 8:49 AMinputTokens (all the tokens you send to the model) and outputTokens (the tokens the model generates in response). These help you get a sense of your API costs and how complex each interaction is.
• Token Details: Metrics like textTokens, audioTokens, and cachedTokens give you a peek into the types of tokens used and how well caching is working.
• Time to First Token (TTFT): This measures how long it takes from when you make a request to when the first output token appears. Lower numbers mean the model is responding more quickly.
• Duration: The total time from starting the request to finishing it—great for checking latency.
• Tokens Per Second: Shows how fast the model is generating output, calculated as outputTokens / duration.
• Cancelled Requests: Keeps track if a request was cancelled, which helps you understand stability.
To make sense of all these metrics, you can use the UsageCollector class to gather data across different sessions. This way, you can keep an eye on API costs, improve performance, and troubleshoot any issues smoothly. If you want, I’m here to help with the implementation—just let me know!
Sources: agents/src/llm/llm.ts | livekit-agents/livekit/agents/llm/llm.py | Logs, metrics, and telemetry | LiveKit Docscreamy-tiger-14805
09/03/2025, 8:51 AMtextTokens, audioTokens, and cachedTokens ?tall-airline-16756
09/03/2025, 8:52 AMtextTokens, audioTokens, and cachedTokens — these are available specifically in the realtime_model_metrics event type. This event pops up when you're using Realtime APIs like OpenAI or Google Gemini that support detailed token tracking. If you’re not seeing these metrics, it might be due to a few reasons:
• Not using a Realtime API: These metrics show up only in realtime_model_metrics, not in the usual llm_metrics.
• Incorrect event handling: Make sure your event listener is checking for type == 'realtime_model_metrics' so you can grab those fields.
• Logging configuration: The default log_metrics helper might skip nested fields like inputTokenDetails and outputTokenDetails. You’ll want to log those explicitly to catch all the details.
Here’s a quick checklist to make sure you’re seeing these metrics:
1. Use a Realtime-capable model, like the OpenAI Realtime API or Google Gemini.
2. Set up your listener to handle realtime_model_metrics events.
3. Explicitly log nested fields such as inputTokenDetails and outputTokenDetails.
If you run into any bumps or want a hand with your setup, just give me a shout — I’m here to help!
Sources: agents/src/metrics/utils.ts | livekit-agents/livekit/agents/metrics/usage_collector.py | Logs, metrics, and telemetry | LiveKit Docscreamy-tiger-14805
09/03/2025, 9:18 AMtall-airline-16756
09/03/2025, 9:18 AMrealtime_model_metrics model in LiveKit is designed to give you a clear snapshot of how real-time interactions are performing. Here’s a quick rundown of the important fields:
• type: Always 'realtime_model_metrics'.
• label: Tells you the source, like 'openai_realtime'.
• requestId: A unique ID for each request.
• timestamp: When the response was created.
• duration: How long it took from request start to finish.
• ttft: Time to first token, measured in seconds.
• cancelled: Shows if the request was cancelled.
• inputTokens and outputTokens: Total tokens for input and output, covering both text and audio.
• tokensPerSecond: How many tokens are generated each second.
• inputTokenDetails: Breaks down audioTokens, textTokens, cachedTokens, and cachedTokensDetails.
• outputTokenDetails: Includes textTokens, audioTokens, and imageTokens.
This model helps you keep track of key metrics during real-time sessions with APIs like OpenAI or Google. If you want, I’m here to help with more details or examples—just let me know!
Sources: agents/src/metrics/usage_collector.ts | livekit-agents/livekit/agents/llm/realtime.pycreamy-tiger-14805
09/03/2025, 9:19 AMclass MetricsCollectedEvent(BaseModel):
type: Literal["metrics_collected"] = "metrics_collected"
metrics: AgentMetrics
created_at: float = Field(default_factory=time.time)tall-airline-16756
09/03/2025, 9:19 AMrealtime_model_metrics in LiveKit:
class RealtimeModelMetrics(BaseModel):
class CachedTokenDetails(BaseModel):
audio_tokens: int
text_tokens: int
image_tokens: int
class InputTokenDetails(BaseModel):
audio_tokens: int
text_tokens: int
image_tokens: int
cached_tokens: int
cached_tokens_details: RealtimeModelMetrics.CachedTokenDetails | None
class OutputTokenDetails(BaseModel):
text_tokens: int
audio_tokens: int
image_tokens: int
type: Literal["realtime_model_metrics"] = "realtime_model_metrics"
label: str
request_id: str
timestamp: float
duration: float
ttft: float
cancelled: bool
input_tokens: int
output_tokens: int
total_tokens: int
tokens_per_second: float
input_token_details: InputTokenDetails
output_token_details: OutputTokenDetails
This model tracks important stuff like token usage, timing, and other details for real-time interactions. If you want me to walk you through it or show some examples, just let me know—I’m happy to help!
Sources: livekit-agents/livekit/agents/llm/realtime.pycreamy-tiger-14805
09/03/2025, 9:20 AMtall-airline-16756
09/03/2025, 9:20 AMRealtimeModelMetrics class lives in the livekit/agents/metrics/base.py file in the LiveKit Python SDK. If you’re having any trouble finding it or want a hand using it, just give me a shout—I’m here to help!