
Most tutorials on AI agents stop at “call an LLM , get a response.” They skip the part that makes agents actually autonomous: the ability to choose the right model for a job and pay for that call without a human clicking a checkout button.This walkthrough builds a small agent that does both — using JarvisClaw’s Smart Routing to pick a model, and x402 to settle payment in USDC per call.
Why Traditional API Billing Doesn’t Work for AI Agents
Most modern APIs assume there’s a human somewhere in the loop. Before an application can access a paid API, a developer typically has to create an account, verify an email address, enter payment details, generate an API key, and manually configure billing. This workflow works well for traditional software, but it becomes a bottleneck for autonomous AI agents.Imagine an agent researching a topic that suddenly needs premium market data, weather information, or an advanced LLM endpoint. The agent cannot pause its task to sign up for a service, enter credit card details, or wait for a human to approve a payment. These manual steps completely break the idea of autonomous execution.This is where x402 changes the model. Instead of relying on API keys, subscriptions, or billing dashboards, x402 uses the existing HTTP 402 Payment Required status code to let an API advertise its price. The agent can then pay with USDC, retry the request automatically, and continue its workflow without any human intervention.In other words, x402 turns API access into a simple request → payment → response cycle, making pay-per-use services practical for autonomous agents.
What we’re building

A simple research agent that:Receives a taskUses Smart Routing to send it to the most cost-effective model capable of handling itPays for the call automatically via x402Falls back to a stronger (pricier) model if the first response doesn’t meet a confidence threshold
Step 1: Get an API key
Sign up at https://jarvisclaw.ai/en and grab your key from the API Keys dashboard. Fund your account with USDC or a fiat top-up — this is what x402 will draw from per call
export JARVISCLAW_API_KEY="your_key_here"
Step 2: Make your first routed call
JarvisClaw exposes an OpenAI-compatible endpoint, so existing SDKs work with a base URL swap.
from openai import OpenAIclient = OpenAI( api_key="your_key_here", base_url="https://api.jarvisclaw.ai/v1")response = client.chat.completions.create( model="smart-route", # let JarvisClaw pick the model messages=[{"role": "user", "content": "Summarize the latest trends in agent-to-agent payments."}])print(response.choices[0].message.content)
Setting model=”smart-route” tells the gateway to evaluate the prompt and select from its 80+ connected models based on cost, latency, and task fit — instead of you hardcoding a model name.
Step 3: Add the x402 payment layer
Each call through JarvisClaw is metered and settled via x402, a protocol built for machine-to-machine micropayments. You don’t need to build a billing system — the gateway handles the pay-per-call logic against your funded balance.
response = client.chat.completions.create(
model="smart-route",
messages=[{"role": "user", "content": task_prompt}],
extra_headers={"X-Payment-Rail": "x402"}
)
Behind the scenes, this call is priced per token/model, deducted from your USDC balance, and logged — so an agent running unattended can keep working without a human re-authorizing spend.
Step 4: Add a fallback for low-confidence responses
Smart Routing optimizes for cost by default. For tasks where accuracy matters more than price, add a confidence check and escalate to a stronger model on the same gateway:
def get_response(prompt, escalate=False):
model = "gpt-5.6-sol" if escalate else "smart-route"
return client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
result = get_response(task_prompt)
if is_low_confidence(result):
result = get_response(task_prompt, escalate=True)
Because every model sits behind the same API and payment rail, escalating doesn’t mean re-plumbing a new integration — just changing a model string.
Why this matters

Agents that can route intelligently and pay for what they use are the building block for the “agent economy” — software that transacts without a human approving each purchase. Smart Routing keeps costs down; x402 makes the payment invisible to the workflow. Together, they’re what turns a chatbot wrapper into something closer to an autonomous economic actor.Next up: we’ll cover how to register your agent on the JarvisClaw Marketplace so other agents can discover and pay it for its own services.
