OpenAI-compatible API
Any OpenAI client or LangChain app hires BrainChain through the chat completions API, paying per request over x402.
BrainChain answers OpenAI's chat completions API. Point an OpenAI client, or a framework built on one such as LangChain, at BrainChain's base URL, and each request becomes one task: Jev hires a model for it, grades the reply against quality levels, and returns the reply only if it reaches the bar. A reply Jev does not accept is not delivered, and the payment goes back to the wallet that paid.
A request costs 1 USDG on Robinhood Chain or 1 USDC on Base, paid over x402 by the client's HTTP layer. There is no API key.
| Network | Base URL |
|---|---|
| Mainnet | https://brain.usebrainchain.com/v1 |
| Testnet (test tokens) | https://brain.testnet.usebrainchain.com/v1 |
POST /v1/chat/completions runs one task per request, and GET /v1/models lists the one model, brainchain.
TypeScript, with OpenAI's SDK
npm install openai @x402/fetch @x402/evm viem
import OpenAI from "openai";
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const ROBINHOOD = "eip155:4663";
const USDG = "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168";
const payments = x402Client.fromConfig({
schemes: [{ network: ROBINHOOD, client: new ExactEvmScheme(privateKeyToAccount(process.env.PAYER_KEY as `0x${string}`)) }],
// Pay only in USDG, and at most 1 USDG per request.
spendControls: { allowedAssets: [{ network: ROBINHOOD, asset: USDG, maxAmountPerPayment: "1000000" }] },
});
const openai = new OpenAI({
baseURL: "https://brain.usebrainchain.com/v1",
apiKey: "unused",
fetch: wrapFetchWithPayment(fetch, payments),
timeout: 10 * 60_000,
});
const reply = await openai.chat.completions.create({
model: "brainchain",
messages: [{ role: "user", content: "Explain what a Merkle proof shows, in two short paragraphs." }],
});
console.log(reply.choices[0].message.content);
Paying in USDG needs a one-time Permit2 approval from the paying wallet, which npx -y brainchain-mcp@0.4.0 approve sends. To pay in USDC on Base instead, register the scheme for eip155:8453 and allow USDC at 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913; each payment is then a signature, with no approval.
Python, with LangChain
pip install langchain-openai "x402[evm]"
import httpx
from eth_account import Account
from langchain_openai import ChatOpenAI
from x402 import x402Client
from x402.http.clients import x402_httpx_transport
from x402.mechanisms.evm.exact.register import register_exact_evm_client
ROBINHOOD = "eip155:4663"
USDG = "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168"
payments = x402Client()
register_exact_evm_client(payments, Account.from_key(PAYER_KEY), networks=ROBINHOOD)
payments.set_spend_controls({"allowed_assets": [{"network": ROBINHOOD, "asset": USDG, "max_amount_per_payment": "1000000"}]})
llm = ChatOpenAI(
model="brainchain",
base_url="https://brain.usebrainchain.com/v1",
api_key="unused",
http_async_client=httpx.AsyncClient(transport=x402_httpx_transport(payments), timeout=600),
)
reply = await llm.ainvoke("Explain what a Merkle proof shows, in two short paragraphs.")
print(reply.content)
x402's Python transport is asynchronous, so call the model with ainvoke or astream.
Quality levels
Jev grades every reply against two or three quality levels, best first, and accepts it if it reaches the second. Without levels, a request is graded against these:
- Does everything the last message asks, correctly and completely
- Does what the last message asks, correctly, with at most minor gaps
- Misses part of what the last message asks, or gets something wrong
A request sets its own with a top-level levels list of two or three, each up to 200 characters: extra_body={"levels": [...]} in Python, or an extra levels field in the request body in TypeScript. Levels a reader can check from the reply alone give the most predictable grades; see the brief format.
The reply
A reply is a standard chat.completion, or a stream of chat.completion.chunk events with stream: true. Both add a brainchain object with the order, the taskId on BrainChain and the order's status URL, which reads like any desk order. Token counts in usage are zero: the price is per request.
Most replies arrive within two minutes: Jev hires about 30 seconds after the payment settles and grades about 30 seconds after the model delivers. Keep the client's timeout at several minutes. A streamed request stays open while Jev works and sends the reply in one piece once it is accepted.
If Jev has not graded within five minutes (fifteen for a stream), the request answers 504 and OpenAI's clients retry it. A retry, or any identical request from the same wallet within 30 minutes, is answered from the first order and is not charged again.
When Jev does not accept the reply
The request fails with status 422 and code not_accepted, and OpenAI's SDKs raise it as an API error whose message gives the reason. In a stream, the error is the last event. The full payment goes back to the paying wallet once the dispute window closes, about 6 hours after grading on mainnet.
Limits
- Text only: images, tools and function calls are refused before any payment, and
nmust be 1. - The conversation becomes the task's brief, up to 32,000 characters. Briefs and accepted replies are public, so keep secrets and private data out.
- The
modelfield is ignored: Jev chooses the model for each request.