Skip to main content
Retrieve a daily breakdown of your API usage from the /v1/usage/report endpoint. The report includes token consumption, request counts, and costs for each day in the requested period.
The endpoint supports predefined periods (day, week, month) and custom date ranges via start_date and end_date query parameters. See the API Reference for the full schema.

Quick Start

import os
import json
import requests
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("SWARMS_API_KEY")
BASE_URL = "https://api.swarms.world"

headers = {
    "x-api-key": API_KEY,
    "Content-Type": "application/json",
}

def get_usage_report(period: str = "month") -> dict | None:
    """Fetch a daily usage report for the given period."""
    resp = requests.get(
        f"{BASE_URL}/v1/usage/report",
        headers=headers,
        params={"period": period},
    )

    if resp.status_code == 200:
        return resp.json()

    print(f"Error: {resp.status_code} - {resp.text}")
    return None

if __name__ == "__main__":
    data = get_usage_report("week")
    if data:
        print(f"Period: {data['period']}")
        print(f"Range: {data['start_date']} to {data['end_date']}")
        print(f"Days with activity: {len(data['results'])}\n")

        for day in data["results"]:
            print(
                f"  {day['day']}: "
                f"cost=${day['total_cost']:.4f}, "
                f"in={day['input_tokens']:,}, "
                f"out={day['output_tokens']:,}, "
                f"reqs={day['request_count']}"
            )

Custom Date Range

Pass start_date and end_date as query parameters to query a specific window. These override the period parameter.
def get_usage_for_march():
    """Fetch usage for a specific month."""
    resp = requests.get(
        f"{BASE_URL}/v1/usage/report",
        headers=headers,
        params={
            "start_date": "2026-03-01",
            "end_date": "2026-03-31",
        },
    )

    data = resp.json()
    total_cost = sum(d["total_cost"] for d in data["results"])
    total_reqs = sum(d["request_count"] for d in data["results"])

    print(f"March 2026: ${total_cost:.2f} across {total_reqs:,} requests")

Monitor Usage Over Time

Build a simple cost tracker that alerts on high-usage days:
def check_daily_spend(threshold: float = 5.0):
    """Alert if any day in the past week exceeded a cost threshold."""
    data = get_usage_report("week")
    if not data:
        return

    for day in data["results"]:
        if day["total_cost"] > threshold:
            print(
                f"High spend on {day['day']}: "
                f"${day['total_cost']:.2f} "
                f"({day['request_count']} requests)"
            )

check_daily_spend(threshold=2.0)

Response Schema

UsageReportOutput

FieldTypeDescription
resultslistDaily usage entries, sorted ascending by date
periodstringThe period applied: day, week, month, or custom
start_datestringReport start date (YYYY-MM-DD)
end_datestringReport end date (YYYY-MM-DD)
timestampstringISO timestamp when the report was generated

UsageReportDay

FieldTypeDescription
daystringDate (YYYY-MM-DD)
total_costnumberTotal cost in USD
input_tokensintegerTotal input tokens consumed
output_tokensintegerTotal output tokens consumed
request_countintegerTotal API requests