Hi everyone, I have built a custom AI outbound cal...
# ask-ai
d
Hi everyone, I have built a custom AI outbound calling system using LiveKit Agents, FastAPI, and Streamlit. Here is a quick overview of how my current bot works: • I upload a call script in Streamlit • I generate agent instructions dynamically • I register a campaign and upload calling data (phone numbers) • FastAPI stores the data in PostgreSQL • Then I run a single outbound call using an agent where LiveKit handles STT, TTS, and the agent session • Everything works fine for 1 call at a time Now I want to scale this into a multi-call, multi-campaign voicebot system, and I want to follow the official or recommended approach for implementing multi-threaded (concurrent) outbound calls in LiveKit. Here are my specific questions: Is it possible to run multiple voice-bot calls at the same time? For example: If a campaign has 100 numbers, can I run 5–10 calls simultaneously? Is it possible to run multiple campaigns in parallel? Example: • Campaign A → 10 calls at once • Campaign B → 5 calls at once Both running at the same time, each with its own script and agent instructions. How does LiveKit handle concurrency for agent sessions? Does every outbound call become a separate Agent Session inside the Worker? Or is there another mechanism for concurrency? What is the recommended architecture for scaling? Should I: • Run one Worker using asyncio to manage multiple calls? • Run multiple Worker replicas in parallel? • Use some type of outbound-call scheduler or queue system? • Something else recommended?
t
Thanks for reaching out, Zahra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you scale your voicebot system for handling multiple calls and campaigns smoothly, here are some important points to consider: • Concurrent Calls: You can definitely run several voicebot calls at the same time by using
asyncio
. For example,
asyncio.gather()
allows you to process multiple calls in parallel, and you can use a semaphore to control how many calls run concurrently, keeping things efficient. • Multiple Campaigns: Each campaign can operate its own batch of concurrent calls. So, Campaign A might handle 10 calls while Campaign B manages 5, all running side by side without any hiccups. • Agent Sessions: Every outbound call is treated as its own agent session. This way, each call is managed independently within the LiveKit system, giving you clear control. • Recommended Architecture: Here’s a solid approach to consider: 1. Run a single worker using
asyncio
to juggle multiple calls at once. 2. Scale up horizontally by adding more worker replicas to boost your system’s capacity. 3. Set up an outbound-call scheduler or queue to organize call distribution and handle retries smoothly. If you'd like, I’m happy to walk you through detailed implementation examples or dive deeper into any of these areas. Just let me know how I can assist you further! Sources: telephony/survey_caller/make_survey_calls.py | Troubleshooting Guide | livekit/livekit_composite | DeepWiki
d
Thank you so much for the helpful explanation, this makes things much clearer! Yes, I would really appreciate a detailed implementation guide. Specifically, I want to understand: 1. How to structure the outbound-call scheduler 2. How to use asyncio + semaphores to manage multiple concurrent calls 3. How each outbound call should create its own agent session in LiveKit 4. How to run multiple campaigns in parallel while keeping their scripts/instructions separate 5. How to scale workers (1 worker vs multiple replicas) 6. Any code examples showing: ◦ fetching unprocessed phone numbers ◦ launching multiple outbound calls ◦ tracking call status ◦ updating FastAPI/Postgres after each call ◦ dispatching new calls when previous ones complete If you can share a sample architecture or code snippet for a full workflow (scheduler → outbound call → agent session → callback/update), that would be extremely helpful. Thank you again, looking forward to the detailed implementation!
t
I'm excited to share a detailed guide to help you implement your multi-call, multi-campaign voicebot system smoothly: • Outbound-Call Scheduler: Think of the scheduler as your friendly assistant that fetches unprocessed phone numbers from your database or CSV, then dispatches calls using LiveKit’s API. Each call carries important details like the phone number, campaign ID, and script info to keep things organized. • Managing Concurrency with
asyncio
and Semaphores:
To keep everything running nicely without overload, we use
asyncio.Semaphore
to limit how many calls happen at once. Here’s a simple example to guide you:
Copy code
semaphore = asyncio.Semaphore(10)  # Limit to 10 concurrent calls

async def controlled_make_call(phone_number, metadata):
    async with semaphore:
        return await make_call(phone_number, metadata)

async def process_calls_concurrently(call_list):
    tasks = [controlled_make_call(item['phone_number'], item['metadata']) for item in call_list]
    await asyncio.gather(*tasks)
• Creating Agent Sessions: Every outbound call gets its own agent session. You can use
create_dispatch
to set up the session and
create_sip_participant
to connect the call seamlessly. Here’s how:
Copy code
dispatch = await lkapi.agent_dispatch.create_dispatch(
    api.CreateAgentDispatchRequest(
        agent_name="voice-agent",
        room=room_name,
        metadata=json.dumps(metadata)
    )
)

sip_participant = await lkapi.sip.create_sip_participant(
    api.CreateSIPParticipantRequest(
        room_name=room_name,
        sip_trunk_id="your_trunk_id",
        sip_call_to=phone_number,
        participant_identity=f"user_{phone_number}"
    )
)
• Running Multiple Campaigns: Keep your campaigns nicely organized by assigning unique identifiers and metadata. This way, each campaign can run its own set of concurrent calls, managed either by a shared or separate worker process. • Scaling Workers: Start simple with one worker managing multiple calls using
asyncio
. When you need more power, you can easily scale horizontally by running multiple worker replicas, each handling different campaign subsets. • Full Workflow Example: To give you a clearer picture, here’s a sample architecture you might find helpful: 1. Scheduler: Fetch unprocessed numbers from PostgreSQL. 2. Outbound Call: Launch calls using
asyncio
and LiveKit’s API. 3. Call Status: Keep track of call progress and update your database once calls finish. 4. Callback: Dispatch new calls as previous ones wrap up.
Copy code
async def main():
    calls = fetch_calls_from_db()
    async with ConcurrentOutboundCaller(max_concurrent=10) as caller:
        results = await caller.make_concurrent_calls(calls)
        update_call_status_in_db(results)
If you’d like, I’m here to help with more details or walk you through any specific part of this implementation. Just let me know! Sources: Making outbound calls | LiveKit docs | README.md | telephony/survey_caller/make_survey_calls.py | livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | App templates | LiveKit docs