Building an AI Calling Agent with FastAPI and CRM Integration
This article outlines the architecture and components required to build an AI calling agent and auto dialer integrated with a CRM, emphasizing compliance, structured workflows, and the use of FastAPI for the control…

An AI calling agent and auto dialer are constructed as a reliable pipeline, not a single LLM prompt. The system includes a lead queue, a preflight check, a dialer worker, an explicit call-state machine, a speech path, CRM writeback, and operator controls. This architecture assumes permission-based, compliant outreach only, with no tricks for spam dialing (Dev.to, 2023). [1]
The high-level architecture consists of six key components: [Lead / Carrier CRM], [FastAPI Operator API + Dashboard], [Dialer Worker], [Call-State Machine], [STT → LLM → TTS pipeline], and [Artifacts: recordings, transcripts, outcomes]. The CRM stores carriers and leads, while FastAPI serves as the control plane for human operators (Dev.to, 2023). [1]
The call-state machine ensures clear transitions between states such as idle, preflight, dialing, ringing, connected, voicemail, completed, and failed. This explicit state machine prevents UI bugs and double-dials by making transitions explicit and enforcing compliance flags before dialing (Dev.to, 2023). [1]
The CRM writeback system involves inserting call records into a database, including details such as contact ID, start and end times, state, outcome, and transcript paths. This ensures that all call data is logged and accessible for review and analysis (Dev.to, 2023). [1]
Advertisement
The AI calling agent and auto dialer described in the Dev.to article is built as a multi-component system, with specific technical and operational scope. The project is structured around six core boxes: a lead or carrier CRM, a FastAPI-based operator API and dashboard, a dialer worker, a call-state machine, an STT → LLM → TTS speech pipeline, and a repository for artifacts such as recordings and transcripts. The architecture assumes compliance with permission-based outreach, with no support for spam dialing. The system includes a dry-run mode, which allows testing without making real calls, and a preflight check that ensures audio devices are present, dial surfaces are reachable, and campaign data is valid before initiating calls. The dialer worker is responsible for managing the long-running dial loop, which is not placed inside a FastAPI request handler due to performance and scalability considerations. The call-state machine ensures predictable behavior by explicitly defining allowed transitions between call states such as idle, preflight, dialing, ringing, connected, voicemail, completed, and failed. This structure prevents UI bugs and double-dials by enforcing clear state transitions. The speech pipeline is designed to be swappable, allowing for the use of either cloud or local models for speech-to-text and text-to-speech processing. Artifacts like recordings and transcripts are stored on disk or in object storage, with their paths referenced from the CRM database. The FastAPI control plane provides typed request/response models, OpenAPI documentation, and WebSocket or SSE hooks for live status updates. The system uses a PostgreSQL-based CRM for data storage, with tables for accounts, contacts, calls, outcomes, and tasks. The project is aimed at logistics and outbound teams, with a focus on self-hosted solutions that allow teams to maintain control over their data and telephony infrastructure. The article also includes sample code for defining call states, handling transitions, and implementing a dry-run dialer. [1]
The project’s technical implementation includes specific code examples and architecture considerations. The FastAPI application defines an enum for call states, including idle, preflight, dialing, ringing, connected, voicemail, completed, and failed. A StartRunRequest model is used to initiate a dialing run, specifying a campaign ID, dry-run flag, and maximum number of calls. The FastAPI endpoint for starting a run checks if a worker is busy, enqueues the run, and returns a run ID along with the initial call state. The call-state machine enforces allowed transitions between states, preventing illegal state changes by checking a predefined set of allowed transitions. For example, a call can only transition from idle to preflight, and from preflight to dialing, failed, or idle. The dialer worker uses a port/interface abstraction to allow swapping between dry-run, PBX, or browser automation implementations without rewriting the CRM. A DryRunDialer class is provided as an example, which logs dial attempts without actually connecting to a phone line. The speech pipeline is structured as a staged process: STT reads partial audio input, the LLM decides the next utterance based on the lead’s information and the current transcript, and TTS generates the response. On hangup, the transcript is finalized and written back to the CRM. The CRM writeback process involves inserting data into PostgreSQL tables for calls, outcomes, and tasks. The system also includes compliance features, such as campaign-level permission checks, quiet hour configurations, and logging of who started a run and with which list version. The project is aimed at logistics and outbound teams, with a focus on self-hosted solutions that allow teams to maintain control over their data and telephony infrastructure. The article also includes sample code for the call-state machine, dialer worker, and CRM writeback process. [1]
An AI calling agent and auto dialer are constructed as a reliable pipeline, not a single LLM prompt. The high-level architecture consists of six key components: [Lead / Carrier CRM], [FastAPI Operator API + Dashboard], [Dialer Worker], [Call-State Machine], [STT → LLM → TTS pipeline], and [Artifacts: recordings, transcripts, outcomes]. This structure ensures that the system operates with predictable dial behavior and clear call states, such as idle, preflight, dialing, ringing, connected, voicemail, completed, and failed (Dev.to, 2023). [1]
The CRM writeback system involves inserting call records into a database, including details such as contact ID, start and end times, state, outcome, and transcripts. This allows for follow-ups and outcomes to be tracked within a CRM-ish screen, ensuring that all call data is tied to a carrier or lead record (Dev.to, 2023). [1]
Advertisement
The call-state machine is the source of truth for what the UI shows, making transitions explicit to avoid UI bugs and double-dials. Allowed transitions between states are defined, ensuring that the system only permits valid state changes, such as from idle to preflight, or from ringing to connected or voicemail (Dev.to, 2023). [1]
An AI calling agent and auto dialer are constructed as a reliable pipeline, not a single LLM prompt. The high-level architecture consists of six key components: [Lead / Carrier CRM], [FastAPI Operator API + Dashboard], [Dialer Worker], [Call-State Machine], [STT → LLM → TTS pipeline], and [Artifacts: recordings, transcripts, outcomes]. These components ensure structured and predictable operation of the dialer system (Dev.to, 1). [1]
The call-state machine ensures clear transitions between states such as idle, preflight, dialing, ringing, connected, voicemail, completed, and failed. This explicit state management is critical to avoid UI bugs and double-dials, as it provides a source of truth for the user interface (Dev.to, 1). [1]
The CRM writeback system involves inserting call records into a database, including details such as contact ID, start and end times, state, outcome, and transcripts. This integration allows for seamless tracking and follow-up within the CRM, ensuring that all call data is centralized and accessible (Dev.to, 1). [1]
Advertisement
The Cali Reporter newsletter
The stories worth your time.
California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.
More in Technology
- Modern Web Guidance 1.0 and Browser Compatibility
Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…
- Apache mod_http2 Vulnerability Detection with Nmap
A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.
- Securing Remote Vendor Access in Industrial Networks
Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.
