So I currently have my livekit agents running in a...
# ask-ai
f
So I currently have my livekit agents running in a AWS ECS task, and I want to scale up my agents to handle concurrent calls. it's reasonable to scale up the memory and cpu within the task definition and monitoring, but I've been running into hurdles finding a good way to rotate api keys for my gemini llm, deepgram tts and elevenlabs stt models (I'm not ready to bump up my billing plan for increased api usage with each provider yet). I'm leaning towards using a redis cache to update with active keys with a lambda to evaluate the usage to trigger the keys to switch, but should I detect when my models (the llm, tts, or stt) are reaching their max usage for livekit? Also, how does livekit cloud manage this scaling?
t
Thanks for reaching out, Akash. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you reached out! Let me walk you through how LiveKit Cloud handles scaling for concurrent calls. It uses a distributed mesh architecture, which means multiple SFU instances can relay streams directly between servers. This clever design helps avoid any single-server bottlenecks and comfortably supports up to 100,000 simultaneous participants per session right out of the box. Plus, if you ever need more, it can scale even further upon request. Since the infrastructure is spread globally, you can expect low latency and a smooth, high-quality real-time experience. When it comes to resource allocation, LiveKit smartly uses elastic capacity scaling and stateful load balancing. What happens is, if an agent server is nearing its load limit, new instances automatically spin up and traffic is smoothly directed to them. This way, any sudden traffic spikes are handled gracefully without affecting your service. Also, you have access to a handy dashboard where you can monitor important metrics like resource consumption and session latency, which can really help you optimize usage over time. About your idea of rotating API keys for your models using a Redis cache with a Lambda function to check usage and switch keys—that’s a solid approach! To make it even more effective, you might consider adding monitoring at the application level to keep an eye on API usage and set thresholds for when to switch keys. This way, you’ll have a clear way to manage usage and avoid hitting limits. If you need any help setting this up or want to explore other options, I’m here to support you every step of the way. Sources: Deploy and scale agents on LiveKit Cloud | Deploy and scale agents on LiveKit Cloud | Deploy and scale agents on LiveKit Cloud | LiveKit Pricing | LiveKit Cloud Architecture | LiveKit Docs
f
how would i add monitoring at the application level to keep an eye on api usage for the different models, could you provide an example?
t
I'd be happy to help you add application-level monitoring for your API usage! A great way to do this is by setting up a system that keeps track of your API calls and monitors usage limits. Here's a friendly step-by-step approach you might find useful: 1. Set up a monitoring service: Consider using tools like Prometheus, Datadog, or even a custom solution to gather and visualize your metrics. You can log API requests and responses, including details like timestamps, status codes, and usage counts, to get a clear picture. 2. Integrate with your application: You can add middleware or hooks within your app to catch API calls. For example, if you're using Node.js with Express, a middleware can help log each request and response seamlessly. 3. Track usage thresholds: It’s important to define limits for each API key. Say an API key allows 10,000 requests per month—you’d want to keep track of how many requests have been made and get alerted as you approach that limit. 4. Automate key rotation: Once a threshold is reached, you can automate switching to a new API key using a function like AWS Lambda. Make sure to store your active keys securely, maybe with AWS Secrets Manager or a Redis cache. 5. Visualize and alert: Dashboards can be a big help here, letting you see your API usage at a glance and setting up alerts so you’re always in the know and ready to act. If you'd like, I can also provide a code example tailored to your favorite programming language or framework—just let me know! Sources: Announcing LiveKit Cloud | Webhook & Event Notification System | livekit/livekit_composite | DeepWiki | Analytics API | LiveKit Docs | pkg/telemetry/telemetryservice.go | protobufs/livekit_analytics.proto