Skip to content

AI cost management: measure by key and project, alert and cap

Founder, AIUBY
Illustration: the AI spend curve crosses control layers up to the limit line

Cost Management + AI Gateway

On this page
  1. The problem: the bill arrives with no owner
  2. Measure every call, without storing the content
  3. Attribute: key, person, model and project
  4. Alert and cap
  5. Out-of-pattern spend
  6. Savings recommendations, with human approval
  7. Limitations

AI cost management starts with a simple question that almost nobody can answer at the end of the month: who spent it, on which model, and for what. The provider's bill shows a total per key, at best. This post shows how AIUBY Cost Management answers that question for usage that goes through the AI Gateway, and where the answer still has gaps. Everything here was checked against the platform code on November 27, 2026.

#The problem: the bill arrives with no owner

When each team creates its own key with the provider, cost becomes a single number with no attribution. You can't tell whether the increase came from a new product, a forgotten loop in a script, or a prompt that grew. Our approach is to route every call through a single point, the Gateway, and record the cost of each one there with the tags that matter to the business.

#Measure every call, without storing the content

Each model call becomes a usage row with the workspace, the calling key, the provider, the model, input and output tokens, latency, and cost in micro-dollars. No column stores the prompt or response text: the meter knows how much it cost, not what was said.

  • Cost is calculated from the model's price table, with upward rounding. The total never underestimates.
  • For models with a long-context tier, a call that exceeds the tier threshold is billed entirely at the tier price, as the providers do.
  • When the provider doesn't report tokens, the platform estimates 1 token per 4 characters and flags the row as estimated, so nobody mistakes an estimate for a measurement.
  • For accounts billed in BRL, the BRL amount is written by the database when the call closes, at the rate in effect at that moment, and never changes afterward. How we arrive at that rate is in Products and prepaid account.

#Attribute: key, person, model and project

The usage dashboard groups cost by workspace, person, key, model, and project. The project is a tag your application sends in the x-aiuby-project header: lowercase letters, digits, period, hyphen, and underscore, up to 63 characters. A tag that doesn't match the format gets 400 invalid_request, instead of being stored malformed and vanishing from reports.

For the question "how much did the payments team spend", which isn't always a workspace, there are saved perspectives: a person picks a dimension and combines values into named groups, by exact value or by prefix with *. Each person can have up to 30 perspectives. Sharing a perspective never shows the recipient a cost they couldn't already see.

The same slice can be exported to CSV from the UI, with up to 50,000 rows per export.

#Alert and cap

Knowing the cost after the fact isn't enough: spending has to stop before it becomes a surprise. That's why the Gateway decides before calling the provider, and the rule is to deny when configuration is missing. The full logic is in AI API spend limits: blocked by default.

ControlWhat it doesResponse when hit
Prepaid balanceWith no balance, no call that uses AI goes out402 insufficient_credit
Workspace budgetRequired; with no budget set, requests are refused403 budget_not_configured
Rule per person, key, model or projectBlocks or only warns, as the creator chooses429 budget_exceeded
Organization capSums all workspaces429 org_budget_exceeded
Workspace rate60 requests per minute and 20 concurrent, by default429 rate_limited

Dimension rules and the workspace budget alert at 50, 80, and 100% of the amount, once per threshold per month. The alert is recorded in the audit trail and emailed to the organization owners. If cost jumps several thresholds at once, only one email goes out, for the highest threshold.

#Out-of-pattern spend

A budget catches spend that passes a fixed amount. It doesn't catch the day cost tripled but still fits within the budget. For that there is anomaly detection, using simple, explainable statistics: the median and median absolute deviation of the last 14 days (or 8 weeks), with at least 10 days (or 4 weeks) of usage. A day is flagged when it exceeds three thresholds at once:

  • the median plus 3.5 times the robust deviation;
  • 1.5 times the median;
  • the median plus US$1.

The flagged day appears in the usage dashboard with the top five contributors by model, key, workspace, and project, which is where the investigation starts.

#Savings recommendations, with human approval

The platform looks at the last 30 days of successful calls, by key, project, or agent, and searches for two patterns. No AI decides, and nothing is switched automatically.

RecommendationWhen it appears
Model larger than neededAt least 50 calls, 95% of responses with up to 300 tokens and of inputs with up to 2,000 tokens, and a model enabled in the workspace at least 30% cheaper for the same load.
Bloated promptThe 95th percentile input is at least 3 times the median and at least 1,000 tokens: some calls carry context the others don't need.

Only findings with estimated savings of at least 10 US cents in the window become recommendations. A model switch is flagged as dependent on evaluation: smaller and cheaper only works if quality holds, and the team makes that call. Once the owner accepts, the platform compares cost per call before and after, with at least 50 calls before and 20 after. Realized savings can come out negative, and are shown that way.

Limit

Task complexity is inferred from token size alone. A short response may require a large model, which is why a switch recommendation is never applied without a person.

#Limitations

  • Only usage that goes through the AI Gateway is metered per call. A key used directly with the provider is outside this count.
  • Prompt caching isn't metered: cache reads and writes are added into input tokens. Cache savings don't show up in the recommendations.
  • The rate limit is per workspace, not per key. To throttle a specific key, use a per-key budget rule.
  • Reading cost, budgets, or recommendations with an API key isn't available yet: reading is through the UI, with a session.
  • An alert email isn't resent if delivery fails; the record stays in the audit trail.
  • Organization-wide budget and anomaly alerts depend on a per-environment activation; workspace and dimension rules don't.

To estimate usage before you start, use the usage calculator. Cloud costs (AWS, GCP, OpenAI, and Anthropic through their cost APIs) are part of the same product and are described in Cost Management, in the docs.

Biweekly digest

New posts and what reached the changelog, every two weeks. No other emails.