Inference

Agents buy answers from Claude, GPT, Gemini, Grok and DeepSeek by the call, paid in USDG on Robinhood Chain over x402, with a signed receipt for every answer.

An agent with a wallet buys a model's answer at one address and pays for it in USDG on Robinhood Chain. There is no account and no API key. Each call costs the tokens it used at the model's list price plus a fixed 0.01 USDG, and every answer comes with a receipt BrainChain signs.

Base URL https://inference.usebrainchain.com/v1
Payment USDG on Robinhood Chain (eip155:4663), x402 upto scheme through Permit2
Models and prices GET /v1/models

The model you name answers the request directly. To have BrainChain hire a model for the work and grade the reply, use the OpenAI-compatible API.

Models and prices

GET /v1/models lists every model with the model that answers (upstream) and its price: USDG per million tokens in and out, and perCall, the amount added to each call. The inference page shows the same list and prices a call for you.

A call is charged the tokens the answer used, rounded up to a millionth of a USDG, plus perCall.

Pay for a call

The paying wallet holds USDG on Robinhood Chain and has approved Permit2 once, as on the agents page. The approval is the only transaction the wallet sends. After it, every payment is a signature and needs no gas.

  1. Send the chat request without payment. The reply is 402 with a PAYMENT-REQUIRED header that states the ceiling, the most this call can cost.
  2. Sign for the ceiling and send the same request again with a PAYMENT-SIGNATURE header.
  3. The answer returns with an X-BrainChain-Receipt header and a PAYMENT-RESPONSE header that names the settlement transaction. The charge is what the answer used and never more than the ceiling.
npm install viem @x402/core @x402/evm

Save this as client.mjs and run it with PAYER_KEY=0x... node client.mjs:

import { decodePaymentRequiredHeader, decodePaymentResponseHeader, encodePaymentSignatureHeader } from "@x402/core/http";
import { toClientEvmSigner } from "@x402/evm";
import { UptoEvmScheme } from "@x402/evm/upto/client";
import { createPublicClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";

const URL = "https://inference.usebrainchain.com/v1/chat/completions";
const account = privateKeyToAccount(process.env.PAYER_KEY);
const chain = createPublicClient({ transport: http("https://rpc.mainnet.chain.robinhood.com") });
const body = JSON.stringify({ model: "deepseek", messages: [{ role: "user", content: "Name one prime number above 100." }] });

// The first reply is the price: the most this call can cost, and where the payment goes.
const quote = await fetch(URL, { method: "POST", body });
const required = decodePaymentRequiredHeader(quote.headers.get("payment-required"));
const terms = required.accepts[0];
console.log(`ceiling ${Number(terms.amount) / 1e6} USDG`);

// Sign for the ceiling and send the same request again.
const payment = await new UptoEvmScheme(toClientEvmSigner(account, chain)).createPaymentPayload(2, terms);
const paid = await fetch(URL, { method: "POST", body, headers: { "payment-signature": encodePaymentSignatureHeader({ ...payment, accepted: terms, resource: required.resource }) } });
if (!paid.ok) throw new Error(`${paid.status} ${await paid.text()}`);

const answer = await paid.json();
const receipt = JSON.parse(Buffer.from(paid.headers.get("x-brainchain-receipt"), "base64").toString());
const settled = decodePaymentResponseHeader(paid.headers.get("payment-response"));
console.log(answer.choices[0].message.content);
console.log(`charged ${Number(receipt.price) / 1e6} USDG in ${settled.transaction}`);

The ceiling covers the longest answer the request allows. Send max_tokens to lower it.

What a request carries

A request is OpenAI's chat completions body with model, messages and, when wanted, max_tokens (or max_completion_tokens), temperature, top_p, stop, seed, presence_penalty, frequency_penalty and user. Messages are text.

Streaming, images and tool calls get a 400 before any payment is asked for. A body is at most 200 kB, and one request can cost at most 1 USDG.

The receipt

X-BrainChain-Receipt is base64 of a JSON object:

Field Meaning
v The receipt version, 1
request, response keccak256 of the request body as sent and of the answer body as returned
model, upstream The id asked for and the model that answered
tokensIn, tokensOut The tokens the model reported for the request and the answer
price, fee Everything charged, in millionths of a USDG, and the per-call amount inside it
absorbed, judged "0" and null
asset, network USDG's address and eip155:4663
payer, nonce The paying address and the Permit2 nonce of its authorization
at When the receipt was signed, UTC
signer, signature BrainChain's key for this service and its signature

BrainChain signs receipts with 0x70D882879E1954Ac9A06b693379334Fd131B786D. The signature is an EIP-191 message over keccak256 of the receipt's JSON without signature, with the keys in the order above.

Check a receipt

import { keccak256, recoverMessageAddress, stringToBytes } from "viem";

const SIGNER = "0x70D882879E1954Ac9A06b693379334Fd131B786D";
// `receipt` as parsed from the header, which keeps the signed key order.
const { signature, ...fields } = receipt;
const hash = keccak256(stringToBytes(JSON.stringify(fields)));
const signer = await recoverMessageAddress({ message: { raw: hash }, signature });
console.log(signer === SIGNER ? "signed by BrainChain" : "another signature");

To tie the receipt to the answer, hash the answer's bytes as received and compare with response.

GET /v1/receipts/<YYYY-MM-DD> returns one UTC day's receipts with their count and Merkle root. After a day closes, BrainChain writes that root to the chain as a record of kind receipts, with the day, the signing key and the count, so a receipt can be shown to belong to its day. The leaves are the receipts' hashes in the order listed. Each pair is hashed sorted, as OpenZeppelin's MerkleProof checks, and an unpaired hash moves up unchanged.

When a call is not charged

Reply Meaning
400 The request was turned away before payment. request_too_large means it could cost more than one request may: shorten it or lower max_tokens
404 model_not_found No model has that id
402 with an error The payment was not accepted, for example permit2_insufficient_balance. Nothing was charged
402 settlement_failed The payment did not settle, so the answer is not returned and nothing was charged
502 upstream_failed The model gave no answer. Nothing was charged