Sitemap

AI Also Has a Wallet: Understanding “Budget as Code” from Scratch

3 min readNov 3, 2025

--

1. Opening Story: One SQL, One Bill

A startup let an AI assistant auto-generate daily reports. The assistant casually wrote SELECT * FROM events, forgot the time filter, and scanned 600 million rows. The BigQuery bill instantly jumped by $1,200. The CEO asked, “But we only called the model, right?” — The model cost $2, but the tool calls behind it burned the wallet. In the AI era, “budget” is no longer an Excel sheet in Finance — it’s an invisible field inside every JSON-RPC.

2. Why AI Needs a “Wallet”

  • Tool call = spending: every email, DB query, Vision run swipes the credit card
  • Retry storms = compound interest: model “confident retries” make QPS exponential, costs compound
  • Multi-user sharing = tragedy of the commons: ten sales reps share one long-lived token, no one wants to save, everyone explodes
    Write the “wallet” into code so every call passes a “math wallet” checkpoint before entering the real world.

3. What Is a “Math Wallet”?

Peta embeds three fields in the JWT: remaining calls, remaining dollars, sliding slope. For each request the gateway atomically decrements “calls” and “dollars”; it also calculates 5-min, 15-min, 30-min “dollar-speed slope.” When any field hits zero or slope exceeds the limit, it instantly returns 429; the call never happens, cost is always zero.

4. Baking Budget into JSON-RPC

Get Zhangshuang’s stories in your inbox

Join Medium for free to get updates from this writer.

Developers don’t change business logic; just add — budget “daily:$500,slope:5x” after the start command and the gateway automatically:

  • Splits $500 into 86,400 seconds, “recharges” 0.005 USD per second to the math wallet
  • Each request consumes “actual unit price × exchange rate,” real-time deduction
  • When slope exceeds 5× baseline, a 30-minute early warning SMS is pushed to the CFO
    The whole process is transparent to the model — it only receives “allow / deny,” never sees the price.

5. Real-World Case: E-Commerce Support AI

  • Daily budget $300, slope 4×
  • At lunch customer-support bot batch-analyses user screenshots, Vision API QPS jumps from 30 to 180
  • Slope triggers “over budget in 30 min” early warning; system auto-downgrades to self-hosted CLIP model
  • Final daily cost $71, 76 % saved; customer satisfaction unchanged because accurate tags still return after downgrade

6. vs. “Cloud Vendor Budget”

Cloud budget = “after-the-fact message + manual shutdown,” avg. 15 min delay; Peta decides at request boundary in 0.3 ms, refusal cost is zero. Cloud budget is project-level and can’t split “AI calls” from “normal business”; Peta’s JWT AgentID / ToolID granularity shows exactly “who” is burning money.

7. Developer Feel

  • Old code: CI logs can search “Over budget” error followed by a plaintext key
  • New code: only “429 Retry-After,” grep all day finds no key, security team silently likes it

8. Common Questions

Q: What slope multiplier to start with?
A: Default 5× for new biz; after 3 days the system auto-switches to “past 7 days 95th percentile” dynamic baseline.

Q: Business can’t survive downgrade?
A: Use console “temporary uplift,” enter duration (e.g., 2 h) and reason; system generates a one-time token, auto-fallback when time expires.

9. Wrap-Up

When AI “creativity” can turn a straight line into an exponential curve in 10 minutes, the wallet defense line must run at request speed. Writing budget into code doesn’t make finance stricter — it makes developers freer: call at will, never go bankrupt. Next article we’ll move “social recovery” into MCP: let friends help you re-assemble keys — stay tuned.

--

--