Skip to main content

Spend logs and exports

The spend log is the gateway's record of every request: who made it, which model answered, how many tokens it used and what it cost. Daily activity rollups total those rows per day and per user, team, tag, organization, customer and agent, which is what the Usage page draws. This page shows how to get both out of the gateway, as rows your scripts can read, as a CSV or JSON file for a spreadsheet or a data warehouse, and how to reset the running spend counters that budgets are checked against without losing the log itself. Everything here is free on every gateway and needs a database.

A real-world scenario​

A data team loads the company's AI usage into its warehouse every night. A job calls the daily activity export for the previous day per team and per model and lands the CSV next to the cloud billing data. A separate support job pulls the raw rows of one customer when the customer disputes a charge. At the end of the quarter the platform admin resets the running spend counters so the new quarter's budgets start from zero, and the spend log keeps every row for the auditors.

Download daily activity as a file​

  1. In the Admin UI open Usage, set the date range, and click Export Data. In Export Usage Data pick an Export type, Day-by-day breakdown by user, Day-by-day breakdown by user and key or Day-by-day by user and model, pick Format (CSV, or JSON (includes metadata)) and click Export CSV. In the Team Usage, Tag Usage, Organization Usage, Customer Usage and Agent Usage views the same button exports per entity and adds Day-by-day breakdown by <entity> and user.

    Expected result: a file named like team-usage-2026-10-01-2026-10-07-daily_with_models.csv downloads, and a toast confirms the export.

  2. The same download from a script, for every team over a week, broken down by model:

    curl "http://localhost:4000/team/daily/activity/export?start_date=2026-10-01&end_date=2026-10-07&export_type=daily_with_models&format=csv" \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -o team-usage.csv

    Expected result: a CSV with one row per day, team and model.

    date,entity_id,entity_alias,api_key,key_alias,user_id,user_email,model,spend,flat_cost,prompt_tokens,completion_tokens,api_requests,successful_requests,failed_requests,cache_read_input_tokens,cache_creation_input_tokens
    2026-10-01,team-nlp,NLP team,,,,,claude-sonnet-5-5,41.2,0.0,2100000,410000,1840,1835,5,300000,0

    export_type is daily, daily_with_keys, daily_with_models or daily_with_users, and format is csv (the default) or json. The route exists for /user, /team, /tag, /organization, /customer and /agent, and each one takes the filters of its Usage view, such as team_ids or tags. Proxy admins and admin viewers can export everything, other roles only what they can see on the Usage page. Every download writes an exported audit row on Spanway_DailySpend with who downloaded it, the entity, the range, the breakdown and the format, so you can tell who took a copy of the spend data.

Read the raw rows​

  1. Page through the spend log with filters. start_date and end_date are required, as YYYY-MM-DD or YYYY-MM-DD HH:MM:SS in UTC:

    curl "http://localhost:4000/spend/logs/v2?start_date=2026-10-14&end_date=2026-10-14&end_user=customer-42&page_size=50" \
    -H "Authorization: Bearer $SPANWAY_API_KEY"

    Expected result: a page of rows with the paging counts.

    {
    "data": [
    {
    "request_id": "chatcmpl-6dcb2540-d3d7-4e49-bb27-291f863f112e",
    "call_type": "acompletion",
    "api_key": "88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",
    "spend": 0.0042,
    "total_tokens": 1400,
    "prompt_tokens": 1200,
    "completion_tokens": 200,
    "startTime": "2026-10-14T09:12:03.120000Z",
    "endTime": "2026-10-14T09:12:04.890000Z",
    "model": "claude-sonnet-5-5",
    "custom_llm_provider": "anthropic",
    "user": "customer-42",
    "team_id": "team-nlp",
    "end_user": "customer-42",
    "request_tags": ["support-bot"],
    "cache_hit": "False",
    "status": "success"
    }
    ],
    "total": 1840,
    "page": 1,
    "page_size": 50,
    "total_pages": 37
    }

    Filters include api_key (the sha256 hash), user_id, team_id, end_user, model, model_group, key_alias, status_filter, cache_hit_filter, min_spend, max_spend, error_code and sort_by. page_size goes up to 1000 and total is capped at 10000 rows, so narrow the range for busy gateways. Internal users only ever see their own rows. Request and response bodies are present only when general_settings.store_prompts_in_spend_logs is on. The Admin UI shows the same rows on the Logs page.

  2. For one day at a time without paging, the older GET /spend/logs returns up to 10000 rows, or one total per day with the default summarize=true:

    curl "http://localhost:4000/spend/logs?start_date=2026-10-01&end_date=2026-10-07" \
    -H "Authorization: Bearer $SPANWAY_API_KEY"

    Expected result: one entry per day with spend, prompt_tokens, completion_tokens and the count of requests. Add &summarize=false for the rows themselves, or ?request_id=<id> for a single request.

  3. Total spend by tag with GET /spend/tags?start_date=...&end_date=..., which returns individual_request_tag, log_count and total_spend per tag, or per tag and day with GET /global/spend/tags. These are the queries behind the Tag Usage view.

Reset the running spend counters​

Every key and team carries a running spend that max_budget is checked against. To start a new budget period for all of them at once, call this with the master key. A proxy admin's own key is refused with a 401, because a reset clears every budget counter on the gateway:

curl -X POST http://localhost:4000/global/spend/reset -H "Authorization: Bearer $SPANWAY_MASTER_KEY"

Expected result: {"message": "Spend for all API Keys and Teams reset successfully", "status": "success"}. The spend log and the daily activity rollups keep every row, so reports and exports are unchanged. Only the counters on the keys and teams go back to 0, and two audit rows record the reset, one for keys and one for teams, each with the total spend and the number of rows before the reset.

Routes​

RouteWhoWhat
GET /{user,team,tag,organization,customer,agent}/daily/activity/exportrole dependentThe daily activity rollups as CSV or JSON.
GET /{user,team,tag,organization,customer,agent}/daily/activity/aggregatedrole dependentThe same rollups as JSON with a breakdown per model, provider, endpoint and key.
GET /spend/logs/v2any key, scoped by roleSpend log rows, paged and filtered.
GET /spend/logsany key, scoped by roleSpend log rows or daily totals, unpaginated, up to 10000 rows.
GET /spend/tags, GET /global/spend/tagsany keySpend per tag, and per tag and day.
GET /global/spend/reportproxy admin, admin viewer, get_spend_routesGateway-wide spend per day grouped by team, customer or key, see spend reports.
POST /global/spend/resetmaster key onlySets every key and team spend to 0.

Errors​

StatusRouteMeaningFix
400 Start date and end date are required./spend/logs/v2The range is missing.Send both dates.
400 Invalid sort_by or Invalid cache_hit_filter./spend/logs/v2A filter value is not one the route accepts.Use the values named in the message.
500 DB not connected. This endpoint needs a database./spend/logs/v2, /spend/logsThe gateway has no database.Set DATABASE_URL.
403 A key with no user can read spend logs only as a proxy admin./spend/logsA key that belongs to no user and is not a proxy admin asked for rows.Use a key that belongs to a user, or a proxy admin key.
400 Invalid date format: <date>. Expected: 'YYYY-MM-DD'./spend/logsA date is not in YYYY-MM-DD form.Send YYYY-MM-DD.
403 Not authorized to view team spend for team_id=<id> or Not authorized to view spend log for request_id=<id>./spend/logs/v2A caller who is not a proxy admin asked for a team or a request they may not read.Ask a team admin for the /spend/logs permission, or use your own requests.
500 DB not connected. This endpoint needs a database./global/spend/resetThe gateway has no database.Set DATABASE_URL.
400 wrapping Database not connected./spend/tagsThe gateway has no database.Set DATABASE_URL.
400 Please provide start_date and end_date./global/spend/tagsThe range is missing.Send both dates.
401 Authentication Error, Tried to access route=/global/spend/reset, which is only for MASTER KEY./global/spend/resetThe key is not the master key, even if it belongs to a proxy admin.Use the master key.
500 Failed to fetch analytics. The gateway log has the cause.export routesReading the rollups failed. The gateway logs Daily activity export failed: <reason>.Check the database, then retry.
403 naming the scopeexport routesThe caller asked for an entity outside what their role can see.Ask an admin, or export your own scope.
422export routesexport_type or format is not one of the allowed values, or a date is malformed.Use daily, daily_with_keys, daily_with_models or daily_with_users, csv or json, and YYYY-MM-DD.
An empty file or [].export routesNo activity in the range for the scope.Widen the range or check the entity filter.
The Admin UI shows Failed to export data.Export Data dialogThe export route failed, usually for one of the reasons above.Retry, then check the gateway log.

Logs​

LevelMessageMeaningAction
ERRORDaily activity export failed: <reason>An export could not read the rollups and the caller got a 500.Check the database.
INFOTwo audit rows per POST /global/spend/resetEvery reset is recorded with the spend it cleared.Normal.
  • Cost tracking for what each row contains and how its cost is calculated.
  • Spend reports for totals per key, user, team or organization with a license.
  • Cost exports for sending the same rollups to a cost management tool on a schedule.
  • Usage page for the Export Data dialog and every view it exports from.
  • Logs page for browsing the spend log in the Admin UI.
  • What Spanway stores for the retention of spend log rows.