square-spoon-72769
11/24/2025, 9:16 PMtall-airline-16756
11/24/2025, 9:16 PMtall-airline-16756
11/24/2025, 9:16 PMprompt_cached_tokens and how to leverage it: prompt_cached_tokens is a read-only metric included in LiveKit’s LLMMetrics that shows how many tokens from an input prompt were served from the LLM provider’s backend cache. You cannot configure or enable it from LiveKit; it simply reports caching activity if the provider returns that info. To “leverage” it, read the metric after each inference and use it for monitoring/analytics (e.g., track cache hit rate, correlate with latency/cost).
• Does it work with LiveKit Inference + OpenAI: The metric is exposed in LiveKit Inference and will appear if the underlying provider reports caching data. However, there is no LiveKit-configurable prompt-caching control for OpenAI models via Inference — OpenAI caching at this level is not documented as supported. If you need caching behavior you must implement application-side caching (cache prompt→response pairs or reuse context) and use prompt_cached_tokens only to observe provider-side caching when it happens.
If you’d like, tell me how you plan to use caching (monitoring vs. reducing costs vs. latency optimization) and I can suggest a concrete app-layer caching pattern or metrics to track.
Sources: Observability data hooks | LiveKit docs | OpenAI LLM models | LiveKit Docssquare-spoon-72769
11/25/2025, 8:31 AMtall-airline-16756
11/25/2025, 8:31 AMLLMMetrics fields like prompt_cached_tokens, prompt_tokens, completion_tokens, total_tokens, and latency.
Python
from livekit.agents import AgentContext
async def on_metrics(ev):
llm = ev.metrics.llm
if not llm:
return
print("prompt_cached_tokens:", llm.prompt_cached_tokens)
print("prompt_tokens:", llm.prompt_tokens)
print("completion_tokens:", llm.completion_tokens)
print("total_tokens:", llm.total_tokens)
print("latency_ms:", llm.latency_ms)
ctx = AgentContext()
ctx.on("metrics_collected", on_metrics)
# run your agent/session as usual
Node.js (TypeScript)
import { AgentSession } from "@livekit/agents";
const session = new AgentSession(/* ... */);
session.on("MetricsCollected", (ev) => {
const llm = ev.metrics.llm;
if (!llm) return;
console.log("prompt_cached_tokens:", llm.prompt_cached_tokens);
console.log("prompt_tokens:", llm.prompt_tokens);
console.log("completion_tokens:", llm.completion_tokens);
console.log("total_tokens:", llm.total_tokens);
console.log("latency_ms:", llm.latency_ms);
});
If you want, I can:
• Provide an example that aggregates these metrics over time (cache hit rate, tokens per request).
• Show how to emit these to a monitoring system (Prometheus/Datadog). Which would you prefer?square-spoon-72769
11/25/2025, 8:32 AMsquare-spoon-72769
11/25/2025, 8:32 AMtall-airline-16756
11/25/2025, 8:32 AMprompt_cached_tokens). Below is a minimal, working example pattern from the LiveKit Python examples that:
• subscribes to the metrics_collected event on a session
• inspects LLMMetrics fields
• optionally aggregates usage with UsageCollector
from livekit.agents import AgentSession, metrics, MetricsCollectedEvent
session = AgentSession(...) # configure your session
@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
llm = ev.metrics
if not isinstance(llm, metrics.LLMMetrics):
return
print("prompt_cached_tokens:", llm.prompt_cached_tokens)
print("prompt_tokens:", llm.prompt_tokens)
print("completion_tokens:", llm.completion_tokens)
print("total_tokens:", llm.total_tokens)
print("latency_ms:", llm.latency_ms)
print("ttft:", llm.ttft)
print("duration:", llm.duration)
# Optional: aggregate usage across the session
usage_collector = metrics.UsageCollector()
@session.on("metrics_collected")
def _collect_usage(ev: MetricsCollectedEvent):
usage_collector.collect(ev.metrics)
# Later, get a summary
summary = usage_collector.get_summary()
print("LLM usage summary:", summary)
If you want, I can:
• point to the exact file in the examples repo,
• adapt this into a full runnable example (including session setup and agent flow), or
• show how to export these metrics to Prometheus/Datadog. Which would you prefer?square-spoon-72769
11/25/2025, 8:33 AMtall-airline-16756
11/25/2025, 8:33 AMsquare-spoon-72769
11/25/2025, 8:33 AMtall-airline-16756
11/25/2025, 8:33 AM