Skip to content

Cost Analytics

Track spending, analyze usage patterns, and optimize your AI costs with GateFlow's analytics.

Dashboard Overview

The cost analytics dashboard shows:

  • Total Spend: Daily, weekly, monthly views
  • Cost by Model: Which models drive spend
  • Cost by Key: Spending per API key
  • Cache Savings: Money saved by caching
  • Trends: Usage patterns over time

Viewing Analytics

Summary

bash
curl https://api.gateflow.ai/v1/management/analytics/cost \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d"

Response:

json
{
  "period": {
    "start": "2024-12-16",
    "end": "2025-01-15"
  },
  "summary": {
    "total_cost": 2345.67,
    "total_requests": 1234567,
    "total_tokens": 567890123,
    "cache_savings": 456.78,
    "effective_cost_per_request": 0.0019
  }
}

By Model

bash
curl https://api.gateflow.ai/v1/management/analytics/cost/by-model \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d"

Response:

json
{
  "by_model": [
    {
      "model": "gpt-4o",
      "cost": 1234.56,
      "percentage": 52.6,
      "requests": 456789,
      "avg_cost_per_request": 0.0027
    },
    {
      "model": "gpt-4o-mini",
      "cost": 234.56,
      "percentage": 10.0,
      "requests": 567890,
      "avg_cost_per_request": 0.0004
    }
  ]
}

By API Key

bash
curl https://api.gateflow.ai/v1/management/analytics/cost/by-key \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d"

By Time

bash
curl https://api.gateflow.ai/v1/management/analytics/cost/timeseries \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d" -d "granularity=day"

Response:

json
{
  "timeseries": [
    {"date": "2025-01-01", "cost": 78.90, "requests": 45678},
    {"date": "2025-01-02", "cost": 82.34, "requests": 48901},
    ...
  ]
}

Cost Breakdown

Token Analysis

bash
curl https://api.gateflow.ai/v1/management/analytics/tokens \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=7d"

Response:

json
{
  "tokens": {
    "input": {
      "total": 123456789,
      "avg_per_request": 234,
      "cost": 123.45
    },
    "output": {
      "total": 45678901,
      "avg_per_request": 89,
      "cost": 456.78
    }
  },
  "ratios": {
    "input_output_ratio": 2.7,
    "output_cost_percentage": 78.7
  }
}

Provider Breakdown

bash
curl https://api.gateflow.ai/v1/management/analytics/cost/by-provider \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d"

Budgets & Limits

Set Budget

bash
curl -X POST https://api.gateflow.ai/v1/management/budgets \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Monthly production budget",
    "amount": 5000.00,
    "period": "monthly",
    "scope": {
      "workspaces": ["production"]
    },
    "alerts": [
      {"threshold_percentage": 50, "notify": ["email"]},
      {"threshold_percentage": 80, "notify": ["slack", "email"]},
      {"threshold_percentage": 95, "notify": ["pagerduty"]}
    ],
    "action_at_limit": "warn"  // or "block"
  }'

View Budget Status

bash
curl https://api.gateflow.ai/v1/management/budgets \
  -H "Authorization: Bearer gw_prod_admin_key"

Response:

json
{
  "budgets": [
    {
      "id": "budget_abc123",
      "name": "Monthly production budget",
      "amount": 5000.00,
      "spent": 2345.67,
      "remaining": 2654.33,
      "percentage_used": 46.9,
      "projected_end_of_period": 4890.00,
      "status": "on_track"
    }
  ]
}

Per-Key Limits

bash
curl -X PATCH https://api.gateflow.ai/v1/management/api-keys/key_123 \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "cost_limits": {
      "daily": 100.00,
      "monthly": 2000.00
    }
  }'

Alerts

Cost Alerts

bash
curl -X POST https://api.gateflow.ai/v1/management/alerts \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Unusual daily spend",
    "condition": {
      "metric": "cost_daily",
      "operator": "gt",
      "threshold": 200.00
    },
    "notify": {
      "channels": ["slack"],
      "message": "Daily spend exceeded $200"
    }
  }'

Anomaly Detection

bash
curl -X POST https://api.gateflow.ai/v1/management/alerts \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Spend anomaly",
    "condition": {
      "metric": "cost_hourly",
      "operator": "anomaly",
      "sensitivity": "medium"
    },
    "notify": {
      "channels": ["email"]
    }
  }'

Cost Reports

Generate Report

bash
curl -X POST https://api.gateflow.ai/v1/management/reports/cost \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "period": {
      "start": "2025-01-01",
      "end": "2025-01-31"
    },
    "format": "pdf",
    "sections": ["summary", "by_model", "by_key", "trends", "recommendations"]
  }'

Scheduled Reports

bash
curl -X POST https://api.gateflow.ai/v1/management/reports/schedule \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Weekly cost report",
    "schedule": "0 9 * * MON",
    "recipients": ["finance@company.com"],
    "format": "pdf"
  }'

Export Data

CSV Export

bash
curl https://api.gateflow.ai/v1/management/analytics/export \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "period=30d" -d "format=csv" \
  -o cost_data.csv

Webhook Integration

Send analytics to external systems:

bash
curl -X POST https://api.gateflow.ai/v1/management/webhooks \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Cost data to DataDog",
    "url": "https://api.datadoghq.com/api/v1/series",
    "events": ["cost.daily"],
    "format": "datadog"
  }'

Optimization Recommendations

Get Recommendations

bash
curl https://api.gateflow.ai/v1/management/analytics/recommendations \
  -H "Authorization: Bearer gw_prod_admin_key"

Response:

json
{
  "recommendations": [
    {
      "type": "model_switch",
      "title": "Switch simple queries to GPT-4o-mini",
      "description": "45% of your GPT-4o requests are simple queries that could use GPT-4o-mini",
      "potential_savings": 234.56,
      "impact": "low_risk",
      "action": {
        "type": "create_routing_rule",
        "config": {...}
      }
    },
    {
      "type": "enable_caching",
      "title": "Enable caching for FAQ queries",
      "description": "23% of queries are repeated verbatim",
      "potential_savings": 123.45,
      "impact": "low_risk"
    }
  ]
}

Apply Recommendation

bash
curl -X POST https://api.gateflow.ai/v1/management/analytics/recommendations/rec_123/apply \
  -H "Authorization: Bearer gw_prod_admin_key"

Cost Attribution

Tag Requests

python
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[...],
    extra_body={
        "gateflow": {
            "tags": {
                "team": "support",
                "feature": "chatbot",
                "environment": "production"
            }
        }
    }
)

View by Tag

bash
curl https://api.gateflow.ai/v1/management/analytics/cost/by-tag \
  -H "Authorization: Bearer gw_prod_admin_key" \
  -G -d "tag=team" -d "period=30d"

Response:

json
{
  "by_tag": {
    "tag": "team",
    "values": [
      {"value": "support", "cost": 1234.56, "requests": 456789},
      {"value": "sales", "cost": 567.89, "requests": 123456},
      {"value": "engineering", "cost": 345.67, "requests": 78901}
    ]
  }
}

Next Steps

Built with reliability in mind.