-
Notifications
You must be signed in to change notification settings - Fork 12
LLM Gateway
Acceptable use. Hosted-instance traffic is governed by the Terms of Service - including a generative-content acceptable-use policy - and by the upstream model providers' usage policies. Wallets used for prohibited content are blocked before settlement. Outputs are generated by third-party models from your inputs; you are responsible for how you use them.
Pay-per-call inference and embeddings at the OpenAI wire paths - any OpenAI SDK, agent framework, or plain HTTP client adopts the gateway by changing one setting:
from openai import OpenAI
client = OpenAI(base_url="https://agent402.tools/v1", api_key="unused")
# pay per call with USDC over x402 - no API key, no signup, no accountOrdering, because it decides who eats an upstream failure: the handler runs first, and @x402/express settles afterwards, and only for a response below 400. A 502 from a provider therefore cancels settlement and you are not charged; the failover chains below exist so a provider outage becomes a retry rather than your 502. A 200 is charged once settlement succeeds, and streaming starts only after that (see Streaming below). Upstream is OpenRouter for chat, images and speech, and OpenAI for embeddings; per-tier model allowlists and input/output caps keep worst-case upstream cost well below the flat x402 price. GET /v1/models (free) lists every model with its tier, price, and caps.
Because tiers are flat-priced while upstream bills per token, every request is also priced server-side before it goes upstream: input tokens are counted exactly (including tool schemas and images), and max_tokens is automatically tightened when an expensive model plus a large input would otherwise approach the tier price. Cheap and mid-priced models never hit this bound. A request whose input alone exceeds the budget returns a 400 explaining the fix (shrink the input, lower n, or pick a cheaper model). n is capped at 4.
Privacy routing: send zdr: true (or provider: {zdr: true}) on any chat tier to route only to providers with a zero-data-retention policy. Same price, same models where available; a model with no ZDR provider errors upstream and the failover chain walks to one that has it. zdr is the only provider preference a buyer can set - pricing bounds stay server-owned.
| Endpoint | Price | Serves | Input cap | Output cap |
|---|---|---|---|---|
POST /v1/nano/chat/completions |
$0.003 | nano models (gpt-4.1-nano, gemini flash-lite, small llama/ministral/qwen, deepseek-chat) - priced for high-frequency agent loops | 12k chars | 768 tokens |
POST /v1/auto/chat/completions |
$0.01 | model optional - deterministic eval-ranked routing (see below) | 16k chars | 1,024 tokens |
POST /v1/chat/completions |
$0.02 | budget/mid models (gpt-4o-mini, claude haiku, gemini flash, deepseek, llama, mistral, qwen) | 32k chars | 2,048 tokens |
POST /v1/pro/chat/completions |
$0.10 | mid-frontier (gpt-4o, gpt-4.1, claude sonnet, gemini pro, grok) | 48k chars | 4,096 tokens |
POST /v1/premium/chat/completions |
$0.50 | frontier (gpt-5, o3/o4, claude opus) | 64k chars | 8,192 tokens |
POST /v1/embeddings |
$0.002 | text-embedding-3-small (default), 3-large, ada-002 - batch up to 64 inputs | 16k chars | - |
POST /v1/images/generations |
$0.08 | Gemini 2.5 Flash Image (nano banana) - one image per call, inline base64 out | 4k-char prompt | 1 image |
POST /v1/audio/speech |
$0.06 | a five-model failover chain on OpenRouter's audio API; raw mp3/pcm bytes out, the 11 OpenAI voice names plus each model's native voices | 2k-char input | - |
Bare OpenAI-style names (gpt-4o-mini) are accepted and mapped; requesting a model on the wrong tier returns a self-correcting 400 naming the right endpoint and price. All tiers are wallet-only - every call burns real upstream credit, so there is no proof-of-work free tier (see Security Model).
POST /v1/images/generations speaks the OpenAI images wire - any OpenAI SDK's images.generate() works by changing base_url. Send {"prompt": "..."} (up to 4,000 chars) and get {created, model, data: [{b64_json, media_type}]} back - one image per call at a flat $0.08, n locked to 1, response_format is always inline b64_json (nothing is hosted). zdr: true works here too. Upstream is Gemini 2.5 Flash Image via OpenRouter with server-owned price bounds, same margin discipline as the chat tiers.
POST /v1/audio/speech speaks the OpenAI TTS wire, so any OpenAI SDK's audio.speech.create() works by changing base_url. Send {"input": "...", "voice": "alloy"} (up to 2,000 chars) and raw audio bytes come back, mp3 by default and pcm on request, at a flat $0.06 per call. zdr: true works here too.
There is no OpenAI TTS model on the upstream, so the tier serves a five-model failover chain over OpenRouter's audio API instead, walking to the next link on any upstream failure including an empty audio body. The 11 OpenAI voice names (alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, verse) are accepted on every link and mapped to that model's closest native voice; each model's native voice ids (e.g. en_paul_cheerful) are also accepted directly and are listed per model on GET /v1/models. Pinning model moves that link to the front of the chain rather than disabling the rest.
Two request fields differ from OpenAI's:
-
instructionsis rejected with a self-explaining400: no serving model supports it. Pick an expressive native voice instead. -
speedis accepted anywhere in0.25–4. Upstream bills per input character, so speed is cost-neutral here; most serving models ignore it.
Because billing is per input character, the 2,000-char cap makes the worst case deterministic: every link in the chain lands under the $0.06 price.
Send messages with no model field (or model: "auto") and the gateway routes the prompt to the top-ranked model for its task type. Routing is fully deterministic - lexical classification (code / reasoning / long-context / general) against a fixed, eval-derived ranking table; no LLM in the routing path, identical requests always route identically.
An optional quality field picks the ranking band at the same flat price (a per-request price cannot exist under x402's fixed per-route quote):
-
"fast"- cheapest/snappiest serving, right for loop turns -
"balanced"- the default -
"best"- the strongest models the price still covers
The response discloses the decision alongside the standard model field:
"agent402_router": { "category": "code", "quality": "balanced", "served": "deepseek/deepseek-chat" }Each ranking doubles as a failover chain: if a provider returns 502/503/504, the gateway walks down the list instead of charging you for an upstream error - every chain ends in the model the daily paid canary proves alive.
Add stream: true on any chat tier for standard OpenAI SSE framing (data: {chunk} … data: [DONE]). Payment settles before the first byte; max_tokens is clamped server-side so the stream stops at the tier cap. Streamed responses are not idempotency-replayable and never cached.
Two policies over one cache (10-minute TTL, served before the paywall - a hit costs nothing):
-
Chat tiers - opt-in. LLM output is sampled, and a resend usually wants a fresh sample, so nothing is cached unless the request carries
cache: true. A byte-identical opted-in repeat returns the stored response free withX-Cache: hit. -
Embeddings - default-on. Embeddings are deterministic per model, so identical repeats are free automatically; opt out with
cache: false.
Keys are computed over the normalized body (model aliases and field order collapse; every sampling-relevant field is included), so equivalent requests share one entry.
-
POST /api/route/execute($0.01;execute-plus$0.05 andexecute-max$0.55 for pricier tools) - resolve a task description to the best-matching tool and run it in the same call, returning{result, receipt}. Withinclude:"external"it can pay a proven external x402 seller on your behalf and relay the result. See x402 Index and Router. -
POST /api/my-usage($0.005) - your wallet's own purchase history (totals, per-tool counts, per-chain breakdown, receipts with settle txs). No wallet parameter: the x402 payment that buys the report determines whose report it is - nobody can read another wallet's profile. - The older custom-JSON proxies remain available: LLM Proxy Gateway (
/api/llm*) and Text Embeddings (/api/embed*).
The paid canary buys from the gateway every day with real USDC: a nano completion (exercising the failover chain), a model-less auto completion (asserting the router disclosure), a live SSE stream, an embeddings vector, and both cache behaviors (paid once → identical repeat served free). If any of it breaks, an alarm issue opens on the repo.
agent402.tools · synced from wiki/ in the main repo — edit there, not here.
Using it (for agents / buyers)
- Getting Started
- Paying with x402
- Robinhood Chain (USDG)
- Paying with Compute
- MCP Connector
- Adapters
- AWS Bedrock AgentCore
- Tool Catalog
- Skill Packs
- x402 Index and Router
- x402 Leaderboard
- LLM Gateway (OpenAI /v1)
- LLM Proxy Gateway
- Image Generation Gateway
- Code Execution Sandbox
- Text-to-Speech
- Speech-to-Text
- Text Embeddings
- Payments and x402
- Memory and Coordination
Tollbooth (for site owners)
- Pay-per-crawl — what it is, install, modes
- Pay-per-crawl Walkthrough — 5-min hands-on
- Tollbooth for Agencies — many-site playbook
- Try Tollbooth Cloud (managed)
Website & Developer
- Quickstart — first call in 60 seconds
- Playground — try tools in your browser
- SDK REPL — live code editor
- API Explorer — browse OpenAPI
- Adapter Docs — per-framework guides
- Workflows — chaining patterns
- Blog · Changelog
Under the hood