important-pharmacist-78974
09/09/2025, 6:40 PMtall-airline-16756
09/09/2025, 6:40 PMtall-airline-16756
09/09/2025, 6:41 PMmetrics_collected event on the AgentSession. This event provides structured metrics objects, including RealtimeModelMetrics, while your session is active.
2. Next, use the UsageCollector class to gather important metrics like token usage and audio duration throughout the session. Just pass each incoming metric to the collect() method, and when the session ends, you can get a summary using get_summary().
Here’s an example in Node.js to guide you:
import { metrics } from '@livekit/agents';
const usageCollector = new metrics.UsageCollector();
session.on('metrics_collected', (ev) => {
metrics.logMetrics(ev.metrics);
usageCollector.collect(ev.metrics);
});
// At session shutdown
async function logUsage() {
const summary = usageCollector.getSummary();
console.log(`Usage Summary: ${JSON.stringify(summary, null, 2)}`);
}
ctx.addShutdownCallback(logUsage);
If you’re working with Python, the process is quite similar. Just let me know if you’d like me to share some examples or walk you through it—I’m here to help!
Sources: Capturing metrics | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs | plugins/openai/src/realtime/realtime_model.ts | livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime...important-pharmacist-78974
09/09/2025, 6:42 PMtall-airline-16756
09/09/2025, 6:43 PMrich library for a beautifully formatted table. Here’s a friendly guide to help you through it:
1. First, set up metrics collection by subscribing to the metrics_collected event. For example:
from livekit.agents import metrics
@agent.on("metrics_collected")
def on_metrics_collected(mtrcs: metrics.AgentMetrics):
display_metrics_table(mtrcs)
1. Next, create a table using `rich`:
from rich.console import Console
from rich.table import Table
def display_metrics_table(metrics):
table = Table(title="LiveKit Metrics Report", show_header=True, header_style="bold cyan")
table.add_column("Metric", style="bold green")
table.add_column("Value", style="yellow")
# Add metrics dynamically
for field in dir(metrics):
if not field.startswith("_"):
value = getattr(metrics, field, None)
if value is not None:
table.add_row(field.replace("_", " ").title(), str(value))
Console().print(table)
This approach will dynamically fill your table with the metrics and their values. If you have any questions or want me to walk you through more examples, just let me know—I’m here to help!
Sources: metrics/send-metrics-to-3p/metrics_server/templates/dashboard.html | Logs, metrics, and telemetry | LiveKit Docs | Capturing metrics | LiveKit Docsimportant-pharmacist-78974
09/09/2025, 6:43 PMtall-airline-16756
09/09/2025, 6:44 PMrich library:
import asyncio
from livekit.agents import metrics, voice
from rich.console import Console
from rich.table import Table
console = Console()
class MetricsAgent(voice.Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful agent.",
llm=voice.openai.LLM(model="gpt-4o")
)
self.llm.on("metrics_collected", self.on_metrics_collected)
async def on_metrics_collected(self, metrics):
table = Table(title="LLM Metrics Report", show_header=True, header_style="bold cyan")
table.add_column("Metric", style="bold green")
table.add_column("Value", style="yellow")
table.add_row("Type", str(metrics.type))
table.add_row("Duration", f"{metrics.duration:.4f}s")
table.add_row("Tokens/Second", f"{metrics.tokens_per_second:.2f}")
table.add_row("Prompt Tokens", str(metrics.prompt_tokens))
table.add_row("Completion Tokens", str(metrics.completion_tokens))
console.print(table)
async def main():
session = voice.AgentSession()
await session.start(agent=MetricsAgent())
if __name__ == "__main__":
asyncio.run(main())
This example listens for the metrics_collected event to gather LLM metrics and then beautifully presents them in a table format. If you have any questions or would like me to walk you through any part of the code, please don’t hesitate to ask—I’m here to help!
Sources: metrics/send-metrics-to-3p/run_3p_metrics_demo.sh | Logs, metrics, and telemetry | LiveKit Docsimportant-pharmacist-78974
09/09/2025, 6:45 PMtall-airline-16756
09/09/2025, 6:45 PMopenai.realtime.RealtimeModel. The metrics_collected event is also emitted for realtime models, so you can use the same approach to gather and show your metrics in a table.
Here’s a friendly example of how you can adjust your session initialization:
session = AgentSession(
llm=openai.realtime.RealtimeModel()
)
session.on('metrics_collected', (metrics) => {
# Process and display metrics here
})
If you have any questions or want me to walk you through more examples, just let me know—I’m here to help!important-pharmacist-78974
09/09/2025, 6:53 PMtall-airline-16756
09/09/2025, 6:54 PMopenai.realtime.RealtimeModel in LiveKit:
from livekit.agents import AgentSession
from livekit.plugins.openai.realtime import RealtimeModel
class RealtimeAgent:
def __init__(self):
self.model = RealtimeModel(
model="gpt-4o-realtime-preview",
voice="alloy",
temperature=0.8,
modalities=["text", "audio"],
instructions="You are a helpful assistant designed for real-time interaction."
)
async def start_session(self):
session = AgentSession(llm=self.model)
await session.start()
if __name__ == "__main__":
agent = RealtimeAgent()
asyncio.run(agent.start_session())
This example shows how to easily set up a session with RealtimeModel and get it running. If you have any questions or want to explore more features, just let me know—I’m here to help!
Sources: agent.py | web/src/components/code-viewer.tsx | examples/avatar_agents/bey/agent_worker.py | Realtime model integrations | LiveKit Docs | OpenAI Realtime API integration guide | LiveKit Docsimportant-pharmacist-78974
09/09/2025, 6:54 PM