Analytics API

The Analytics API provides aggregated views of your cost data, optimized for reporting and dashboards.

Base URL

https://api.infraprism.com/v1/analytics

Authentication

Include your API key in the Authorization header:

curl https://api.infraprism.com/v1/analytics/costs \
  -H "Authorization: Bearer ip-your-api-key"

Endpoints

Cost Summary

Get aggregated costs over a time period.

GET /v1/analytics/costs

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date (YYYY-MM-DD)
end_datestringYesEnd date (YYYY-MM-DD)
entity_typestringNoFilter by entity type
entity_idstringNoFilter by entity ID
modelstringNoFilter by model
group_bystringNoGroup results (see below)
tagsstringNoFilter by tags (key:value,key:value)

Group By Options:

  • entity - Group by entity_type and entity_id
  • model - Group by model
  • day - Group by day
  • week - Group by week
  • month - Group by month
  • tags.{key} - Group by a specific tag

Example Request:

curl "https://api.infraprism.com/v1/analytics/costs?start_date=2025-01-01&end_date=2025-01-31&entity_type=customer&group_by=entity" \
  -H "Authorization: Bearer ip-..."

Response:

{
  "data": [
    {
      "entity_type": "customer",
      "entity_id": "acme-corp",
      "total_cost": 523.45,
      "total_events": 15234,
      "total_input_tokens": 2500000,
      "total_output_tokens": 8500000,
      "avg_latency_ms": 1150
    },
    {
      "entity_type": "customer",
      "entity_id": "globex",
      "total_cost": 312.20,
      "total_events": 8921,
      "total_input_tokens": 1200000,
      "total_output_tokens": 5100000,
      "avg_latency_ms": 980
    }
  ],
  "summary": {
    "total_cost": 835.65,
    "total_events": 24155,
    "period": {
      "start": "2025-01-01",
      "end": "2025-01-31"
    }
  }
}

Cost Time Series

Get costs over time for charting.

GET /v1/analytics/costs/timeseries

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date
end_datestringYesEnd date
intervalstringNoInterval: hour, day, week, month (default: day)
entity_typestringNoFilter by entity type
entity_idstringNoFilter by entity ID

Example Request:

curl "https://api.infraprism.com/v1/analytics/costs/timeseries?start_date=2025-01-01&end_date=2025-01-31&interval=day" \
  -H "Authorization: Bearer ip-..."

Response:

{
  "data": [
    {
      "timestamp": "2025-01-01T00:00:00Z",
      "cost": 12.34,
      "events": 523
    },
    {
      "timestamp": "2025-01-02T00:00:00Z",
      "cost": 15.67,
      "events": 612
    }
  ],
  "interval": "day"
}

Model Usage

Get usage breakdown by model.

GET /v1/analytics/models

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date
end_datestringYesEnd date
entity_typestringNoFilter by entity type
entity_idstringNoFilter by entity ID

Response:

{
  "data": [
    {
      "model": "gpt-4o",
      "provider": "openai",
      "total_cost": 450.00,
      "total_events": 10000,
      "total_input_tokens": 2000000,
      "total_output_tokens": 6000000,
      "avg_latency_ms": 1200,
      "percentage_of_total": 54.2
    },
    {
      "model": "gpt-4o-mini",
      "provider": "openai",
      "total_cost": 85.00,
      "total_events": 25000,
      "total_input_tokens": 5000000,
      "total_output_tokens": 12000000,
      "avg_latency_ms": 450,
      "percentage_of_total": 10.2
    }
  ]
}

Entity Leaderboard

Get top entities by cost.

GET /v1/analytics/entities/top

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date
end_datestringYesEnd date
entity_typestringNoFilter by entity type
limitintegerNoNumber of results (default: 10, max: 100)
orderstringNodesc (default) or asc

Response:

{
  "data": [
    {
      "entity_type": "customer",
      "entity_id": "acme-corp",
      "total_cost": 523.45,
      "total_events": 15234,
      "rank": 1
    },
    {
      "entity_type": "customer",
      "entity_id": "globex",
      "total_cost": 312.20,
      "total_events": 8921,
      "rank": 2
    }
  ]
}

Sessions

Get session-level analytics.

GET /v1/analytics/sessions

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date
end_datestringYesEnd date
entity_typestringNoFilter by entity type
entity_idstringNoFilter by entity ID
limitintegerNoNumber of results

Response:

{
  "data": [
    {
      "session_id": "550e8400-e29b-41d4-a716-446655440000",
      "entity_type": "customer",
      "entity_id": "acme-corp",
      "started_at": "2025-01-15T10:00:00Z",
      "ended_at": "2025-01-15T10:15:00Z",
      "turn_count": 5,
      "total_cost": 0.0234,
      "total_tokens": 3500
    }
  ],
  "summary": {
    "total_sessions": 1523,
    "avg_turns_per_session": 4.2,
    "avg_cost_per_session": 0.0185
  }
}

Anomalies

Get detected anomalies.

GET /v1/analytics/anomalies

Query Parameters:

ParameterTypeRequiredDescription
start_datestringYesStart date
end_datestringYesEnd date
entity_typestringNoFilter by entity type
statusstringNoactive, resolved, dismissed

Response:

{
  "data": [
    {
      "id": "anomaly_123",
      "detected_at": "2025-01-15T10:30:00Z",
      "entity_type": "customer",
      "entity_id": "acme-corp",
      "type": "cost_spike",
      "severity": "high",
      "description": "Cost increased 340% compared to 7-day average",
      "expected_value": 12.50,
      "actual_value": 55.00,
      "status": "active"
    }
  ]
}

Compare Periods

Compare two time periods.

GET /v1/analytics/compare

Query Parameters:

ParameterTypeRequiredDescription
period1_startstringYesFirst period start
period1_endstringYesFirst period end
period2_startstringYesSecond period start
period2_endstringYesSecond period end
entity_typestringNoFilter by entity type
entity_idstringNoFilter by entity ID

Response:

{
  "period1": {
    "start": "2025-01-01",
    "end": "2025-01-15",
    "total_cost": 450.00,
    "total_events": 12000
  },
  "period2": {
    "start": "2024-12-16",
    "end": "2024-12-31",
    "total_cost": 380.00,
    "total_events": 10500
  },
  "changes": {
    "cost_change": 70.00,
    "cost_change_percent": 18.4,
    "events_change": 1500,
    "events_change_percent": 14.3
  }
}

Filtering by Tags

Use the tags parameter to filter by custom tags:

# Single tag
curl "...?tags=feature:chatbot"

# Multiple tags (AND)
curl "...?tags=feature:chatbot,env:production"

Response Formats

CSV Export

Add format=csv to get CSV output:

curl "https://api.infraprism.com/v1/analytics/costs?...&format=csv" \
  -H "Authorization: Bearer ip-..." \
  -o costs.csv

Python Example

import requests
from datetime import datetime, timedelta

API_KEY = "ip-your-api-key"
BASE_URL = "https://api.infraprism.com/v1"

def get_customer_costs(start_date: str, end_date: str):
    response = requests.get(
        f"{BASE_URL}/analytics/costs",
        headers={"Authorization": f"Bearer {API_KEY}"},
        params={
            "start_date": start_date,
            "end_date": end_date,
            "entity_type": "customer",
            "group_by": "entity",
        },
    )
    response.raise_for_status()
    return response.json()

# Get last 30 days of customer costs
end = datetime.now()
start = end - timedelta(days=30)
data = get_customer_costs(
    start.strftime("%Y-%m-%d"),
    end.strftime("%Y-%m-%d"),
)

for customer in data["data"]:
    print(f"{customer['entity_id']}: ${customer['total_cost']:.2f}")

Next Steps