when calculating total token used, should we inclu...
# ask-ai
b
when calculating total token used, should we include the summation of all the tokens - cached token for billing purposes
✅ 1
t
Thanks for reaching out, Utkarsh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’m responding about whether to subtract cached tokens when calculating total tokens for billing. Do not subtract cached tokens. Count cached input tokens and uncached input tokens separately (they’re both included in the billed usage) and also include output tokens. Cached input tokens are billed at a lower rate, not excluded. If you want, share a sample usage breakdown (input, cached input, output) and I’ll show the exact billing calculation. Sources: https://livekit.com/pricing.md| | Build voice, video, and physical AI | LiveKit, with interactive elements
b
AgentSessionUsage(model_usage=[LLMModelUsage(type='llm_usage', provider='chatgpt-us2.openai.azure.com', model='gpt-realtime', input_tokens=11293, input_cached_tokens=10240, input_audio_tokens=1143, input_cached_audio_tokens=704, input_text_tokens=10150, input_cached_text_tokens=9536, output_tokens=488, output_audio_tokens=328, output_text_tokens=160)]) for the above payload, what should be the calculation?
t
Thanks — I’m responding to your payload and the calculation question. You provided: • input_tokens = 11,293 • input_cached_tokens = 10,240 • input_audio_tokens = 1,143 • input_cached_audio_tokens = 704 • input_text_tokens = 10,150 • input_cached_text_tokens = 9,536 • output_tokens = 488 • output_audio_tokens = 328 • output_text_tokens = 160 Clarification: billing counts uncached input tokens, cached input tokens (billed at a lower cached rate), and output tokens. Audio/text breakdowns are subcomponents of the input/output totals and are not billed separately unless your pricing table lists separate audio/text rates—typically you use the totals (
input_tokens
,
input_cached_tokens
,
output_tokens
) for billing. I can compute a final cost — please tell me which per-1M rates to use for: • Input (uncached) rate • Cached input rate • Output rate If you’d like, I can use example rates and show the full numeric total. Which do you prefer? Sources: https://livekit.com/pricing.md| | Build voice, video, and physical AI | LiveKit, with interactive elements
b
get me an example
t
Thanks — I’ll use example per-1M rates and show the full calculation for your payload. Assumed example rates: • Input (uncached) rate: $5.00 per 1M tokens • Cached input rate: $0.50 per 1M tokens • Output rate: $2.50 per 1M tokens Values from your payload: • input_tokens = 11,293 • input_cached_tokens = 10,240 • output_tokens = 488 Compute uncached input: • Uncached input tokens = input_tokens − input_cached_tokens = 11,293 − 10,240 = 1,053 Compute costs: • Uncached input cost = (1,053 / 1,000,000) × $5.00 = $0.005265 • Cached input cost = (10,240 / 1,000,000) × $0.50 = $0.00512 • Output cost = (488 / 1,000,000) × $2.50 = $0.00122 Total cost: • Total = $0.005265 + $0.00512 + $0.00122 = $0.011605 Summary: • Uncached input tokens: 1,053 → $0.005265 • Cached input tokens: 10,240 → $0.00512 • Output tokens: 488 → $0.00122 • Total example cost: $0.011605 If you want this using your actual pricing rates, share them and I’ll recalc. , with interactive elements
b
how to get the token consumed for a call?
t
You're asking how to get the tokens consumed for a single call — here's how to do that. • Use the session's metrics event: listen for the
metrics_collected
(or provider-specific metrics) event on your AgentSession; the metrics payload includes
input_tokens
,
input_cached_tokens
, and
output_tokens
. • For OpenAI-style providers, the model response also returns
prompt_tokens
(input),
completion_tokens
(output), and `total_tokens`; LiveKit passes these through. • You can aggregate or report per-call usage with the
UsageCollector
utility, which processes
metrics_collected
events and sums token counts per call/service. Example (Python-style):
Copy code
@session.on("metrics_collected")
def on_metrics(metrics):
    print("input_tokens:", metrics.input_tokens)
    print("input_cached_tokens:", metrics.input_cached_tokens)
    print("output_tokens:", metrics.output_tokens)
Notes: • Use
input_tokens
and
input_cached_tokens
together to compute billed input (uncached = `input_tokens - input_cached_tokens`; cached billed separately). • Audio/text subfields (e.g.,
input_audio_tokens
,
input_text_tokens
) are components of the totals; use the totals for billing unless you have separate audio/text rates. If you want, tell me your SDK/language and I’ll give a short code example for that environment. Sources: Realtime model metrics | LiveKit Documentation | OpenAI LLM models | LiveKit Documentation | Agents (Python) | livekit/livekit_composite | DeepWiki, with interactive elements
b
what about session_usage_updated?
t
You're asking about
session_usage_updated
— here's what it is and how to use it. • Purpose:
session_usage_updated
notifies you of aggregated usage changes for an AgentSession (e.g., updated totals for input tokens, cached input tokens, output tokens, and potentially audio/text subfields). It’s useful for tracking session-level billing and showing live usage to users. • How it differs from `metrics_collected`: ◦
metrics_collected
is emitted per LLM call or metric collection interval and contains granular metrics for that event. ◦
session_usage_updated
provides the updated session-wide totals (cumulative) after those granular metrics are applied. • What to read from it: ◦ Use the session totals (
input_tokens
,
input_cached_tokens
,
output_tokens
) from
session_usage_updated
for session-level billing and reporting. ◦ To compute per-call billed amounts, subtract the previous session totals from the new totals to get per-call delta, then apply your pricing (uncached = input − cached; cached billed at cached rate; output billed at output rate). • Example flow: a. Listen for
session_usage_updated
. b. On each event, compute delta = new_totals − previous_totals. c. Apply pricing to the delta tokens (uncached vs cached vs output). d. Update UI or billing records with the computed delta cost and new cumulative totals. If you want, tell me which SDK/language you use and I’ll give a brief code snippet showing how to listen for
session_usage_updated
and compute the per-call delta. , with interactive elements