Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Recipe — Threshold-buy agent · Boson Protocol
Skip to content

Recipe: Threshold-buy agent

An autonomous agent that finds offers matching a criteria, commits to one when the price is right, redeems, and reports.

This combines Build for AI agents, the MCP stack, and the idempotency patterns.

import { generateText } from "ai"
import { anthropic } from "@ai-sdk/anthropic"
import { bosonProtocolPlugin } from "@bosonprotocol/agentic-commerce"
import { getOnChainTools } from "@goat-sdk/adapter-vercel-ai"
import { viem } from "@goat-sdk/wallet-viem"
 
const tools = await getOnChainTools({
  wallet: viem(walletClient),
  plugins: [bosonProtocolPlugin({ url: MCP_URL })],
})
 
const SYSTEM = `
You are a Boson Protocol buyer agent on Base Sepolia.
You buy items matching the user's criteriabut only if the price is below the threshold.
 
Rules:
- Never commit twice for the same goal. Persist the result to your memory store.
- Always wait for indexing before reading state you just wrote.
- On any error you can't classify, abort and report.
`
 
async function tryBuyOnce(goal: { keyword: string; maxPriceUSDC: bigint }) {
  if (await alreadyBoughtFor(goal)) return
  await generateText({
    model: anthropic("claude-sonnet-4-6"),
    tools,
    maxSteps: 12,
    system: SYSTEM,
    prompt: `Find an offer matching "${goal.keyword}" priced ≤ ${goal.maxPriceUSDC} USDC base units. If you find one, commit. Otherwise report no match.`,
  })
}
 
// Cron loop
setInterval(() => {
  tryBuyOnce({ keyword: "GPU", maxPriceUSDC: 500_000_000n }).catch(console.error)
}, 5 * 60_000)

Required state

The agent persists per-goal idempotency keys, so re-runs after crashes don't double-buy. See Build for AI agents → Idempotency & retry and Memory & state.

Production hardening

  • Wrap the agent in a circuit breaker — pause if the wallet balance drops or if reverts spike.
  • Cap max-spend per day at the wallet level (use a session key on a Safe).
  • Log every tool call with a correlation ID.
  • See Going to production.

Related