Home › Docs › Adapters › Vercel AI SDK

Vercel AI SDK Adapter

agent402-ai-sdk

Four meta tools (find, route, call, about) as AI SDK tool objects, keyed by name for generateText and streamText on any provider the SDK supports.

Install

npm install agent402-ai-sdk ai @ai-sdk/openai zod @x402/fetch @x402/core @x402/evm viem

Quick start

import { agent402Tools } from "agent402-ai-sdk";
import { generateText, stepCountIs } from "ai";
import { openai } from "@ai-sdk/openai";
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const payClient = new x402Client();
registerExactEvmScheme(payClient, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
const payFetch = wrapFetchWithPayment(fetch, payClient);

// an object keyed by tool name; agent402_call pays search and answer through this fetch
const tools = await agent402Tools({ fetch: payFetch });

const { text } = await generateText({
  model: openai("gpt-4o"),
  tools,
  stopWhen: stepCountIs(6),   // find, search, answer, then reply
  prompt: "Search the web for x402 payment protocol adoption, then answer with citations: what is the x402 payment protocol?",
});

No wallet yet? The pure-CPU tools (hash, uuid, base64, markdown, JSON and more) run free with proof-of-work and no wallet:

// the same four tools with no fetch: agent402_call pays pure-CPU tools with proof-of-work
const tools = await agent402Tools();

Configuration

OptionTypeDescription
fetch typeof fetch A payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that agent402_call uses for wallet-only tools.
fetchImpl typeof fetch The plain fetch used for the unpaid lookups (find, route, about) and proof-of-work calls. Defaults to the global fetch.
baseUrl string Point to a self-hosted Agent402 instance.
// A payment-wrapped fetch (@x402/fetch for x402, mppx for MPP) that agent402_call uses for wallet-only tools.
agent402Tools({ fetch: payFetch })

// The plain fetch used for the unpaid lookups (find, route, about) and proof-of-work calls. Defaults to the global fetch.
agent402Tools({ fetchImpl: globalThis.fetch })

// Point to a self-hosted Agent402 instance.
agent402Tools({ baseUrl: "https://my-agent402.example.com" })

Works with

generateText streamText Any AI SDK provider
← All adapters Integrations overview GitHub source →