Doc / CSV / Python script (ZIP + PDF)
spend_kill_switch.py reads per-key usage from the OpenRouter, OpenAI and Anthropic admin APIs (or from a CSV export for Bedrock and others), checks each key against your daily cap, a spike rule and its expected models, alerts to a webhook and, only when you pass --execute, disables or deletes the key. Dry run by default. Usage APIs lag, so keep the provider-side cap too. Includes a provider matrix of what can and cannot be revoked automatically, an AWS Bedrock budget-action and manual revoke runbook, and a first-hour stolen-key playbook. Plugs into the Agent Cyber Defense Dashboard.
What's inside
01_Provider_Matrix.md02_Bedrock_Runbook.md03_Revoke_Playbook.mdkey_caps.csvusage_sample.csvspend_kill_switch.pymodule.json22_AI_Spend_Kill_Switch_Kit.pdf: every document in one printable guide
Verify it works
python3 spend_kill_switch.py --selftestprintsselftest ok. It starts mock OpenRouter, OpenAI and Anthropic admin APIs on localhost and checks reading usage, pagination, dry run, revoke calls and the webhook.python3 spend_kill_switch.py --caps key_caps.csv --usage-csv usage_sample.csvflags the sample batch key and Bedrock credential (Sample data), prints the dry-run and manual steps, changes nothing and exits 1.- Replace the sample rows in
key_caps.csvwith your keys (key ids or hashes only, never key values). Seton_breachtoalertuntil you trust your caps, thenrevoke. - Put admin keys in env vars (
OPENROUTER_MANAGEMENT_KEY,OPENAI_ADMIN_KEY,ANTHROPIC_ADMIN_KEY) and run with--provider ...in dry run for a few days. - Schedule it hourly or daily (cron, GitHub Actions schedule) with
--executeand--webhook-urlonce the dry runs look right. - Dashboard: add
--afk-jsonand save toresults/spend-kill-switch.json. Python 3.9+, no installs. Without--executeit is read-only.
Preview
First part of the main document, shown as-is:
01 Provider matrix: what can be capped and revoked automatically
A stolen model API key gets used to run someone else's workload on your bill (often called LLMjacking). This kit bounds the damage: a written cap for every key, a daily check, and a switch that turns the key off when it breaches. This page says, per provider, which parts spend_kill_switch.py automates and which stay manual. Checked against each provider's public docs on Oct 22, 2026; APIs change, so re-check the linked page before you rely on a step.
| Provider | Per-key usage via API | Hard cap at provider | Automatic revoke by this script | Reversible? |
|---|---|---|---|---|
| OpenRouter | Yes, USD (usage_daily, usage_weekly) from GET /api/v1/keys with a Management API key |
Yes, per-key credit limit with optional daily/weekly/monthly reset |
Yes: PATCH /api/v1/keys/{hash} with disabled: true |
Yes, PATCH disabled: false |
| Anthropic | Tokens per key from the Usage & Cost Admin API (usage_report/messages, grouped by api_key_id); the script estimates USD from your per-token rates |
Set spend limits in the Claude Console (no cap call in this script) | Yes: POST /v1/organizations/api_keys/{id} with status: inactive (Admin API key) |
Yes, set status: active |
| OpenAI | Tokens per key from GET /v1/organization/usage/completions grouped by api_key_id; USD estimated from your rates |
Set project budgets and alerts in the dashboard (no cap call in this script) | Yes for user-owned project keys: DELETE /v1/organization/projects/{project_id}/api_keys/{key_id} |
No. Deletion is final; issue a new key |
| OpenAI service-account keys | As above | As above | No. The delete endpoint refuses keys owned by a service account; the script prints a manual step | n/a |
| AWS Bedrock | No per-key usage in this script; use Cost Explorer / CUR exports into the CSV path | AWS Budgets with a budget action that applies a deny IAM policy | No. The script prints the IAM CLI command; see 02 | Yes, set the credential Active again |
| Anything else | Use the CSV path | Provider console | No: manual step printed | n/a |
Notes
- Token-based costs are estimates. Fill usd_per_mtok_in / usd_per_mtok_out in key_caps.csv from your provider's price page, and treat the number as a tripwire, not an invoice.
- Usage APIs lag. A stolen key can spend for some time before any API shows it, so set the provider-side cap too; the script is the second line, not the first.
- Admin keys are powerful. Keep them in your secret store, pass them as environment variables, and give the job that runs this script nothing else.
Sources: OpenRouter Management API keys https://openrouter.ai/docs/features/provisioning-api-keys ; Anthropic Usage & Cost API https://docs.anthropic.com/en/api/usage-cost-api and Update API key https://docs.anthropic.com/en/api/admin-api/apikeys/update-api-key ; OpenAI Admin APIs https://developers.openai.com/api/docs/guides/admin-apis and Delete project API key https://developers.openai.com/api/reference/resources/admin/subresources/organization/subresources/projects/subresources/api_keys/methods/delete ; AWS links in 02.