agent-spend-gate
Your AI agent is about to spend money. This makes it ask you first.
Adopting this in an existing repo?
skill/scans for paid API calls, asks once which vendors should stay behind a prompt, and wraps the call sites. One fingerprint for the whole policy. Read the case study — 215 apparent call sites turned out to be 3 real ones.
One line before the paid call. A dialog says who gets paid and how much. Then macOS asks for your fingerprint or password. If either step fails, the call never happens.
┌─ Spending approval ─────────────────────┐
│ openai/gpt-4o requests 2.00 CNY for: │
│ summarise 10 documents │
│ Quantity: 10 calls │
│ │
│ [ Cancel ] [ Approve ] │
└─────────────────────────────────────────┘
↓ you click Approve
┌─ macOS ─────────────────────────────────┐
│ 🔒 Touch ID │
│ Approve spending 2.00 CNY to openai │
└─────────────────────────────────────────┘
↓ fingerprint accepted
the API call runs
Install
pip install agent-spend-gate
macOS only, Python 3.11+. On any other platform the gate refuses — it does not silently skip the check.
The one thing you have to do
Put require_approval in front of every billable call.
from spend_gate import require_approval
require_approval(amount=2.0, currency="CNY",
vendor="openai/gpt-4o",
purpose="summarise 10 documents",
quantity="10 calls")
# Only reached after a human approved. Now spend.
client.chat.completions.create(...)
If you do not wrap a call, that call is not protected. The gate is opt-in per call site — there is no interception layer, and nothing fails loudly when you miss one. That is the single most important thing to understand about this library.
Three ways to spend, pick one
| Situation | Use | Prompts |
|---|---|---|
| You know the price | require_approval(amount=...) |
one per call |
| A job whose total you don't know | open_envelope(max_amount=...) |
one per job, then charges run inside it |
| Many calls, small amounts | auto-approve policy | none, until a ceiling is hit |
Envelope: when the total isn't known upfront
from spend_gate import open_envelope
with open_envelope(max_amount=10.0, vendor="openai",
purpose="nightly summarisation", ttl_hours=24) as env:
for doc in docs:
rid = env.reserve(0.50) # hold capacity first
cost = call_api(doc) # actual cost, known only afterwards
env.settle(rid, cost) # charge what it really cost
One approval for "up to 10 CNY". Charges inside it do not prompt. It stops accepting charges when the cap or the 24 hours runs out. If you already know the price, env.spend(0.05) is the shortcut.
Ceilings: what a day, week or month may cost
spend-gate policy set --daily-limit 20 --weekly-limit 80 --monthly-limit 200
These apply to all spending, regardless of envelopes. Opening ten envelopes does not get around them.
When a charge would cross one, the dialog changes and tells you by how much:
Budget limit reached
openai/gpt-4o requests 5.00 CNY for: batch job
This would exceed your daily limit:
daily: 18.00 + 5.00 = 23.00 > 20.00
[ Cancel ] [ Just this once ] [ Allow for this day ]
"Allow for this day" writes a waiver until the period ends, so a long job keeps running instead of prompting at every file.
Per-vendor limits and whitelist
spend-gate policy set --vendor dashscope --vendor-daily-limit 5 --vendor-trusted
--vendor is a prefix; longest match wins, so dashscope/wanx2.1-t2i is covered by dashscope.
Trusted means: skip the dialog for this vendor while it stays inside its own and the global ceilings. It removes the interruption, not the limit. Exceed any ceiling and you are asked again.
What it does and does not protect
Protects: an agent or script running as you deciding to spend on its own. Both gates need a human action, and every attempt is recorded.
Does not protect: anyone who can edit this library (including an agent that can edit its own code), set SPEND_GATE_BYPASS=1, or obtain root. A call site you forgot to wrap is unprotected too.
This is a seatbelt, not a safe. Read SECURITY.md before trusting it with real money.
Every attempt is recorded
Two files under ~/.spend-gate/:
audit.jsonl— each approval decision: amount, vendor, purpose, method, resultledger.jsonl— each money movement, hash-chained.spend-gate verifydetects edited or deleted rows and checks the head against a Keychain checkpoint.
spend-gate verify # ledger chain + envelope reconciliation
spend-gate audit # recent decisions
Commands
spend-gate doctor # can this machine enforce approval?
spend-gate approve --amount 0.01 --vendor demo --purpose "smoke test"
spend-gate verify
spend-gate audit --tail 20
spend-gate policy show
spend-gate policy set --auto-under 0.05 --daily-limit 20
spend-gate policy set --vendor openai --vendor-daily-limit 20 --vendor-trusted
spend-gate policy load policy.json # batch, one fingerprint for all
Environment variables
| Variable | Purpose |
|---|---|
SPEND_GATE_HOME |
Override ~/.spend-gate |
SPEND_GATE_LEDGER_PATH |
Override the ledger path |
SPEND_GATE_AUDIT_PATH |
Override the audit path |
SPEND_GATE_NO_KEYCHAIN=1 |
Run without a Keychain; anchor checks report as skipped |
SPEND_GATE_BYPASS=1 |
Skips both gates. Tests only. Every use is logged as bypassed. |
Languages
Dialog, limit warning and Touch ID prompt follow the macOS UI language: English, Simplified Chinese, Traditional Chinese, Japanese, Korean, German, French, Spanish. Cantonese maps to Traditional Chinese; anything else falls back to English. Adding a language is a copy of en.json — a test fails if keys are missing.
Wrapping an existing codebase
The skill lives in skill/ and ships with this repository — no separate install.
python3 skill/scan_paid_calls.py /path/to/repo # what actually costs money
Then point your agent at skill/SKILL.md. It asks four questions, writes the policy, applies it in one authenticated call, and wraps the call sites. It also requires the agent to list what it did not wrap and why.
Why it is bundled rather than a separate repository: the skill depends on this CLI's flags, so the two must version together, and the files are small enough that a second repo would only add a way for them to drift.
License
Apache-2.0.
Metadata
Release files for agent-spend-gate 0.7.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agent_spend_gate-0.7.2.tar.gz | 48.1 kB | Without provenance | Details for agent_spend_gate-0.7.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agent_spend_gate-0.7.2-py3-none-any.whl | Python 3 | none | any | Details for agent_spend_gate-0.7.2-py3-none-any.whl |
Total release size: 88.3 kB
Release files / agent_spend_gate-0.7.2.tar.gz
| Download URL | agent_spend_gate-0.7.2.tar.gz |
|---|---|
| Size | 48.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4281b9c37d63bb193e4e35ebd6af9e4fd526046dfa24082eb7a567307d1dda3f
|
|
BLAKE2b-256 checksum How to use checksums |
d76a1f4c9fd782776bbc698e1f83b2bc6bc68ab9a9714edac28ed66aebb5557d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / agent_spend_gate-0.7.2-py3-none-any.whl
| Download URL | agent_spend_gate-0.7.2-py3-none-any.whl |
|---|---|
| Size | 40.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e32904484d935fd0ff0535ac310ae73662a247019c5727987dda288c0f294061
|
|
BLAKE2b-256 checksum How to use checksums |
53aedeea4bdd5e65988db1e559ce45b4b483ebaefa4e97351cba8845a1006cf8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for agent-spend-gate 0.7.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.