# fardinhash/ai-calling-agent architecture

> Architecture diagram of the GitHub repository fardinhash/ai-calling-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fardinhash/ai-calling-agent
- Repository: https://github.com/fardinhash/ai-calling-agent

## Overview

AI Calling Agent is a FastAPI service for initiating recorded outbound calls and bridging Twilio voice streams to OpenAI Realtime. The principal flow is call request → Twilio call → webhook-generated media stream → bidirectional AI conversation; recording callbacks and prompt-based session configuration are supporting capabilities. Runtime relationships are grounded in main.py and documented architecture in README.md; this compact repository implements most behavior in one file.

Main entry points: [`main.py`](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py) (Media stream handler), [`prompts/system_prompt.txt`](https://github.com/fardinhash/ai-calling-agent/blob/master/prompts/system_prompt.txt) (System prompt)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_call["Call orchestration"]
  node_fastapi["FastAPI service<br/>[main.py]"]
  node_make_call["Outbound call endpoint<br/>[main.py]"]
  node_call_webhook["Call webhook<br/>[main.py]"]
  node_recording["Recording callback<br/>[main.py]"]
  node_health["Health endpoint<br/>[main.py]"]
end

subgraph group_audio["Realtime conversation"]
  node_media_stream["Media stream handler<br/>[main.py]"]
  node_session_setup["Session configuration<br/>[main.py]"]
end

subgraph group_config["Configuration"]
  node_prompt_loader["Prompt loader<br/>[main.py]"]
  node_system_prompt["System prompt<br/>[system_prompt.txt]"]
end

node_caller(("Call initiator"))
node_twilio["Twilio Voice"]
node_openai["OpenAI Realtime"]

node_caller -->|"requests call"| node_make_call
node_make_call -->|"creates recorded call"| node_twilio
node_twilio -->|"requests call instructions"| node_call_webhook
node_call_webhook -->|"configures stream"| node_media_stream
node_twilio -->|"streams audio"| node_media_stream
node_media_stream -->|"sends audio"| node_openai
node_openai -->|"returns audio events"| node_media_stream
node_media_stream -->|"sends audio"| node_twilio
node_media_stream -->|"configures session"| node_session_setup
node_session_setup -->|"sends settings"| node_openai
node_session_setup -->|"uses instructions"| node_system_prompt
node_prompt_loader -->|"loads text"| node_system_prompt
node_make_call -->|"sets callback"| node_recording
node_twilio -->|"posts recording status"| node_recording
node_caller -->|"checks service"| node_health

click node_fastapi "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_make_call "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_call_webhook "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_recording "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_media_stream "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_session_setup "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_prompt_loader "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"
click node_system_prompt "https://github.com/fardinhash/ai-calling-agent/blob/master/prompts/system_prompt.txt"
click node_health "https://github.com/fardinhash/ai-calling-agent/blob/master/main.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_fastapi,node_make_call,node_call_webhook,node_recording,node_health toneBlue
class node_media_stream,node_session_setup toneAmber
class node_prompt_loader,node_system_prompt toneMint
class node_caller,node_twilio,node_openai toneIndigo
```

## Components

### Call orchestration

- [FastAPI service](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [Outbound call endpoint](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [Call webhook](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [Recording callback](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [Health endpoint](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`

### Realtime conversation

- [Media stream handler](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [Session configuration](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`

### Configuration

- [Prompt loader](https://github.com/fardinhash/ai-calling-agent/blob/master/main.py): `main.py`
- [System prompt](https://github.com/fardinhash/ai-calling-agent/blob/master/prompts/system_prompt.txt): `prompts/system_prompt.txt`

### Other components

- **Call initiator**
- **Twilio Voice**
- **OpenAI Realtime**

## Connections

- Call initiator → Outbound call endpoint: requests call (evidence: `README.md`)
- Outbound call endpoint → Twilio Voice: creates recorded call (evidence: `main.py`)
- Twilio Voice → Call webhook: requests call instructions (evidence: `main.py`)
- Call webhook → Media stream handler: configures stream (evidence: `main.py`)
- Twilio Voice → Media stream handler: streams audio (evidence: `README.md`)
- Media stream handler → OpenAI Realtime: sends audio (evidence: `main.py`)
- OpenAI Realtime → Media stream handler: returns audio events (evidence: `main.py`)
- Media stream handler → Twilio Voice: sends audio (evidence: `main.py`)
- Media stream handler → Session configuration: configures session (evidence: `main.py`)
- Session configuration → OpenAI Realtime: sends settings (evidence: `main.py`)
- Session configuration → System prompt: uses instructions (evidence: `main.py`)
- Prompt loader → System prompt: loads text (evidence: `main.py`)
- Outbound call endpoint → Recording callback: sets callback (evidence: `main.py`)
- Twilio Voice → Recording callback: posts recording status (evidence: `main.py`)
- Call initiator → Health endpoint: checks service (evidence: `main.py`)
