# intellwe/ai-calling-agent architecture

> Architecture diagram of the GitHub repository intellwe/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/intellwe/ai-calling-agent
- Repository: https://github.com/intellwe/ai-calling-agent

## Overview

AI Calling Agent is a FastAPI service that initiates recorded outbound Twilio calls, returns TwiML to connect calls to a media WebSocket, and bridges call audio with OpenAI Realtime for spoken responses. The media bridge forwards audio in both directions and handles speech-start interruption; OpenAI session behavior comes from the system prompt. Recording-status callbacks are a separate operational branch. The graph reflects the single inspected runtime module and README-documented capabilities; no separate recording persistence or compliance store is shown.

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

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_call_setup["Call Setup"]
  node_fastapi["FastAPI service<br/>[main.py]"]
  node_outbound["Outbound call<br/>[main.py]"]
  node_call_webhook["Call webhook<br/>[main.py]"]
  node_recording_status["Recording callback<br/>[main.py]"]
  node_recording["Call recording<br/>[main.py]"]
end

subgraph group_voice_runtime["Voice Runtime"]
  node_media_bridge["Media WebSocket bridge<br/>[main.py]"]
  node_twilio_stream["Twilio media stream"]
  node_audio_forward["Inbound audio forwarding<br/>[main.py]"]
  node_audio_return["Response audio forwarding<br/>[main.py]"]
  node_interrupt["Speech interruption<br/>[main.py]"]
end

subgraph group_ai_session["AI Session"]
  node_openai_realtime["OpenAI Realtime API"]
  node_session_config["Session configuration<br/>[main.py]"]
  node_system_prompt["System prompt<br/>[system_prompt.txt]"]
end

node_caller(("Call initiator"))
node_twilio_voice["Twilio Voice"]

node_caller -->|"requests call"| node_outbound
node_outbound -->|"creates recorded call"| node_twilio_voice
node_twilio_voice -->|"requests TwiML"| node_call_webhook
node_call_webhook -->|"starts stream"| node_twilio_stream
node_twilio_stream -->|"connects WebSocket"| node_media_bridge
node_media_bridge -->|"opens WebSocket"| node_openai_realtime
node_media_bridge -->|"configures session"| node_session_config
node_session_config -->|"loads instructions"| node_system_prompt
node_media_bridge -->|"dispatches receive task"| node_audio_forward
node_audio_forward -->|"sends input audio"| node_openai_realtime
node_media_bridge -->|"dispatches send task"| node_audio_return
node_audio_return -->|"sends response audio"| node_twilio_stream
node_audio_return -->|"handles speech start"| node_interrupt
node_interrupt -->|"clears playback"| node_twilio_stream
node_interrupt -->|"cancels response"| node_openai_realtime
node_twilio_voice -->|"posts status"| node_recording_status
node_outbound -->|"enables recording"| node_recording
node_fastapi -->|"routes request"| node_outbound
node_fastapi -->|"routes webhook"| node_call_webhook
node_fastapi -->|"routes callback"| node_recording_status
node_fastapi -->|"routes WebSocket"| node_media_bridge

click node_fastapi "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_outbound "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_call_webhook "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_recording_status "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_media_bridge "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_audio_forward "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_audio_return "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_interrupt "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_session_config "https://github.com/intellwe/ai-calling-agent/blob/master/main.py"
click node_system_prompt "https://github.com/intellwe/ai-calling-agent/blob/master/prompts/system_prompt.txt"
click node_recording "https://github.com/intellwe/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_outbound,node_call_webhook,node_recording_status,node_recording toneBlue
class node_media_bridge,node_twilio_stream,node_audio_forward,node_audio_return,node_interrupt toneAmber
class node_openai_realtime,node_session_config,node_system_prompt toneMint
class node_caller,node_twilio_voice toneIndigo
```

## Components

### Call Setup

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

### Voice Runtime

- [Media WebSocket bridge](https://github.com/intellwe/ai-calling-agent/blob/master/main.py): `main.py`
- **Twilio media stream**
- [Inbound audio forwarding](https://github.com/intellwe/ai-calling-agent/blob/master/main.py): `main.py`
- [Response audio forwarding](https://github.com/intellwe/ai-calling-agent/blob/master/main.py): `main.py`
- [Speech interruption](https://github.com/intellwe/ai-calling-agent/blob/master/main.py): `main.py`

### AI Session

- **OpenAI Realtime API**
- [Session configuration](https://github.com/intellwe/ai-calling-agent/blob/master/main.py): `main.py`
- [System prompt](https://github.com/intellwe/ai-calling-agent/blob/master/prompts/system_prompt.txt): `prompts/system_prompt.txt`

### Other components

- **Call initiator**
- **Twilio Voice**

## Connections

- Call initiator → Outbound call: requests call (evidence: `README.md`)
- Outbound call → Twilio Voice: creates recorded call (evidence: `main.py`)
- Twilio Voice → Call webhook: requests TwiML (evidence: `README.md`)
- Call webhook → Twilio media stream: starts stream (evidence: `main.py`)
- Twilio media stream → Media WebSocket bridge: connects WebSocket (evidence: `README.md`)
- Media WebSocket bridge → OpenAI Realtime API: opens WebSocket (evidence: `main.py`)
- Media WebSocket bridge → Session configuration: configures session (evidence: `main.py`)
- Session configuration → System prompt: loads instructions (evidence: `main.py`)
- Media WebSocket bridge → Inbound audio forwarding: dispatches receive task (evidence: `main.py`)
- Inbound audio forwarding → OpenAI Realtime API: sends input audio (evidence: `main.py`)
- Media WebSocket bridge → Response audio forwarding: dispatches send task (evidence: `main.py`)
- Response audio forwarding → Twilio media stream: sends response audio (evidence: `main.py`)
- Response audio forwarding → Speech interruption: handles speech start (evidence: `main.py`)
- Speech interruption → Twilio media stream: clears playback (evidence: `main.py`)
- Speech interruption → OpenAI Realtime API: cancels response (evidence: `main.py`)
- Twilio Voice → Recording callback: posts status (evidence: `README.md`)
- Outbound call → Call recording: enables recording (evidence: `main.py`)
- FastAPI service → Outbound call: routes request (evidence: `main.py`)
- FastAPI service → Call webhook: routes webhook (evidence: `main.py`)
- FastAPI service → Recording callback: routes callback (evidence: `main.py`)
- FastAPI service → Media WebSocket bridge: routes WebSocket (evidence: `main.py`)
