AI provider spend
Hard caps on OpenAI, Anthropic and Gemini spend through the AI proxy, or observe and stop with admin keys.
Model tokens are often an agent's biggest bill, and the easiest to run up by accident: a loop, a long context, a retry storm. ndStill controls AI provider spend in two ways. The AI proxy puts a hard cap in front of every call (control level Authorize). Admin keys let ndStill watch usage and cut off access when a budget is crossed (Observe & stop). Provider-side limits are kept in sync underneath either way.
Option 1: the AI proxy (hard caps)#
The proxy exposes endpoints compatible with the OpenAI, Anthropic and Gemini APIs, including streaming. Each agent gets its own ndStill key; ndStill maps it to your upstream provider key, which it stores encrypted and never hands to the agent. Point the agent's client at the proxy and change nothing else.
import OpenAI from "openai";
const openai = new OpenAI({
baseURL: "https://ai.ndstill.com/openai/v1",
apiKey: process.env.NDSTILL_AGENT_KEY, // nds_live_…, bound to this agent
defaultHeaders: { "Ndstill-Task": "weekly-report" }, // optional attribution
});
const r = await openai.chat.completions.create({ model: "gpt-4.1-mini", messages: [{ role: "user", content: "Summarise this week" }], max_tokens: 800 });The SDKs include helpers that build these clients for you (ndstill.ai.openai() and ndstill.ai.anthropic() in TypeScript); see the TypeScript SDK.
What happens on each call#
- Estimate and reserve. Before forwarding, the proxy estimates the most the call can cost: input tokens plus
max_tokens, times the price from a versioned price table. It reserves that amount in the agent's budgets (and the tree's, team's and org's). - Refuse if it can't fit. If the reservation doesn't fit, the call never reaches the provider. The client gets a structured error naming the budget, so a well-behaved agent can stop or ask for more.
- Stream through. Tokens are counted as they arrive. In hard-cut mode (the proxy's default), a stream that would cross its reservation is stopped there.
- Post the actual cost. From the response's usage field; the rest of the reservation is released. A usage event is written for Books.
{
"object": "error",
"type": "declined",
"code": "payment_declined",
"message": "Over Research orchestrator's daily budget by $3.10.",
"decline_reasons": [
{ "code": "over_budget", "stage": "budget", "effect": "decline", "params": { "scope_name": "Research orchestrator", "window": "day" } }
],
"request_id": "req_01JD…"
}Declines on the proxy use HTTP 402; when a provider-compatible client needs a familiar shape, the proxy can answer 429 instead, with the same body.
Attribution#
Every call is attributed to the agent by its key. Add Ndstill-Task, Ndstill-Customer or Ndstill-Project headers to split cost by task, end customer or project in spend reports and Books. This is how AI products get cost per customer.
Option 2: admin keys (observe and stop)#
Where routing through a proxy isn't practical, connect the provider's admin or usage API instead (OpenAI, Anthropic, Google, AWS Bedrock, Azure). ndStill:
- polls usage and cost per key or project and attributes it to agents
- applies the same budgets, alerting before a cap is reached (
budget.threshold_reached) - on breach, runs the kill switch for that agent: pauses the provider key where the provider allows it, deletes it where it doesn't
- writes provider-side spend limits to match your policy, as a backstop
Overspend is possible up to the provider's reporting delay, so the console shows these connections as Observe & stop, with their measured stop time. Create the connection with POST/connections.
AI gateways#
If you already use an AI gateway (LiteLLM, Portkey, Cloudflare AI Gateway, OpenRouter), ndStill can set its budgets and pull its usage in, so gateway spend lands in the same budgets as everything else.
A policy for AI spend#
version: "2026-11-01"
budgets:
- amount: { amount: 80000, asset: USD } # $800.00
window: month
- amount: { amount: 6000, asset: USD } # $60.00
window: day
rails:
allow: [ai]
risk:
velocity_per_10m: 120
conditions:
- name: Stop near the monthly cap
when: "budget.used_ratio > 0.95"
then: declineThe mandate stage doesn't apply on usage-based rails, so AI calls show it as not on this rail:
- Passed
- Held here
- Declined here
- Not on this rail
- Not reached
Read next: Budgets and reservations, and the policy cookbook for more AI recipes.