SPOT TAPE
FRNT-200K2.180+1.42%
MID-128K0.4204−0.81%
OW-70B0.0840+3.11%
OW-8B0.0191+0.44%
VIS-1M1.6400−2.19%
RSN-XL6.9000+0.93%
EMB-S0.01100.00%
14:02:07 UTC
Documentation v1 · UPDATED 21 AUG 2026
Get an API key
On this page

Buyer flow

Submit a max_price bid. The gateway matches an ask, dispatches to the winning seller, and returns a standard OpenAI chat.completion object. Consume it the same way you consume OpenAI today.

Discovery for the gateway base URL:

  1. Pass an explicit base_url / baseUrl to the client constructor, or
  2. Call from_config / fromConfig with a config origin (GET {config_url}/configgatewayApiUrl), or
  3. Error — there is no hardcoded fallback URL.

Set GATEWAY_URL (explicit constructor) or GATEWAY_CONFIG_URL (discovery) in the environment.

See POST /v1/chat/completions for the wire contract.

Request

fieldtypenotes
modelstringInstrument / model id
messages{role, content}[]OpenAI chat turns
max_pricedecimal stringMax price per 1,000 tokens. Passed through verbatim — the SDK does not parse or rescale it.
max_tokensnumberOptional generation cap
temperaturenumberOptional sampling
streamboolSSE when true

Response

OpenAI chat.completion object.

fieldtypenotes
idstringCompletion identifier
objectstringTypically "chat.completion"
createdintegerUnix timestamp (seconds)
modelstringModel that produced the completion
choices[]arrayGenerated choices
usageobject, optionalToken accounting when the seller reports it

Each choice:

fieldtypenotes
indexintegerZero-based index among choices
message{role, content}Assistant message for this choice
finish_reasonstring, optionalWhy generation stopped (for example "stop")

Usage, when present: prompt_tokens, completion_tokens, total_tokens.

Errors

codeHTTPmeaning
no_match / bid_timeout408No ask crossed before the bid wait expired
rate_limited429Buyer key exceeded burst
insufficient_balance402Ledger rejected the trade
seller_unreachable502Winning seller timed out or 5xx

Retry patterns live in Error handling.

OpenAI compatibility

The response is the standard OpenAI chat.completion object. The only client change is base URL + max_price.

from openai import OpenAI

openai = OpenAI()
completion = openai.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "hello"}],
)
print(completion.choices[0].message.content)

spot = OpenAI(base_url=f"{GATEWAY_URL}/v1")
completion = spot.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "hello"}],
    extra_body={"max_price": "0.05"},
)
print(completion.choices[0].message.content)
import OpenAI from "openai";

const openai = new OpenAI();
const completion = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: "hello" }],
});
console.log(completion.choices[0].message.content);

const spot = new OpenAI({ baseURL: `${process.env.GATEWAY_URL}/v1` });
const bid = await spot.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: "hello" }],
  max_price: "0.05",
});
console.log(bid.choices[0].message.content);

Full-loop example

Submit a bid, receive an OpenAI-shaped completion, consume .choices[0].message.content.

import os

from token_gateway import BuyerAuth, BuyerTokenGateway


def main() -> None:
    gw = BuyerTokenGateway.from_config(
        os.environ["GATEWAY_CONFIG_URL"],
        buyer=BuyerAuth(os.environ["BUYER_API_KEY"]),
    )
    completion = gw.chat_completions(
        model="gpt-4",
        messages=[{"role": "user", "content": "hello from python"}],
        max_price="0.05",
        max_tokens=64,
    )
    print(completion.id)
    print(completion.model)
    print(completion.choices[0].message.role)
    print(completion.choices[0].finish_reason)
    use_completion(completion.choices[0].message.content)


def use_completion(text: str) -> None:
    print(text)
import OpenAI from "openai";

const gatewayUrl = process.env.GATEWAY_URL;
if (!gatewayUrl) {
  throw new Error("GATEWAY_URL must be set");
}
const apiKey = process.env.BUYER_API_KEY;
if (!apiKey) {
  throw new Error("BUYER_API_KEY must be set");
}

const openai = new OpenAI({
  baseURL: `${gatewayUrl}/v1`,
  apiKey,
});

const completion = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [{ role: "user", content: "hello from javascript" }],
  max_price: "0.05",
  max_tokens: 64,
});

const { id, model } = completion;
const { content } = completion.choices[0].message;
const { finish_reason } = completion.choices[0];
console.log(id);
console.log(model);
console.log(finish_reason);
useCompletion(content);

function useCompletion(text) {
  console.log(text);
}

Run it locally

Install

pip install token-gateway
npm install @ai-token-gateway/sdk

Environment

variableused by
GATEWAY_CONFIG_URLPython discovery (GET /config)
BUYER_API_KEYBuyer bearer key for both languages
GATEWAY_URLJavaScript OpenAI client baseURL

Run

GATEWAY_CONFIG_URL=http://127.0.0.1:8080 BUYER_API_KEY=sk-buyer python buyer_quickstart.py
GATEWAY_URL=http://127.0.0.1:8080 BUYER_API_KEY=sk-buyer node buyer_quickstart.js

Expected output is the completion text from choices[0].message.content.

Next steps

Wire contract: POST /v1/chat/completions.