Skip to content

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.

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.

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,
}

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 buckets
  • cost_per_1k_tokens — efficiency metric: total cost ÷ total tokens × 1000
  • cache_hit_pct — cached tokens ÷ (input + cached tokens)
  • sub_items — nested breakdown (e.g., models within a feature)
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,
}
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.

The dashboard aggregates Twilio expenses from call, message, phone_number, and organization_twilio_settings tables.

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>,
}
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,
}

Organizations using their own Twilio credentials (is_byo: true in organization_twilio_settings) are tracked separately:

  • total_twilio_cost — all organizations, including BYO
  • loquent_twilio_cost — excludes BYO organizations
  • grand_total_cost — AI cost + loquent_twilio_cost (reflects actual platform spend)

BYO organizations display a badge in the Twilio org table.

Defined in src/mods/admin/services/twilio_pricing.rs:

ConstantValueDescription
TWILIO_VOICE_PER_MINUTE$0.014US programmable voice
TWILIO_PHONE_NUMBER_MONTHLY$1.15Local US number monthly fee
TWILIO_SMS_OUTBOUND_PER_SEGMENT$0.0079Outbound SMS per segment
TWILIO_SMS_INBOUND_PER_SEGMENT$0.0075Inbound SMS per segment

Phone number cost is prorated by the selected time range: count × $1.15 × (days_in_range / 30).

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:

ProviderModelInputOutputAudio InAudio Out
anydeepseek/deepseek-v3.2$0.25$0.40
anygoogle/gemini-3.1-pro-preview$2.00$12.00
anygoogle/gemini-3.1-flash-lite-preview$0.25$1.50
anyanthropic/claude-sonnet-4.6$3.00$15.00
openaigpt-realtime-mini$0.60$2.40$10.00$20.00
openaigpt-realtime / gpt-realtime-1.5$4.00$16.00$32.00$64.00
geminigemini-live-2.5-flash-native-audio$0.50$12.00
openaigpt-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_minute

The UI renders six sections:

  1. 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)
  2. Grouped cost chart — filterable stacked bar chart with modes: Total, By Model, By Feature, By Provider

  3. 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

  4. AI cost by organization table — per-org totals with API calls, input/output tokens, and provider splits

  5. Twilio expense cards (3 cards) — Voice (minutes, cost), Phone Numbers (count, monthly cost), SMS (inbound/outbound counts, cost)

  6. Twilio cost by organization table — per-org voice, calls, numbers, SMS, with BYO badge

  1. Add a match arm in get_model_pricing() at src/mods/ai/types/ai_pricing_type.rs
  2. Set the per-1M-token rates from the provider’s pricing page
  3. The cost dashboard picks up the new model automatically on next query
FilePurpose
src/mods/ai/types/ai_pricing_type.rsModelPricing, get_model_pricing(), calculate_cost()
src/mods/admin/services/admin_ai_cost_service.rsAggregation queries and breakdown computation
src/mods/admin/services/twilio_pricing.rsTwilio pricing constants
src/mods/admin/api/get_admin_ai_costs_api.rsGET /api/admin/ai-costs endpoint
src/mods/admin/types/admin_data_types.rsResponse types
src/mods/admin/components/admin_ai_costs_tab_component.rsDashboard UI components