Consumption guardrails
@agentcosts/consumption-guardrails
Set and check consumption guardrails for an AI product: per-customer daily and monthly spend caps, month-end forecasts, spend spike rules, runaway agent runs, and failure rate spikes, plus a runbook for what to do when each alert fires. Runs on usage files; exit code 2 on critical alerts for cron or CI.
- Activity
- New: be the first to try it
- Installs
- 0 installs
- Likes
- 0 likes
- Clones
- 0 clones
What it makes
Real output from this kit
- Sample file3 KB, download to openGuardrailsFrom the kit's tests
- Sample file2 KB, download to openGuardrailsFrom the kit's tests
- Sample file554 bytes, download to openGuardrailsFrom the kit's tests
How this works
- It is a skill: written instructions Claude reads when your request matches what the skill is for. You do not call it by name; just ask for the task.
- While it works, it reads and changes files in the places it lists, nowhere else.
- It runs these commands on your computer: node scripts/guardrails.mjs.
- Nothing runs until you choose to use it, and you can remove it any time.
SKILL.md
Consumption guardrails
Agent products have a failure mode normal SaaS does not: one bad loop or one enthusiastic customer can burn a month of margin in an afternoon. Guardrails are simple numbers, checked often, with a plan for each alert. This kit sets the numbers from your own usage, checks them, and writes the runbook.
Inputs
- Usage in the shared schema (
timestamp, customer, task, run_id, model, tokens..., cost_usd, outcome), as fresh as possible. Usellm-usage-normalizerfor provider exports. Config
data/guardrails.json(start fromexamples/guardrails.json):{ "warn_at": 0.8, "defaults": { "daily_cap_usd": 6, "monthly_cap_usd": 100, "run_cost_cap_usd": 3, "max_calls_per_run": 25 }, "customers": { "cobalt-labs": { "monthly_cap_usd": 60, "daily_cap_usd": 4 } }, "anomaly": { "multiplier": 3, "baseline_days": 7, "min_usd": 1 }, "failure_rate": { "max": 0.4, "min_runs": 5 } }
Steps
Set limits from data, not guesses. Look at 30 days of usage: each customer's normal and heaviest day, the p95 cost and call count per run for each task, and what each customer pays. Propose:
- Monthly cap: the spend at which that customer's gross margin hits the floor the user accepts (revenue x (1 - target margin)), or their plan's included usage.
- Daily cap: about 3x a normal heavy day.
- Cost per run: 3x the p95 of the most expensive task. Calls per run: 2x the p95 calls.
Show the proposal as a table and write
data/guardrails.jsononce the user agrees.
Check:
node scripts/guardrails.mjs data/usage.csv --config data/guardrails.json --prices data/llm-prices.csv --out out/guardrails--as-of 2026-10-27checks a past day (useful to test the rules on a known incident).--fail-on-alertexits with code 2 on any critical alert, for a scheduled job or CI step.- Report from
out/guardrails.md: critical alerts first, with the customer, the rule, and the numbers. For runaway runs, list run IDs so engineering can trace them. - Write the runbook
out/guardrails-runbook.mdfromtemplates/runbook.md: fill in the limits table with the agreed numbers and why, who owns each alert, and the action for each. Ask the user what the product should do at a cap (notify only, throttle, queue, or pause non-urgent tasks) and who can raise a cap. - Make it routine. Suggest running the check on a schedule after usage is exported (daily is a good start; hourly for high-volume products) and sending the markdown to the team channel. Do not set up schedules or notifications yourself; give the user the command.
- Tune. After a week, look at alerts that fired: if one rule fires daily with no action, its threshold is wrong. Change the config, not the code.
Rules the script applies
| Rule | Fires when | Severity |
|---|---|---|
| Daily cap | spend on the as-of day > daily_cap_usd | warning, critical over 2x |
| Monthly cap | month to date > monthly_cap_usd | critical |
| Monthly forecast | month to date pace > cap | warning |
| Near cap | month to date > warn_at x cap | info |
| Spend spike | day > multiplier x median of the prior baseline_days and at least min_usd (customers with less history than baseline_days are skipped) | warning, critical over 2x the multiplier |
| Runaway runs | a run over run_cost_cap_usd or max_calls_per_run | warning, critical at 3+ runs |
| Failure rate | failed share > max with at least min_runs runs that day | warning |
Guardrails for this kit
- The script only reports. It never throttles, blocks, or changes anything in the product or the provider account.
- Do not lower a paying customer's limits or recommend cutting them off without the account owner in the loop.
- Costs depend on the price table. Make sure prices were checked against each provider's current pricing.
README.md
Consumption guardrails
Sets spend caps, forecasts, spike rules, and runaway-run limits for each customer of your AI product, checks your usage against them, and writes a runbook for what to do when each alert fires. Exit code 2 on critical alerts, so it drops into a cron job or CI step.
Who it is for: founders and engineers running agent products who have been surprised by a bill, or want to make sure they never are.
Example
Input: examples/usage.csv (October usage, including a day when one customer's research agent looped), examples/guardrails.json.
node scripts/guardrails.mjs examples/usage.csv --config examples/guardrails.json --prices examples/prices.csv --as-of 2026-10-27Output (out/guardrails.md):
| Severity | Customer | Rule | Detail |
|---|---|---|---|
| CRITICAL | cobalt-labs | daily cap | Spent $23.39 on 2026-10-27, cap $4.00. |
| CRITICAL | cobalt-labs | spend spike | 13.6x the 7-day median of $1.71. |
| CRITICAL | cobalt-labs | runaway runs | 6 research_agent runs over $3.00 per run or 25 calls per run (up to 52 calls). |
| WARNING | cobalt-labs | monthly forecast | On pace for $66.24 this month, over the $60.00 cap. |
| WARNING | cobalt-labs | failure rate | 6 of 6 research_agent runs failed. |
Plus out/guardrails-runbook.md, written with you from templates/runbook.md.
What it can access
- Runs
node scripts/guardrails.mjs(Node built-ins only). - Reads
data/. Writesdata/guardrails.json(your limits) andout/guardrails*. - No network. It reports; it never blocks or throttles anything.
Setup
None to try it. To run it daily, schedule the command after your usage export. Check the example LLM prices against each provider's current pricing first.
Files
- README.md
- SKILL.md
- examples/guardrails.json
- examples/prices.csv
- examples/usage.csv
- kit.json
- scripts/guardrails.mjs
- scripts/lib.mjs
- scripts/usage.mjs
- templates/runbook.md
Did it work?
0worked for them0did not work
Sign in to say whether it worked for you.
Nobody has said yet. Tried it? Tell the next person.
