How to figure out usage details while using open a...
# ask-ai
c
How to figure out usage details while using open ai realtime? How can I select realtime-mini or choose other versions?
t
Thanks for reaching out, Subham. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just to make sure I’m on the same page, could you tell me a bit more about what you mean by "usage details"? Are you thinking about things like bandwidth usage, session duration, or something else? Also, when you mention choosing "realtime-mini" or other versions, are you talking about specific settings or deployment options within LiveKit? Happy to help once I get a clearer picture!
c
Copy code
{
  "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
}
t
Here’s a friendly breakdown of the usage details you shared: • llm_prompt_tokens: This is the total number of tokens in the prompt sent to the LLM, including any cached tokens. • llm_prompt_cached_tokens: These are tokens that were pulled from the cache, which helps save time and reduce costs. • llm_completion_tokens: Tokens generated by the LLM in its response. • tts_characters_count: The count of characters processed by the Text-to-Speech (TTS) model. • tts_audio_duration: How long the audio output from the TTS model lasts. • stt_audio_duration: The length of the audio input processed by the Speech-to-Text (STT) model. To keep track of and analyze these metrics during your session, you can use the
UsageCollector
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 Docs
c
yes, this is a result from the usage collector, my question is how to make sense of it in case of realtime models?
t
When you're working with realtime models, here’s a friendly breakdown of how to understand the usage metrics: • Input and Output Tokens: This covers
inputTokens
(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 Docs
c
@session.on("metrics_collected") def _on_metrics_collected(_ev_: MetricsCollectedEvent): metrics.log_metrics(ev.metrics) usage_collector.collect(ev.metrics) why don't i get
textTokens
,
audioTokens
, and
cachedTokens
?
t
Hey there! Just a quick heads-up about the metrics
textTokens
,
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 Docs
c
what is the model for realtime_model_metrics?
t
The
realtime_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.py
c
No, i meant something like this,
Copy code
class MetricsCollectedEvent(BaseModel):
    type: Literal["metrics_collected"] = "metrics_collected"
    metrics: AgentMetrics
    created_at: float = Field(default_factory=time.time)
t
Here’s the Python model definition for
realtime_model_metrics
in LiveKit:
Copy code
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.py
c
Where is this defined?
t
The
RealtimeModelMetrics
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!