Skip to main content

Spend capture rate

The spend capture rate is the share of a provider's bill that went through the gateway and was priced. If OpenAI billed you 1,000 dollars for a day and the gateway recorded 969 dollars of OpenAI spend for that day, the capture rate is 0.969. A rate of 1.0 means every dollar was seen and priced. A lower rate means some applications call the provider directly with a raw provider key, or some requests were not costed, usually because a model has no price. A rate above 1.0 means the gateway prices above the bill, which points at outdated or custom prices that are too high. The check is free on every gateway, needs a database, and today supports OpenAI.

A real-world scenario​

A platform team has moved every application onto the gateway and told developers to stop using raw OpenAI keys. Two months later finance notices that the OpenAI invoice is still higher than the gateway's spend reports. The team runs the capture rate for the last week and sees 0.82. They open the daily breakdown, see that the gap is the same every day, and find a batch job in another department that still calls OpenAI directly. They rotate that provider key, and the next week's rate is 0.99. The remaining percent turns out to be a fine-tuned model without a price, which they fix with custom pricing.

Check the rate​

  1. Give the gateway an OpenAI admin API key with access to the organization's costs, as the environment variable OPENAI_ADMIN_KEY, and restart it.

  2. In the Admin UI open Usage, then the Spend reports tab. Proxy admins and admin viewers see a Spend capture rate card under the report form. Set From and To (at most 90 days) and click Compare with the OpenAI bill.

    Expected result: a sentence saying what share of the OpenAI bill Spanway recorded, followed by the recorded and billed amounts. When the share is below the alert threshold the card adds that some traffic is likely missing. When OpenAI billed nothing in the range it says there is no rate to compare.

  3. Run the same check from a script, which also gives you the per-day numbers:

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

    Expected result:

    {
    "provider": "openai",
    "start_date": "2026-10-01",
    "end_date": "2026-10-07",
    "captured_spend": 1843.12,
    "provider_spend": 1902.55,
    "capture_rate": 0.969,
    "threshold": 0.9,
    "below_threshold": false,
    "days": [
    {"date": "2026-10-01", "captured_spend": 251.4, "provider_spend": 260.03, "capture_rate": 0.967}
    ]
    }

    end_date is inclusive and days are UTC. threshold (default 0.9) sets the ratio under which below_threshold is true. project_ids scopes the OpenAI bill to specific projects. Captured spend is never scoped, so list every project the gateway's OpenAI keys belong to. capture_rate is null on days the provider billed nothing.

Daily check and alert​

To be told when the rate drops instead of checking by hand, turn on the scheduled check in the config:

general_settings:
spend_capture_rate_check:
providers: ["openai"]
threshold: 0.9
lookback_days: 7
openai_project_ids: ["proj_abc123"]

The check runs every day at 01:15 UTC, and once two minutes after the gateway starts. It compares the last lookback_days days, publishes the rate as a metric, and sends a failed_tracking_spend alert through your alerting channels when the rate drops below threshold. It also alerts when OPENAI_ADMIN_KEY is not set or the provider bill cannot be read, so a broken check does not stay silent. With several instances and Redis configured, only one instance sends each alert. Without Redis every instance sends its own. threshold must be between 0 and 1, lookback_days at most 180, and the defaults are ["openai"], 0.9 and 7 days.

Investigating a low rate​

Look for applications that call the provider directly with a raw provider key, and rotate that key so all traffic has to go through the gateway. Then open Logs and filter on a cost of 0, or watch the Prometheus counter spanway_zero_cost_requests_total{reason="model_not_priced"}, to find requests the gateway could not price, and add a price with custom pricing. A rate above 1.0 means a custom price is higher than what the provider charges you, or the bundled price map is behind, so compare the deployment's model_info with the provider's price list.

Errors​

StatusMeaningFix
403 Only proxy admins and admin viewers can read the capture rate.The key is not a proxy admin or admin viewer.Use an admin key. The Admin UI hides the card from other roles.
503 OPENAI_ADMIN_KEY is not set on the proxy, so the openai bill cannot be read.The gateway has no admin credential for the provider.Set OPENAI_ADMIN_KEY and restart.
502 Could not read the openai bill: ....The provider's billing API refused or failed, for example because the key lacks the costs permission.Check the key's permissions in the provider console and try again.
400 end_date must not be before start_date or Date range too large; maximum is 180 days.The range is invalid or longer than the API allows. The Admin UI card is stricter and compares at most 90 days.Fix the dates. The Admin UI disables the button for such a range.
500 DB not connected. This endpoint needs a database.The gateway has no database, so there is no captured spend to compare.Set DATABASE_URL.
capture_rate is null for the whole range.The provider billed nothing in the range.Widen the range, or check that the admin key belongs to the organization that is being billed.

Logs​

LevelMessageMeaningAction
INFOSpend capture-rate check: <result>The daily check ran and this is what it found.Normal. Act on the alert when the rate is low.
INFOSpend capture-rate check: another pod alerted this windowAnother instance already sent this alert.Normal.
WARNINGSpend capture-rate check: could not read the lock: <reason>Redis could not be read, so this instance alerted on its own.Check Redis.

The alert itself reads, for example, Spend capture-rate check: OPENAI_ADMIN_KEY is not set, so the openai bill cannot be read. or Spend capture-rate check: could not read the openai bill (<reason>).