Skip to content

Subscription payer

A long-lived agent that pays a fixed monthly subscription to a single recipient.

Policy

const policies = [
  { type: "spend", token: "USDC", amount: 30, period: "monthly" },
  { type: "contract", whitelist: ["billing.acme.eth"] },
  { type: "rate", count: 1, period: "daily" },
] as const;

The agent cannot spend more than 30 USDC a month. It can only send to billing.acme.eth. It can only act once per day, which prevents accidental double-billing.

Implementation

import { createAgent, getAgent } from "@agensai/sdk";
 
const NAME = "my-agent";
 
async function ensureAgent() {
  try {
    return await getAgent(`${NAME}.agensai.eth`, {
      apiKey: process.env.AGENSAI_API_KEY!,
      ownerPrivateKey: process.env.AGENSAI_OWNER_PRIVATE_KEY! as `0x${string}`,
    });
  } catch {
    return await createAgent({
      name: NAME,
      chainId: 8453,
      apiKey: process.env.AGENSAI_API_KEY!,
      ownerPrivateKey: process.env.AGENSAI_OWNER_PRIVATE_KEY! as `0x${string}`,
      policies: [
        { type: "spend", token: "USDC", amount: 30, period: "monthly" },
        { type: "contract", whitelist: ["billing.acme.eth"] },
        { type: "rate", count: 1, period: "daily" },
      ],
    });
  }
}
 
async function payMonthly() {
  const agent = await ensureAgent();
  const tx = await agent.execute({
    to: "billing.acme.eth",
    amount: "30 USDC",
  });
  console.log(`paid: ${tx}`);
}
 
payMonthly();

Scheduling

Wrap payMonthly() in a cron job that fires on day 1 of each month. The agent's rate: 1/daily policy is your safety net if the cron mistakenly double-fires.