tall-lamp-87106
08/05/2025, 9:35 AMtall-airline-16756
08/05/2025, 9:35 AMtall-airline-16756
08/05/2025, 9:35 AMUsageCollector class. Here's how you can do it:
1. Initialize the UsageCollector: const usageCollector = new UsageCollector();
2. Collect Metrics via Event Listeners: session.on("metrics_collected", (ev) => {
usageCollector.collect(ev.metrics);
});
3. Retrieve the Usage Summary: const summary = usageCollector.summary;
4. Extract Specific Metrics:
◦ LLM Tokens: summary.llmCompletionTokens & summary.llmPromptTokens
◦ TTS Characters: summary.ttsCharactersCount
◦ STT Audio Duration: summary.sttAudioDuration
1. Integrate with Billing Systems: Use the collected metrics to calculate costs based on your billing structure. For example, sum the LLM tokens for each session and apply your pricing model.
Here’s an example:
// Initialize UsageCollector
const usageCollector = new UsageCollector();
// Collect metrics from events
session.on("metrics_collected", (ev) => {
usageCollector.collect(ev.metrics);
});
// Log the usage summary (e.g., at session shutdown)
ctx.addShutdownCallback(async () => {
const summary = usageCollector.summary;
console.log("Usage Summary:", summary);
// Implement logic to store or process these metrics for billing
});
Let me know if you need further assistance with this!
Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...tall-lamp-87106
08/05/2025, 9:38 AMtall-airline-16756
08/05/2025, 9:39 AMUsageCollector on the agent side to keep track of metrics and also add logic for user metadata and session details. Here’s a friendly walkthrough on how to get started:
Tracking Metrics with UsageCollector
1. Initialize the UsageCollector: const usageCollector = new UsageCollector();
2. Collect Metrics: Just attach an event listener to catch those metrics as they come in. session.on("metrics_collected", (ev) => {
usageCollector.collect(ev.metrics);
});
3. Log Usage Summary: When the session ends, grab the summary and log it. ctx.addShutdownCallback(async () => {
const summary = usageCollector.summary;
console.log("Usage Summary:", summary);
});
Tracking User Metadata and Session Details
1. Store Metadata: You can easily store custom info like user ID or session start time using session.userdata. session.userdata["user_id"] = user.id;
session.userdata["session_start_time"] = start_time;
2. Access Metadata: Need to check or update metadata during the session? No problem! `const userId = session.userdata["user_id"];
console.log(Session started by user: ${userId});`
Schema Design for Storing Data
To keep things organized, here’s a simple schema you might find handy for storing session and usage data:
{
"session_id": "string", // Unique identifier for the session
"user_id": "string", // User identifier
"session_start_time": "timestamp", // Start time of the session
"session_end_time": "timestamp", // End time of the session
"llm_tokens": {
"prompt_tokens": "number",
"completion_tokens": "number"
},
"tts_characters_count": "number", // Total TTS characters used
"stt_audio_duration": "number", // Total STT audio duration in seconds
"conversation_type": "string", // Type of conversation (e.g., support, general)
"custom_metadata": "object" // Additional metadata as needed
}
If you want to dive deeper or tweak anything, just let me know—I’m here to help!tall-lamp-87106
08/05/2025, 10:03 AMtall-airline-16756
08/05/2025, 10:04 AMimport json
metadata = {
"user_id": "your_internal_user_id",
"user_name": "John Doe",
"user_phone": "+1234567890"
}
await create_explicit_dispatch(
room="my-room",
agent="test-agent",
metadata=json.dumps(metadata)
)
2. Consume Metadata in the Agent:
Inside the agent’s entrypoint function, you can grab the metadata from the JobContext like this:
import json
async def entrypoint(ctx: JobContext):
metadata = json.loads(ctx.job.metadata)
user_id = metadata["user_id"]
user_name = metadata["user_name"]
user_phone = metadata["user_phone"]
# Use the user_id as needed
print(f"User ID: {user_id}")
Just a few quick notes:
• Using a structured format like JSON makes it super easy to parse and pull out specific values.
• Keep in mind any sensitive info you include, since metadata gets stored in job logs.
• There’s no strict size limit, but try to keep metadata reasonably sized to keep things running smoothly.
If you want a hand with this or have any other questions, just let me know—I'm here to help!
Sources: Job lifecycle | LiveKit Docs | Agent dispatch | LiveKit Docs | Handling RAG Delays in Voice Agents | LiveKit Docstall-lamp-87106
08/11/2025, 9:34 AMtall-lamp-87106
08/11/2025, 9:34 AMtall-airline-16756
08/11/2025, 9:35 AMUsageCollector to keep track of metrics for TTS, STT, and LLM usage:
from livekit.agents import metrics
# Initialize usage collector
usage_collector = metrics.UsageCollector()
# Track metrics
@agent.on("metrics_collected")
def _on_metrics_collected(ev):
usage_collector.collect(ev.metrics)
# Log usage summary on shutdown
async def log_usage():
summary = usage_collector.get_summary()
print(f"Usage Summary: {summary}")
ctx.add_shutdown_callback(log_usage)
2. Cost Estimation:
Next, match those usage metrics to your provider's pricing model like this:
• STT: Audio duration (seconds) → Cost per minute of audio processed
• LLM: Tokens used → Cost per token
• TTS: Audio duration (seconds) → Cost per minute of audio generated
3. Integrate with Billing Service:
Then, pass the collected metrics to your billing system. For example, if you’re using Stripe:
import stripe
stripe.api_key = "your-stripe-api-key"
summary = usage_collector.get_summary()
# Example pricing
pricing = {
"stt": 0.024, # Cost per second
"llm": 0.002, # Cost per token
"tts": 0.022 # Cost per second
}
total_cost = (
summary.stt_duration * pricing["stt"] +
summary.llm_tokens * pricing["llm"] +
summary.tts_duration * pricing["tts"]
)
# Create invoice
invoice = stripe.Invoice.create(
customer="cus_123456789",
amount=round(total_cost * 100), # Convert to cents
currency="usd"
)
4. Reporting and Monitoring:
It’s also a great idea to set up monitoring so you can keep an eye on usage patterns and generate reports:
# Track daily usage
usage_collector.start_reporting_daily()
5. Example Implementation:
Here’s a complete example that shows how you can track metrics and estimate costs:
from livekit.agents import Agent, metrics
class BillingAssistant(Agent):
def __init__(self):
super().__init__()
self.usage_collector = metrics.UsageCollector()
async def on_enter(self):
self.session.generate_reply("Hello! How can I help you today?", allow_interruptions=True)
async def entrypoint(ctx):
agent = BillingAssistant()
await ctx.connect()
@agent.on("metrics_collected")
def on_metrics_collected(ev):
agent.usage_collector.collect(ev.metrics)
async def log_usage():
summary = agent.usage_collector.get_summary()
print(f"Usage Summary: {summary}")
ctx.add_shutdown_callback(log_usage)
await agent.start(ctx.room, await ctx.wait_for_participant())
Final Notes:
• Make sure you have accurate pricing details for each provider and model.
• Don’t forget to handle currency conversions if needed.
• And remember to add error handling for your billing API calls to keep things running smoothly.
If you have any questions or want to dive deeper into any part of this, just let me know—I’m here to help!
Sources: Logs, metrics, and telemetry | LiveKit Docs | Google integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/__init__.py | agent/main.pytall-airline-16756
08/11/2025, 9:35 AMUsageCollector to gather metrics for TTS, STT, and LLM usage:
from livekit.agents import metrics
# Initialize usage collector
usage_collector = metrics.UsageCollector()
# Track metrics
@agent.on("metrics_collected")
def _on_metrics_collected(ev):
usage_collector.collect(ev.metrics)
# Log usage summary on shutdown
async def log_usage():
summary = usage_collector.get_summary()
print(f"Usage Summary: {summary}")
ctx.add_shutdown_callback(log_usage)
2. Define Pricing Model
Next, map those usage metrics to your provider’s pricing like this:
• STT: Charge based on audio duration (think cost per second).
• LLM: Charge based on tokens used (cost per token).
• TTS: Charge based on audio duration (again, cost per second).
3. Calculate Costs
Then, use the collected metrics to figure out your costs:
# Example pricing
pricing = {
"stt": 0.024, # Cost per second
"llm": 0.002, # Cost per token
"tts": 0.022 # Cost per second
}
summary = usage_collector.get_summary()
total_cost = (
summary.stt_duration * pricing["stt"] +
summary.llm_tokens * pricing["llm"] +
summary.tts_duration * pricing["tts"]
)
print(f"Total Cost: ${total_cost:.2f}")
4. Integrate with Billing System
Once you have the costs, send them over to your billing system (like Stripe):
import stripe
stripe.api_key = "your-stripe-api-key"
invoice = stripe.Invoice.create(
customer="cus_123456789",
amount=round(total_cost * 100), # Convert to cents
currency="usd"
)
5. Reporting and Monitoring
It’s super helpful to keep an eye on usage patterns and generate reports, so consider this:
# Track daily usage
usage_collector.start_reporting_daily()
6. Schema for Storing Usage Data
Here’s a simple schema idea to store session and usage data neatly:
{
"session_id": "string", // Unique session identifier
"user_id": "string", // User identifier
"stt_duration": "number", // Total STT audio duration in seconds
"llm_tokens": "number", // Total LLM tokens used
"tts_duration": "number", // Total TTS audio duration in seconds
"total_cost": "number", // Calculated cost for the session
"timestamp": "timestamp" // Session timestamp
}
7. Final Notes
• Make sure pricing is accurate for each provider and model you’re using.
• Don’t forget to add proper error handling for those billing API calls — it’ll save headaches later.
• If you’re billing in multiple currencies, think about how you’ll handle currency conversion.
If you want to chat more about this or need help tweaking anything, just let me know — I’m here to help!