Giving a Vercel AI SDK agent a wallet, safely
The AI SDK made tool calling pleasant. Payments are just another tool, except the side effect is irreversible, which changes how the tool should be shaped and how the result should read.
In this guide
A tool that moves money differs from a tool that fetches weather in three ways: it can fail partially, its result may be pending for an hour, and a wrong call cannot be undone by retrying. Design the tool surface with that in mind and most of the risk disappears.
The tool surface
patram-sdk ships two tools already: intraTransfer for paying an address, and fetchPremiumData for a paywalled URL. Both have schemas the model can read and a return contract it can reason about.
import { generateText, stepCountIs } from "ai";
import { google } from "@ai-sdk/google";
import Patram from "patram-sdk";
import { patram } from "./lib/patram";
void patram; // init() has run in this process
const result = await generateText({
model: google("gemini-2.0-flash"),
system:
"You are a treasury agent. Pay invoices with intraTransfer and buy " +
"paywalled data with fetchPremiumData. Never invent an amount. If a " +
"payment is pending approval, tell the user and stop.",
tools: Patram.PatramTools,
stopWhen: stepCountIs(5),
prompt: "Pay 0.05 SOL to 4r9zHRRnZigrewvrB4nEVqbzJtBymQcWeknH6nmHMaDc",
});
console.log(result.text);Write your own tool when the model needs intent
The built-in tools take an address and an amount. If your agent pays invoices, it is better to expose payInvoice(invoiceId) and let your server resolve the address and amount. The model then cannot get the destination wrong, because it never names it.
import { tool } from "ai";
import { z } from "zod";
import Patram from "patram-sdk";
export const payInvoice = tool({
description:
"Pay a pending invoice by id. Only call this with an id returned by listInvoices.",
inputSchema: z.object({ invoiceId: z.string().uuid() }),
execute: async ({ invoiceId }) => {
const invoice = await db.invoice.findUniqueOrThrow({
where: { id: invoiceId },
});
const signature = await Patram.guardedOnChainTransfer(
invoice.recipient,
invoice.amountLamports,
);
if (typeof signature === "string") {
return { status: "paid" as const, signature };
}
return {
status: "awaiting_approval" as const,
message:
"Over the agent's daily limit. Nothing was sent; the owner must approve it.",
};
},
});Shape the result for the model
Tool output is prompt text. A result like { ok: false } invites a retry; a result that says what happened, what did not happen and what to do next ends the run cleanly.
{
"status": "awaiting_approval",
"paid": false,
"message": "The payment exceeds the daily limit and was queued for the
owner. Do not retry. Tell the user it is pending and wait."
}Keep the credentials out of the model
- Never pass the agent secret key, the gas key or the API key into a prompt, a tool schema or a tool result.
- Run the SDK in a server runtime. It is ESM-only and expects Node 20 or newer.
- Give each agent its own process. The SDK keeps configuration in module state, so two agents in one process will share it.
Cost and latency
A guarded transfer adds one RPC round trip plus confirmation, typically under two seconds on devnet. Budget for it in your step limit: a run that pays three invoices needs enough steps to pay them and to summarise.