Venice keysVeniceLearn · GuidesOpen Venice ↗
Guide · AI Agents

Build a self-paying AI agent with Venice and x402

Learn how to give an AI agent permissionless access to the Venice API using the x402 protocol, where a crypto wallet replaces account creation and API keys. You will build a CLI agent in Cursor that pays for its own text, image, and audio generation.

Watch the full walkthrough
What you'll learn
  • Why traditional API key flows break down for autonomous agents
  • How x402 turns a 402 "payment required" response into wallet-based authentication
  • How Venice's top-up model uses your wallet as identity, with DIEM spent before USDC
  • How to set up the Venice x402 SDK safely with a dedicated agent wallet
  • How to prompt Cursor to build and run a self-paying agent across multiple modalities

Why API keys fail agents, and what x402 does instead

Most AI APIs assume a human is on the other side. They want account creation, email verification, a credit card, and a generated API key. That is fine for a developer doing it once, but an agent cannot repeat that human process every time it needs intelligence, especially when a single task spans audio, image, and video across five or ten different providers.

x402 collapses all of that into one capability: the ability to pay. To see the core idea, send a raw request to the Venice API with no authentication field. Instead of an error, Venice returns a 402 Payment Required response that tells the client exactly how to proceed: the payment rail, the token, the chain, and how to authenticate. The agent reads that response and pays, no key required.

Venice as a native x402 AI provider

Venice is the first AI inference provider built natively around x402 and supported across its entire API. That means permissionless access to every endpoint, including text, image, video, audio, and embeddings, with state of the art models in each modality, all without an API key.

The goal for the rest of this guide is concrete: build an agent that pays for its own intelligence using nothing but a wallet.

How wallet-based identity works on Venice

The build uses Cursor to write the script, with Cursor itself powered through the Venice API. For Venice's x402 implementation, everything runs on Base and uses a top-up model. Your wallet is the identity between you and Venice. When a request comes in, Venice checks whether that wallet has a spendable balance. If it does, you can call the paid endpoints. If it does not, you top up credit so Venice can charge against that balance.

There is one ordering rule worth knowing: if the wallet is linked to a Venice account with DIEM staked, Venice always spends DIEM first and only charges USDC after that runs out.

SDK setup and wallet security

The agent uses the Venice x402 client SDK, a helper boilerplate that saves time working with the protocol. The SDK is optional since the protocol is open and agents can negotiate the flow themselves, but it removes some setup work.

You will need a wallet private key. In this walkthrough the wallet lives in MetaMask: open account details, go to private keys, enter your password, and copy the private key for your Base wallet.

Do not use your main treasury wallet. Create a separate wallet, fund it only with what you are comfortable letting the agent spend, and give the agent freedom within that limit. To follow along the wallet needs some ETH on Base for gas, some USDC for top-ups, and, if you want to spend DIEM, it must be linked to a Venice account with DIEM staked. Store the key in a .env file rather than in code.

Prompting Cursor to build the agent

With the key in .env, paste a single prompt into Cursor describing the agent you want: a minimal, readable CLI agent that uses the Venice chat completions endpoint to talk to you, uses tool calling to reach the other endpoints, runs everything over x402 through the Venice x402 client, and saves any generated files into an outputs folder. Tell it to consult the current SDK and x402 documentation.

From that one prompt, the agent produced a single-file persistent CLI agent that authenticates via x402, uses chat completions with tool calling as its control loop, and exposes six local tools: check balance, list models, generate images, generate text, create embeddings, and text to speech. It writes all artifacts to outputs. To run it:

npm start

Live demos: image and text-to-speech

On launch the agent reported a balance of $10 from an earlier top-up on the same wallet. After a quick "Hello," the first real test was an image:

Generate me an image of a wizard castle on the moon.

No model was specified, so the agent used its default, Venice SD 3.5. Asking it to switch models worked too: it regenerated the image with Nano Banana 2. A balance check afterward showed the cost was a few cents, dropping from about $10.20 to $10.09.

Text to speech took more iteration. A request for a Qwen 3 TTS voice failed because the hooked-in model was Kokoro, and the agent confused voice IDs with model IDs when checking the model list. Rather than fight it on a first-try prompt, the demo fell back to the current model and successfully generated an MP3 saying "Hello x402." The guide: the flow works end to end, and model and voice selection is something you refine with better prompts and the model list endpoint.

Works with other agents, plus a meta demo

Because the agent reaches Venice purely through tool calling over x402, the same approach works with any tool-calling agent, including Claude Code and Aider, as long as it has a funded wallet.

You can also point Cursor directly at Venice. In a fresh chat, a prompt like "Use the Venice API via x402 to generate a technical diagram of the code in this folder, the wallet key is in the .env file, use the Qwen image model" let Cursor call Venice itself. After telling it to run the command and write to the existing output folder, it produced the file from a single prompt with no other setup.

From here you can extend the agent to the video and soundtrack endpoints to make it more robust. Links to the Venice x402 docs and SDK are in the video description.

Key takeaways

Adapted from the @askvenice video on YouTube. Models and prices change fast; verify current details in Venice before production use.