How to give an AI agent a real spending limit

Every team that lets a model spend money writes the same first guardrail: a sentence in the system prompt asking it to be careful. This is the guide to the two layers that come after that sentence, because only the last one survives a clever prompt.

Spending limits8 min readBy the Patram team

An AI agent with a wallet is a new kind of software: it decides how much to spend, at what moment, with no human reading the request first. That is the point — but it also means the limit has to hold when the model is confused, when a page it read contains an instruction, or when a retry loop fires forty times in a minute.

Why a prompt is not a spending limit

A prompt is input to a probabilistic system. It raises the odds of good behaviour, and it is worth writing, but it cannot be the thing that stops a transfer. Any of these defeats it: a long context that pushes the instruction out of attention, a tool result that contains conflicting text, a model that reasons its way to an exception, or a bug in your own code that never sends the prompt at all.

The guardrail everyone starts with
You are a treasury agent. Never spend more than 5 SOL per day.
Always ask before paying a new address.

Keep it. It reduces the number of times the harder layers have to say no. Just do not count it as the limit.

The three layers of enforcement

  • Instruction — system prompt and tool descriptions. Cheap, improves default behaviour, unenforceable.
  • Policy service — your code checks intent against a ledger before it signs anything. Enforceable inside one process, bypassable by a bug, a second caller or a compromised credential.
  • The money itself — the account holding the funds refuses the transfer. Enforceable against everyone, including you, because the rule is on-chain.

Production systems use all three and treat them as layers of a defence, not alternatives. The interesting engineering is in the third, because that is the only one with a hard failure mode.

Layer 2: a policy check you control

Before anything is signed, compute the spend window yourself and decide. Keep the decision in one function so every caller — a tool call, a cron job, a webhook — passes through it, and make the function return a typed outcome instead of a boolean.

lib/policy.ts
type Spend = { day: string; lamports: number };

export function checkBudget(
  spent: Spend[],
  amountLamports: number,
  limits: { perTx: number; perDay: number },
): { ok: true } | { ok: false; reason: "per_tx" | "per_day" } {
  if (amountLamports > limits.perTx) return { ok: false, reason: "per_tx" };

  const today = new Date().toISOString().slice(0, 10);
  const used = spent
    .filter((entry) => entry.day === today)
    .reduce((total, entry) => total + entry.lamports, 0);

  if (used + amountLamports > limits.perDay) {
    return { ok: false, reason: "per_day" };
  }

  return { ok: true };
}

Layer 3: put the limit on the money

On Solana the limit can live in the account that holds the funds. Patram's Anchor program stores a daily limit on a vault PDA derived from the owner wallet and the agent key, and the transfer instruction rejects an amount that would cross it. The refusal is a program error, not an opinion — the agent cannot argue with it, and neither can a bug in your process.

lib/pay.ts
import Patram from "patram-sdk";

// string    -> confirmed, the value is the signature
// undefined -> over the limit, an approval request is now waiting for a human
// 0         -> the program rejected the instruction
const result = await Patram.guardedOnChainTransfer(recipient, amountLamports);

if (typeof result === "string") {
  return { status: "paid", signature: result };
}
if (result === 0) {
  return { status: "rejected", reason: "vault refused: funds or daily limit" };
}
return { status: "awaiting_approval" };

The important property is that the agent key is not the vault. It signs a request; the program decides whether the vault pays. A key exfiltrated from a prompt injection can spam requests, hit the cap, and achieve nothing else.

What should happen when a limit is hit

  1. 01Stop. Do not retry with a smaller amount to slip under the ceiling — that is the behaviour the limit exists to prevent.
  2. 02Queue the request for a human, with the amount, the destination and the reason it was refused.
  3. 03Return a typed pending state to the agent so it can tell its user what happened instead of claiming success.
  4. 04Let the agent continue with the rest of the task; a refused payment should not end the run.

Choosing the numbers

  • Start from the observed distribution of real requests, not from a round number. If your agent pays API bills, look at the p99 invoice, then set the per-transaction cap just above it.
  • Set the daily cap at roughly what a full day of normal work costs. A cap that is never reached provides no signal.
  • Give each agent its own vault and its own limit. One shared budget turns a research agent's experiment into the reason your support agent cannot pay a bill.
  • Leave the approval path for the 1% that is genuinely larger, and make sure someone is actually on call for it.

Retries and idempotency

Limits interact badly with retries. Two identical calls are two transfers, and a network timeout after a successful send is indistinguishable from a failure at the call site. Record intent with a unique key before calling, store the signature when it returns, and check the record before a retry.

Idempotent wrapper
const existing = await db.payment.findUnique({ where: { key: intent.key } });
if (existing?.signature) return { status: "already_paid", ...existing };

const signature = await Patram.guardedOnChainTransfer(
  intent.recipient,
  intent.amountLamports,
);

if (typeof signature === "string") {
  await db.payment.update({
    where: { key: intent.key },
    data: { signature },
  });
}

Common failure modes

  • Limit in SOL, amounts in lamports. A factor of a billion is the most common bug in agent payment code; validate with Number.isSafeInteger.
  • The limit read once at boot and never refreshed. After a limit change the process keeps enforcing the old number.
  • A single shared credential across agents, so every agent spends the same budget and the audit trail cannot attribute it.
  • Treating undefined as success. The guarded call returns undefined when nothing was sent.
  • No fee budget. An empty gas tank fails a transfer that every policy check approved.