Architecture Overview
VoiceGateway is cost tracking and reconciliation for LiveKit voice agents. It returns native LiveKit STT, LLM, and TTS plugin instances across cloud providers and local models, with modality-aware unit accounting (audio-minutes, tokens, characters), resolver-time fallback chains, rate limiting, budget enforcement, avoicegw reconcile command, and a web dashboard.
System Architecture
Request Flow
Every call togateway.stt(), gateway.llm(), or gateway.tts() follows the same path:
Directory Structure
The Python package lives undervoicegateway/. A separate dashboard/
tree carries the FastAPI backend and the React + TypeScript + Vite +
Recharts frontend for the local cost dashboard.
Design Principles
- Async throughout — all database, HTTP, and provider operations use async/await. The Gateway provides synchronous wrapper methods for convenience.
-
Lazy loading — providers are only imported and instantiated on first use.
pip install voicegateway[openai]installs only the OpenAI SDK. -
Transparent instrumentation —
InstrumentedSTT/LLM/TTSwrappers proxy all attribute access via__getattr__, so user code sees the exact same API as the underlying provider instance. -
Config layering — three sources merged at startup: environment variables (highest priority), SQLite managed tables (dashboard/MCP writes), and YAML (base config). Each resource carries a
sourcefield ("yaml"or"db"). -
Encryption at rest — all API keys stored in SQLite are encrypted with Fernet (AES-128-CBC + HMAC-SHA256). Keys in API responses are masked to
secr...2345format.
Key Components
| Component | File | Purpose |
|---|---|---|
| Gateway Core | core/gateway.py | Main orchestrator, entry point for all requests |
| Provider Abstraction | providers/base.py | ABC for all 11 provider implementations |
| Middleware | middleware/ | Cost, latency, rate limiting, fallback, budget |
| Cost Tracking | pricing/, middleware/cost_tracker.py | Per-request cost calculation, pricing layer, streaming validation |
| Storage | storage/sqlite.py | SQLite schema, tables, views, indexes |
| Config Layers | core/config_manager.py | YAML + SQLite + env merge strategy |
| Security | core/crypto.py | Fernet encryption, secret management, masking |