Cost & Usage Dashboard
The Cost & Usage dashboard gives super-admins a unified view of AI and Twilio spending across the platform. It aggregates AI usage logs, Twilio voice/SMS/phone number costs, and renders breakdowns by model, provider, feature, and organization — with sparkline trends, efficiency metrics, and BYO Twilio distinction.
API Endpoint
Section titled “API Endpoint”GET /api/admin/ai-costs?time_range=<TimeRange>Auth: Super-admin only (checked via ensure_super_admin).
Query parameter: time_range — a TimeRange enum value (Last7Days, Last30Days, Last90Days, AllTime).
Response: AdminAiCostsData containing KPIs, breakdowns, time-series data, and Twilio costs.
Response Shape
Section titled “Response Shape”pub struct AdminAiCostsData { // AI cost KPIs pub total_cost: f64, pub previous_total_cost: f64, pub cost_change_pct: f64, pub total_api_calls: u64, pub avg_cost_per_call: f64, pub cost_per_day_avg: f64, pub top_model: String, pub top_model_cost: f64,
// Token totals pub total_input_tokens: u64, pub total_output_tokens: u64,
// Breakdowns pub cost_by_model: Vec<AdminAiCostBreakdownItem>, pub cost_by_org: Vec<AdminAiCostOrgItem>, pub cost_by_provider: Vec<AdminAiCostBreakdownItem>, pub cost_by_feature: Vec<AdminAiCostBreakdownItem>,
// Time series — single line pub cost_series: Vec<AdminCostChartPoint>,
// Grouped time series — stacked charts pub cost_series_labels: Vec<String>, pub cost_series_by_model: Vec<AdminCostSeriesGroup>, pub cost_series_by_feature: Vec<AdminCostSeriesGroup>, pub cost_series_by_provider: Vec<AdminCostSeriesGroup>,
// Twilio pub twilio: AdminTwilioCostsData,
// Combined total pub grand_total_cost: f64,
pub time_range: TimeRange, pub range_label: String,}Breakdown Items
Section titled “Breakdown Items”Each breakdown item carries cost, percentage, and optional token/efficiency metrics:
pub struct AdminAiCostBreakdownItem { pub label: String, pub cost: f64, pub percentage: f64, pub api_calls: Option<u64>, pub input_tokens: Option<u64>, pub output_tokens: Option<u64>, pub sub_items: Option<Vec<AdminAiCostBreakdownItem>>, pub trend: Option<Vec<f64>>, pub cost_per_1k_tokens: Option<f64>, pub cache_hit_pct: Option<f64>,}trend— sparkline values aligned with time-series bucketscost_per_1k_tokens— efficiency metric: total cost ÷ total tokens × 1000cache_hit_pct— cached tokens ÷ (input + cached tokens)sub_items— nested breakdown (e.g., models within a feature)
Per-Organization Items
Section titled “Per-Organization Items”pub struct AdminAiCostOrgItem { pub organization_id: Uuid, pub organization_name: String, pub total_cost: f64, pub openrouter_cost: f64, pub openai_cost: f64, pub gemini_cost: f64, pub top_feature: String, pub api_calls: u64, pub input_tokens: u64, pub output_tokens: u64,}Grouped Time Series
Section titled “Grouped Time Series”pub struct AdminCostSeriesGroup { pub label: String, // e.g., "GPT-4o", "Voice Agent", "OpenAI" pub values: Vec<f64>, // Aligned with cost_series_labels}The backend selects the top 5 groups by total cost and aggregates the rest into an "Other" group.
Twilio Costs
Section titled “Twilio Costs”The dashboard aggregates Twilio expenses from call, message, phone_number, and organization_twilio_settings tables.
Response Shape
Section titled “Response Shape”pub struct AdminTwilioCostsData { pub voice_total_minutes: f64, pub voice_total_cost: f64, pub voice_cost_per_minute: f64, pub voice_call_count: u64, pub phone_number_count: u64, pub phone_number_monthly_cost: f64, pub sms_count: u64, pub sms_total_cost: f64, pub sms_inbound_count: u64, pub sms_outbound_count: u64, pub total_twilio_cost: f64, pub loquent_twilio_cost: f64, pub cost_by_org: Vec<AdminTwilioCostOrgItem>,}Per-Organization Twilio
Section titled “Per-Organization Twilio”pub struct AdminTwilioCostOrgItem { pub organization_id: Uuid, pub organization_name: String, pub voice_minutes: f64, pub voice_cost: f64, pub call_count: u64, pub phone_numbers: u64, pub phone_number_cost: f64, pub sms_count: u64, pub sms_cost: f64, pub is_byo: bool,}BYO vs Loquent-Managed
Section titled “BYO vs Loquent-Managed”Organizations using their own Twilio credentials (is_byo: true in organization_twilio_settings) are tracked separately:
total_twilio_cost— all organizations, including BYOloquent_twilio_cost— excludes BYO organizationsgrand_total_cost— AI cost +loquent_twilio_cost(reflects actual platform spend)
BYO organizations display a badge in the Twilio org table.
Twilio Pricing Constants
Section titled “Twilio Pricing Constants”Defined in src/mods/admin/services/twilio_pricing.rs:
| Constant | Value | Description |
|---|---|---|
TWILIO_VOICE_PER_MINUTE | $0.014 | US programmable voice |
TWILIO_PHONE_NUMBER_MONTHLY | $1.15 | Local US number monthly fee |
TWILIO_SMS_OUTBOUND_PER_SEGMENT | $0.0079 | Outbound SMS per segment |
TWILIO_SMS_INBOUND_PER_SEGMENT | $0.0075 | Inbound SMS per segment |
Phone number cost is prorated by the selected time range: count × $1.15 × (days_in_range / 30).
AI Pricing Engine
Section titled “AI Pricing Engine”The get_model_pricing() function maps (provider, model) pairs to per-1M-token rates. The calculate_cost() function applies these rates to a usage log row’s token counts.
pub struct ModelPricing { pub input_per_1m: f64, pub output_per_1m: f64, pub cached_per_1m: f64, pub audio_input_per_1m: f64, pub audio_output_per_1m: f64, pub audio_per_minute: f64,}Supported models and rates:
| Provider | Model | Input | Output | Audio In | Audio Out |
|---|---|---|---|---|---|
| any | deepseek/deepseek-v3.2 | $0.25 | $0.40 | — | — |
| any | google/gemini-3.1-pro-preview | $2.00 | $12.00 | — | — |
| any | google/gemini-3.1-flash-lite-preview | $0.25 | $1.50 | — | — |
| any | anthropic/claude-sonnet-4.6 | $3.00 | $15.00 | — | — |
| openai | gpt-realtime-mini | $0.60 | $2.40 | $10.00 | $20.00 |
| openai | gpt-realtime / gpt-realtime-1.5 | $4.00 | $16.00 | $32.00 | $64.00 |
| gemini | gemini-live-2.5-flash-native-audio | $0.50 | $12.00 | — | — |
| openai | gpt-4o-transcribe | $2.50 | $10.00 | $6.00 | — |
Unknown models fall back to zero-cost with a warning log.
Cost formula:
cost = (input_tokens × input_rate + output_tokens × output_rate + cached_tokens × cached_rate + input_audio_tokens × audio_input_rate + output_audio_tokens × audio_output_rate) / 1,000,000 + audio_duration_secs / 60 × audio_per_minuteDashboard Layout
Section titled “Dashboard Layout”The UI renders six sections:
-
KPI cards (3×2 grid):
- Grand Total (AI + Twilio)
- AI Spend (with period-over-period change)
- Twilio Spend (Loquent-managed only if BYO orgs exist)
- API Calls
- Tokens Used (input/output breakdown)
- Voice Minutes (call count and cost)
-
Grouped cost chart — filterable stacked bar chart with modes: Total, By Model, By Feature, By Provider
-
Breakdown cards (3 columns) — Model, Provider, and Feature breakdowns with sparkline trends, token counts, efficiency metrics (
$/1K tokens, cache hit %), and nested model details for features -
AI cost by organization table — per-org totals with API calls, input/output tokens, and provider splits
-
Twilio expense cards (3 cards) — Voice (minutes, cost), Phone Numbers (count, monthly cost), SMS (inbound/outbound counts, cost)
-
Twilio cost by organization table — per-org voice, calls, numbers, SMS, with BYO badge
Adding a New Model
Section titled “Adding a New Model”- Add a match arm in
get_model_pricing()atsrc/mods/ai/types/ai_pricing_type.rs - Set the per-1M-token rates from the provider’s pricing page
- The cost dashboard picks up the new model automatically on next query
Key Files
Section titled “Key Files”| File | Purpose |
|---|---|
src/mods/ai/types/ai_pricing_type.rs | ModelPricing, get_model_pricing(), calculate_cost() |
src/mods/admin/services/admin_ai_cost_service.rs | Aggregation queries and breakdown computation |
src/mods/admin/services/twilio_pricing.rs | Twilio pricing constants |
src/mods/admin/api/get_admin_ai_costs_api.rs | GET /api/admin/ai-costs endpoint |
src/mods/admin/types/admin_data_types.rs | Response types |
src/mods/admin/components/admin_ai_costs_tab_component.rs | Dashboard UI components |