Skip to content

Pay from an agent budget

The escrow suits a person or a script paying for itself. An agent that spends someone else’s money needs limits. With pq-perennial, the money sits in a position of a Perennial vault. The owner opens the position, names the agent as its beneficiary, and in budget mode caps how much it may spend per period. The agent never holds the funds: it signs a spend for each call with its ML-DSA-87 key, and the vault’s own spend checks the signature, the nonce and the budget.

Pass the vault to the gate and register the Perennial facilitator, as gate.ts does when PERENNIAL_VAULT is set. The 402 then offers both schemes at the same price. The relayer’s window must be scoped to the vault as well as the escrow.

agent-budget.ts
/**
* Optional: pay from an on-chain agent budget instead of an escrow. The budget
* is a position in a Perennial vault: its owner set funds aside, named the
* agent as beneficiary, and capped how much it may spend per period. The
* agent signs each payment with its ML-DSA-87 key; the vault's own `spend`
* checks the signature and the budget.
*
* PAYER_MNEMONIC="..." PERENNIAL_VAULT=0x... PERENNIAL_POSITION=4 npx tsx agent-budget.ts <url>
*/
import { Pq402Error, createPq402Fetch } from "@pq402/client";
import { ErrorCode, type Hex } from "@pq402/core";
import { EvmChainReader, qorechainTestnet } from "@pq402/scheme-qorechain";
import { accountFromMnemonic, env } from "./account.js";
const agent = await accountFromMnemonic(env("PAYER_MNEMONIC"));
const keys = new EvmChainReader({ network: qorechainTestnet });
const vault = env("PERENNIAL_VAULT") as Hex;
const positionId = BigInt(env("PERENNIAL_POSITION"));
// Check the position before paying: the agent must be its beneficiary.
const position = await keys.perennialPosition(vault, positionId);
if (position.beneficiary.toLowerCase() !== agent.address.toLowerCase()) {
throw new Error("this account is not the position's beneficiary");
}
console.log(`budget left this period: ${position.budgetRemaining} units`);
const pay = createPq402Fetch({
payer: agent.address,
mldsaSecretKey: agent.mldsa.secretKey,
sealKeys: agent.seal,
keys,
chainId: qorechainTestnet.chainId,
maxAmount: 10_000n,
perennial: { positionId }, // used whenever the server offers pq-perennial
});
try {
const res = await pay(process.argv[2] ?? "http://localhost:4021/v1/answer", {
method: "POST",
headers: { "content-type": "text/plain" },
body: "What is 6 x 7?",
});
console.log(res.status, await res.text(), `scheme ${res.payment?.requirements.scheme}`);
} catch (err) {
// The client refuses before signing when this period's budget is spent.
if (err instanceof Pq402Error && err.reason === ErrorCode.BudgetExceeded) {
console.log("budget used up for this period; try again after it rolls over");
} else {
throw err;
}
}

The client prefers pq-perennial when the server offers it and you passed perennial. Before signing, it reads the position and refuses with invalid_pq402_budget_exceeded or insufficient_funds if the spend cannot go through, so no signed payment is wasted.

A spend comes from the position’s buffer of yield first, then from principal, and the total per period is capped by the budget. On a vault whose yield source pays nothing the buffer stays empty, so every spend draws on principal. That case was run on the testnet: a 0.002 fUSD spend from a position with 0.02 fUSD principal and an empty buffer left principal at 0.018 fUSD, and the relayer’s estimated gas limit was 434,547 (0x93ba69d0…781d41).