DEV Community

Aghazie David
Aghazie David

Posted on

Give Your AI Agent a Wallet It Can't Overspend (Telegraph + MCP)

Prevents runaway retry loops from draining funds

Disclosure: I built this for Telegraph Hackathon Season II (Apps & Agents, Mission 09: Personal Agent), which has cash prizes. Everything below runs on testnet with test USDC. No real money moves.

The problem: agents guess

Ask an AI agent "What's the BTC price right now?" or "Is it raining in Lagos?" and it has two bad options: guess from old training data, or call some random API you never approved.

Telegraph is a network where an agent can buy one live fact at a time, paying about $0.01 per answer. Providers (called miners) answer, and the payment settles in USDC.

That's exactly what a personal agent needs. But it raises a scary question:

Would you hand your agent a wallet?

Without guard rails, it could:

  • spend more than you meant,
  • pay for things it already knows,
  • and leave you no record of what it bought or why.

So I built Telegraph Purse: a small open-source MCP server that sits between your agent and the money.

What the purse does, in plain words

Think of it as pocket money with rules.

  1. Caps. You set a maximum per answer and a maximum per day. The purse checks the price Telegraph quotes before anything is signed. Over the limit? Nothing is paid.
  2. Policy. A short skill file tells the agent to only buy live facts (prices, weather now, fresh search), and to write a one-line why for every purchase.
  3. Receipts. Every purchase, every blocked attempt and every failure is written to a local ledger: what was asked, which miner answered, the cost, the time taken and the settlement reference.

How it works

You ask your agent a question
        │
        ▼
Agent decides it needs a live fact  ──►  writes a one-line "why"
        │
        ▼
Telegraph Purse
  1. ask Telegraph for the price (free quote)
  2. check per-call and daily caps   ──►  over cap? stop, log "blocked"
  3. sign the payment (test USDC)
  4. get the answer + receipt
  5. write a ledger row
        │
        ▼
Agent answers you, with cost and receipt
Enter fullscreen mode Exit fullscreen mode

What it looks like

This is a real session with a Hermes agent:

> what is the BTC price in USD right now?

Hermes: Using telegraph-purse to buy the live BTC price fact.
  Purse · purse_ask

Hermes: BTC is $74,114 USD right now.
Cost $0.01 · miner Cp Coingecko (#190070) · receipt 0x1e6ea5f7…8a089f
Enter fullscreen mode Exit fullscreen mode

One question, one cent, one receipt.

Try it yourself

You need Node 20 or newer. Works with Hermes or any MCP client.

Step 1: Check it works, no wallet needed (about 1 minute)

git clone https://github.com/cipoklean/telegraph-purse.git
cd telegraph-purse
npm ci
npm test        # offline tests: caps, ledger, payment rails
npm run probe   # free: lists live intents and price quotes from Telegraph
Enter fullscreen mode Exit fullscreen mode

npm run probe spends nothing. It just shows you what the network can answer right now and what each answer costs.

Step 2: Make a real (testnet) purchase

npm run setup
Enter fullscreen mode Exit fullscreen mode

This one command:

  • builds the project,
  • creates a fresh burner wallet in .env (never use a wallet with real funds),
  • mints 5 test USDC if the wallet has a little Sepolia ETH for gas,
  • installs the Hermes skill,
  • and prints an MCP entry to copy.

No Sepolia ETH yet? Send the printed address a little from any Sepolia faucet, then run npm run setup again.

Step 3: Connect it to your agent

Paste the printed purse: entry under mcp_servers: in ~/.hermes/config.yaml, then check it:

hermes mcp test purse   # you should see 7 tools
Enter fullscreen mode Exit fullscreen mode

Now ask your agent something live, like "What's the ETH price right now?", and watch it pay 0.01 test USDC through Telegraph.

Setting your limits

All settings live in .env:

Setting Default What it means
PURSE_MAX_PER_CALL_USD 0.05 The most one answer may cost
PURSE_DAILY_CAP_USD 0.50 The most it spends per day
PURSE_ALLOWED_INTENTS all Only allow certain kinds of facts, e.g. CRYPTO_PRICE_USD,WEATHER_CHECK_TEMPERATURE

With the defaults, the worst a runaway agent can do is about 50 answers a day for $0.50.

The 7 tools your agent gets

Tool Costs money? What it does
purse_ask ~$0.01 Buy one answer
purse_quote free See the price without paying
purse_budget free Today's spend vs. your caps
purse_receipts free Recent purchases
purse_balance free Wallet balance
purse_intents free What the network can answer
purse_verify free Check a purchase on the Telegraph node

What I learned the hard way

1. A failed answer can still cost money.
On Nov 1 the weather miner was down. Telegraph's node returned an error (HTTP 500) but still settled the payment, 8 times. My first version logged those as "not charged", which was wrong. Now any failure after a signed payment is recorded as failed_paid and counts against your caps. I wrote it up for Telegraph in a settlement report.

2. Check which chain you're actually on.
Parts of the docs say Base Sepolia. The live devnode settles on Ethereum Sepolia. Always confirm on a block explorer.

3. An allow-list must fail closed.
A free-text question only reveals which kind of fact it bought after you've paid. So when you set an allow-list, the purse refuses free-text questions instead of hoping for the best.

Results so far (testnet)

  • 36 on-chain payments of 0.01 test USDC from my agent's wallet (through Nov 14, 2026)
  • Bought crypto prices, weather and web search
  • A newer escrow payment option settled a live answer in 367 ms
  • 36 offline tests run in GitHub Actions, no wallet needed

Your turn

I'd really like to know if this works for you, or where it breaks.

  1. Clone the repo and run npm run probe (free, 1 minute).
  2. If you can, make one testnet purchase.
  3. Open an issue with what happened, good or bad: https://github.com/cipoklean/telegraph-purse/issues

Learn more about Telegraph:

Top comments (20)

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

The failed_paid lesson is the part most budget-guard write-ups skip — a cap that only counts successful purchases is accounting fiction when a down miner can settle 8 failed payments. Recording failures against the cap is the real control here, not the $0.50 daily limit. Do you blacklist a miner after repeated failed_paid events, or does the daily cap alone have to absorb the bleed?

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Thanks.

Honest answer: no numbers yet, it only shipped today and hasn't fired in real use. One refinement since: it now ignores failures caused by the settlement layer (facilitator timeouts and the like), so a healthy miner doesn't get blamed for a platform outage. The bad-minute vs bad-hour question is fair; both values are configurable (PURSE_QUARANTINE_FAILS, PURSE_QUARANTINE_MINUTES), and I'll share how often it fires once there's data.

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

That settlement-layer distinction is the right call — quarantining a healthy miner for a facilitator timeout would erode trust in the guard fast. Looking forward to the firing data once it's had real traffic; the frequency will say a lot about whether the thresholds are tuned right.

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Update: shipped it. After 2 failed_paid from the same miner (or the same auto-routed intent) within 60 minutes, the purse now skips it before even asking for a price, and the agent gets a clear "BLOCKED by quarantine" with when it lifts. Both numbers are configurable (PURSE_QUARANTINE_FAILS, PURSE_QUARANTINE_MINUTES).

Thanks for pushing on this. The daily cap is now the last line of defence, not the first.

github.com/cipoklean/telegraph-pur...

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

Shipped the same morning is the best possible answer to a design question. The 'BLOCKED by quarantine' message with a lift time is the detail that matters — the agent gets a reason, not a silent disappearance, so it can route around the miner instead of retrying into it. Two numbers I'd be curious about once it runs a while: how often the quarantine actually fires, and whether 2-in-60-minutes ever catches a miner that's just having a bad minute rather than a bad hour.

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Thanks, this is exactly the trap I fell into first.

Honest answer: there's no automatic blacklist yet. Today it's three layers:

• The skill tells the agent not to retry a miner after a failed_paid, so one bad miner costs one cent per question, not a retry loop.
• PURSE_PIN_MINERS lets you pin an intent to a known-good miner (weather is pinned by default), still paid through Telegraph.
• The daily cap absorbs whatever slips through, because failed_paid counts toward it.

(Telegraph did later credit those 8 failed payments back to escrow, but a client shouldn't count on that.)

Your question convinced me the cap shouldn't be the backstop. I'm adding an auto-quarantine: after 2 failed_paid from the same miner within an hour, the purse skips it and logs why. I'll reply here when it lands.

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

The three layers are honest, especially the parenthetical — a credit back from Telegraph is a refund, not a control, and counting on it would be the same mistake as counting on the cap. The auto-quarantine is the piece that changes the shape: after 2 failed_paid in an hour the miner stops costing anything, so the daily cap goes back to being a last line instead of the first. Glad the question landed somewhere useful.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

A failed answer still costing money is a nasty edge. Counting failed_paid against the daily cap is the right call.

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Thanks Robert. It surprised me too: the node returned HTTP 500 and still settled. Since then the purse also quarantines a miner after 2 paid failures in an hour, so it stops before the cap is even touched.

If you try it (npm run probe is free), I'd really value an issue with anything that breaks.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Quarantine after 2 paid failures in an hour is a neat guard. The 500-that-settles is still the weird part.

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Does the probe run cover the 500-that-settles case, or is that still on the list to reproduce?

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Good question. No: npm run probe is free and never signs a payment, so it can't reproduce a settlement. The 500-that-settles case is covered two ways:

• Offline: a test fakes an HTTP 5xx after signing, with a settlement header, and checks the purse records it as failed_paid and counts it against the cap.
• On-chain: the real occurrences are in the settlement report and reported to Telegraph here: github.com/telegraphprotocol/bounties/issues/228

Reproducing it live needs a miner that's actually down, so I can't trigger it on demand.

Thread Thread
 
robertpaschal profile image
Odinaka Robert Nnamani •

The offline fake-5xx test plus the on-chain report covers it well enough for me. A miner that is actually down is the one case you cannot stage on demand.

Collapse
 
ahmetozel profile image
Ahmet Özel •

Putting a dedicated budget layer between the agent and payment signing is the right architecture for paid tool calls. Enforcing per-call and daily caps inside the MCP proxy before signing ensures a runaway retry loop cannot drain a wallet even if the model ignores its policy file.

Logging blocked attempts alongside completed purchases in the same local ledger is another thoughtful detail. In practice, reviewing why an agent tried to buy a fact it already had in context is the fastest way to tighten the skill instructions and cut redundant spend.

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Thanks Ahmet, that's exactly why the why field is mandatory. purse_receipts shows each buy next to the agent's one-line reason, so a redundant purchase stands out right away ("user asked for BTC price" twice in a minute = tighten the skill, not raise the cap).

If you have an MCP client handy, I'd love you to run npm run probe (free, about a minute) and tell me where the setup is unclear. Issues welcome: github.com/cipoklean/telegraph-purse/issues

Collapse
 
aistackguide profile image
AI Stack Guide •

The pending-row-before-signing fix handles the crash case, and the other half of it is reconciliation: a small job that scans the burner wallet's transfers and matches each settlement hash to a ledger row. Anything on-chain with no row becomes an orphan instead of staying invisible — that is how you backfill the Nov 11 one you already found.

Two other gaps that a cap in USD does not cover. First, per-intent sub-caps: PURSE_ALLOWED_INTENTS says which facts are allowed, but not how much of the 0.50 any one of them may take, so a weather loop can eat the crypto budget. A per-intent daily amount fixes it with one more column.

Second, quote-to-sign slippage. You quote, check the cap, then sign, and the miner can re-price in between — the DEX problem. Storing the quoted price with the intent and refusing anything above quote + a small tolerance keeps the cap from being a rubber stamp.

Collapse
 
aghazie_david_4dc30f4c9a8 profile image
Aghazie David •

Thanks, this is really useful.

Reconciliation: agreed, the pending row only covers the crash case. A job that walks the wallet's USDC transfers and flags any settlement with no ledger row as an orphan is going on the list right next to it. That's exactly how the Nov 11 one would have surfaced on its own.

Per-intent sub-caps: good catch. The allowlist says what the agent may buy, not how much of the day each intent can take. A per-intent daily amount is a small change and I'll add it.

Quote-to-sign slippage: today the cap check runs on the quoted price right before signing. Pinning the signed amount to the stored quote plus a small tolerance, and refusing anything above it, makes the cap a real guarantee instead of a check on a number that can move. Adding that too.

If you want to try any of it, npm run probe runs a dry check without spending, and issues are open on the repo.

Collapse
 
alexshev profile image
Alex Shev •

A spending cap is strongest when it is paired with idempotency keys and an auditable reservation record. That way a retry cannot silently create a second charge, and an operator can see whether a refusal came from budget, policy, or settlement state.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.