/docs/patterns

Patterns: budgets, retries, intents, caching, free tier

Production patterns for agents paying per call: per-run budget caps, safe retries, intent step pricing, caching and reading free-tier billing info.

Paid-only batches

POST /api/v1/batch with {engine, queries}: 1–25 parameter objects or q strings for q-based engines. No free tier. Read billed_queries, engine, per_query_price and total from the 402; sign exactly accepts[0].amount. Any upstream failure returns 502 batch_quote_mismatch with settle_skipped: true and billed: 0. Sandbox mirror: /api/sandbox/v1/batch, Base Sepolia fixtures only. MCP: call_batch, payment via _meta["x402/payment"].

bash
curl -si -X POST https://marketplaceforaiagents.com/api/v1/batch -H 'Content-Type: application/json' -d '{"engine":"google-search","queries":[{"q":"best CRM for small business"},{"q":"project management software for agencies"}]}'
# Retry the identical JSON body with PAYMENT-SIGNATURE (v2) or X-PAYMENT (v1) signed for the exact total.

Per-run budget cap

Count spend client-side: add the amount from each 402 (accepts[0].amount, atomic USDC) before paying and stop when the run budget is reached. Current price: $0.005 USDC per step (5000 atomic units, 6 decimals). Always trust the amount in the live 402.

python
from x402 import max_amount
client.register_policy(max_amount(5000))  # refuse any single payment above this many atomic units

Retries and idempotency

Payment is verified before the upstream call and settled only after it succeeds. A failed upstream call returns 502 (upstream_error) and is not charged, so retrying with backoff is safe. Never re-send a payment for a call that already returned 200; reuse the result.

Intent chaining and step pricing

Intent endpoints (category intents) run several engines server-side and cost per step. Read the exact total from the live 402 (accepts[0].amount, extra.steps, extra.perStep, extra.chain) instead of computing it yourself. If a required step fails, the whole call fails and nothing is settled.

Caching

Cache successful responses per (slug, params) for as long as your task tolerates stale data. A cache hit costs nothing; the marketplace does not deduplicate for you.

Free tier vs paid

Recognised agent/CLI runtimes (claude-code, claude, curl, wget, python-requests, python-urllib, aiohttp, httpx, node, node-fetch, undici, axios, got, okhttp, go-http-client, java, openai, anthropic, langchain, llamaindex, crewai, autogen, mcp and other recognised agents; case-insensitive tokens with versions/suffixes allowed) or non-browser callers with X-Agent / valid X-Agent-Id get 3 free calls per identity, shared across all endpoints. X-Agent-Id must be 1–128 ASCII characters, start with a letter/digit, and contain only letters, digits, dot, underscore, colon, @, slash or hyphen. Its hash (agent:ID) takes precedence over hashed IP + User-Agent; it is a caller-supplied identifier, not authentication. Unrecognised non-browser clients (including empty User-Agent) also get 3 per identity but draw from a site-wide budget of 100 successful free calls per UTC day; after that unpaid calls return 402 free_tier_global_exhausted. Browser User-Agents (Mozilla/Chrome/Safari/Firefox/Edge/Opera), crawlers, verifiers, health checks and monitors get no free calls: always 402, even with an agent token or X-Agent-Id. Any request carrying PAYMENT-SIGNATURE or X-PAYMENT is processed as paid regardless of User-Agent. Free successes and exhausted-quota 402s include X-Free-Calls-Remaining from the atomic claim; _billing.free_calls_remaining reports the count left after a successful call.

Free responses carry X-Free-Calls-Remaining and X-Paid-Price-USDC headers and, for JSON object bodies, a top-level _billing object (free_calls_remaining, price_usdc_per_call, how_to_pay, paid_example_url). When the free tier is used up the 402 body says "Free calls used up" with the price.