All Posts
AIVoice AITwilioAutomationPython

Building an Outbound AI Calling Campaign Engine: Concurrency, Retries, Voicemail Detection and Calling Hours

Hardik Devmurari· Full-Stack & AI EngineerNovember 23, 20268 min read

Getting an AI agent to place a single outbound call is a satisfying afternoon project. Running it across a contact list is a different problem entirely. Suddenly you are worrying about how many calls can run at once, what happens when nobody answers, whether you are calling at 3 a.m., and what a runaway conversation does to your bill.

This post covers the outbound campaign engine I built into voicelab, my self-hosted AI voice platform. The rules are the unglamorous part of voice AI, and they are what separates a demo from something you can leave running.


From One Call to a Campaign

In voicelab a campaign is a real entity, not a fire-and-forget loop:

  • An agent (persona, voice, tools) that makes the calls
  • A contact list, imported from CSV or entered by hand
  • A schedule: start manually, run once at a set time, or recur on chosen days
  • A per-number status, so you can see who was called, what happened and how many attempts it took

The dispatcher runs as an in-process async task, which keeps the architecture simple for a single-tenant deployment: no extra queue service to run. Each loop iteration asks a series of questions before it dials the next number.


The Dialer Loop, Step by Step

Before placing any call, the dispatcher checks, in order:

  1. Is the campaign still running? Pausing or deleting it stops the loop.
  2. Are there any open targets left? If not, the campaign is marked done.
  3. Are we allowed to dial right now? Calling hours and the daily limit are checked here.
  4. Is there a free call slot? If the live-call cap is reached, it waits a couple of seconds.
  5. Is anyone due? Numbers mid-call or waiting out a retry delay are skipped.
  6. Is the number still current? The number is re-read from the live contact right before dialing, so an edit made after the campaign was set up is respected.

Only then does it place the call through Twilio. This order matters. Cheap, local checks come first, and the expensive action (a real phone call) comes last.


Rule 1: Cap Live Calls to Your Provider Plans

Every live call holds open connections to your speech-to-text, LLM and text-to-speech providers, and those providers limit concurrency. voicelab enforces a live-call cap (MAX_CONCURRENT_CALLS, default 5), and it counts calls actually in flight, not the size of the batch.

A slot is only released when Twilio's end-of-call webhook arrives. If a webhook never comes, a reaper frees stale calls after the maximum call length plus a buffer, so a lost callback cannot block the campaign forever.

Set your concurrency cap at or below your voice provider's plan limit, and leave one slot spare. Hitting a provider's concurrency wall mid-call is a worse failure than a slightly slower campaign.


Rule 2: Pace Calls and Respect Calling Hours

Carriers do not like bursts. The engine enforces a minimum gap between call starts (DIAL_INTERVAL_SECONDS) and backs off when Twilio answers with a rate-limit error.

It also respects calling windows: by default 09:00 to 20:00 in the campaign's own timezone. Outside the window the dispatcher simply waits and re-checks every minute. A daily dial limit (default 30 attempts) adds a second safety net, which is especially useful when ramping a new number up gradually.

A small design choice I like: the window must be a same-day range. An overnight window such as 22:00 to 06:00 raises an error instead of silently never matching, which fails loudly rather than quietly dialing at the wrong time.


Rule 3: Record Real Outcomes, Then Retry Sensibly

A call that ends is not simply "done". The engine records the real Twilio outcome for every number:

Outcome What happens
Completed (answered) Marked completed, no retry
Busy Retried later
No answer Retried later
Voicemail Hung up, retried later
Failed or canceled Marked failed, no retry

Retryable outcomes get up to MAX_RETRIES extra attempts (default 2), spaced RETRY_DELAY_MINUTES apart (default 180). So a number is tried at most three times, hours apart, instead of being hammered. The decision logic lives in a small pure module with no database or network calls, which makes every rule easy to unit test with plain values.


Rule 4: Do Not Talk to Answering Machines

An AI agent happily delivering its pitch to a voicemail box wastes money and annoys people. voicelab turns on Twilio's answering machine detection. When a machine picks up, the call is ended and the number is queued for a later retry instead of being counted as a conversation.

It adds a small per-call fee, but for outbound campaigns it pays for itself by cutting off calls that would otherwise run until the maximum duration.


Rule 5: Do-Not-Call and Hard Cost Caps

Two last guards round out the engine:

  • A do-not-call list, managed through the API and the dashboard. Listed numbers are never dialed, regardless of which campaign they appear in.
  • A hard per-call time limit (MAX_CALL_SECONDS, default 600) so a conversation that never ends cannot run up the cost indefinitely. Ring time is capped too (30 seconds by default).

A /call-engine/status endpoint shows live calls, dials so far today and the active limits, so you can check the engine without digging through logs.


Lessons From Running It

  • Make every limit configurable, with safe defaults. Different clients have different provider plans and compliance needs.
  • Re-resolve data right before the action. Stale phone numbers and double-dial races were real bugs I fixed along the way.
  • Treat webhooks as unreliable. Always have a reaper for calls that never report back.
  • Keep decision logic pure. If the retry rules need a database to test, you will not test them.
  • Deploys pause campaigns. Restarting the service interrupts a running campaign and abandons that run's pending retries, so I only deploy when no calls are live.

If your AI agent is going to call real people, these guard rails are not optional. They are the product. I will share the per-stage costs and latency from my real test calls in the next post.


Hardik Devmurari is a full-stack and AI engineer at JRHMSquare, a software development collective specialising in full-stack, mobile and AI products.

Building something?

We are a team of full-stack, mobile, and AI engineers who ship production products. Let's talk about your project.

Get in touch