KWOTA

API docs

KWOTA exposes an OpenAI-compatible API. Point any OpenAI SDK at the KWOTA base URL and use your KWOTA API key.

Quick start

1. Open the app, sign in with your wallet, and create an API key. 2. Call the API:

curl https://<your-domain>/api/v1/chat/completions \
  -H "Authorization: Bearer kw_live_…" \
  -H "Content-Type: application/json" \
  -d '{"model":"openai/gpt-4o-mini","messages":[{"role":"user","content":"Hello"}]}'
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://<your-domain>/api/v1", apiKey: process.env.KWOTA_API_KEY });
const r = await client.chat.completions.create({ model: "openai/gpt-4o-mini", messages: [{ role: "user", content: "Hi" }], stream: true });
for await (const chunk of r) process.stdout.write(chunk.choices[0]?.delta?.content ?? "");

Endpoints: GET /v1/models, POST /v1/chat/completions (with stream: true support).

Metering

  • Cost = input tokens × input price + output tokens × output price (kwota per 1M tokens, listed on the transparency page). 1 kwota = $1.
  • Before a request runs, the maximum possible cost is reserved; after it finishes you pay only for tokens actually used and the rest is released.
  • Non-streaming responses include kwota.cost_micro and the headers x-kwota-cost-micro / x-kwota-balance-micro (µkwota = 10⁻⁶ kwota).
  • Streaming responses end with an SSE comment line : kwota {…cost_micro…} that OpenAI parsers ignore.
  • Limits: max cost per request, max output tokens, and requests per minute per key and per wallet (see transparency page).

Agents — EIP-712 signed requests

Register your ERC-8004 agent in the app and allocate kwota to it. The agent then signs each request with its agent wallet. The signature binds the exact request body, a unique nonce and a short expiry (≤ 5 minutes), so requests cannot be replayed or altered.

import { keccak256, toHex } from "viem";
import { privateKeyToAccount } from "viem/accounts";

const agent = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY);
const REGISTRY = "0x…";          // ERC-8004 identity registry
const AGENT_ID = 12n;            // your agent's id in that registry
const body = JSON.stringify({ model: "openai/gpt-4o-mini", messages: [{ role: "user", content: "Summarize…" }] });

const nonce = BigInt(Date.now()) * 1000n + BigInt(Math.floor(Math.random() * 1000));
const expiry = BigInt(Math.floor(Date.now() / 1000) + 120);
const signature = await agent.signTypedData({
  domain: { name: "KWOTA Gateway", version: "1", chainId: 46630, verifyingContract: REGISTRY },
  types: { KwotaRequest: [
    { name: "agentId", type: "uint256" }, { name: "bodyHash", type: "bytes32" },
    { name: "nonce", type: "uint256" }, { name: "expiry", type: "uint64" },
  ] },
  primaryType: "KwotaRequest",
  message: { agentId: AGENT_ID, bodyHash: keccak256(toHex(body)), nonce, expiry },
});

await fetch("https://<your-domain>/api/v1/chat/completions", {
  method: "POST",
  headers: {
    "content-type": "application/json",
    "x-kwota-agent": AGENT_ID.toString(),
    "x-kwota-registry": REGISTRY,
    "x-kwota-nonce": nonce.toString(),
    "x-kwota-expiry": expiry.toString(),
    "x-kwota-signature": signature,
  },
  body,   // send exactly the bytes you hashed
});

The signer must be the agent wallet recorded in the registry (or the owner if none is set). Smart-contract wallets (ERC-1271) are supported.

Agent API keys

Alternatively create an API key bound to an agent (Agents → “New agent key”). It can only spend that agent’s allocation and respects its daily limit.

Errors

401 invalid_api_key / bad_signature / replay / signature_expiredAuthentication failed
402 insufficient_kwotaNot enough available kwota for the reserved maximum cost
403 agent_inactiveAgent stopped or frozen by its owner
429 rate_limited / daily_limit_exceededPer-minute limit or agent daily cap reached
400 max_cost_exceededPrompt too large for the per-request cost cap
502 provider_errorUpstream model error — nothing is charged

Verify an epoch yourself

Each epoch publishes a JSON snapshot, its SHA-256 and a Merkle root. The verifier re-derives every allocation from on-chain Transfer events:

git clone <kwota repo> && cd kwota && pnpm install
pnpm --filter @kwota/api verify-epoch -- --api https://<your-domain>/api --epoch 12 --rpc <robinhood-chain-rpc> --from-block <token deploy block>

Leaf encoding (OpenZeppelin StandardMerkleTree): (uint256 epochId, address holder, uint256 twab, uint256 kwotaMicro). Proofs: GET /public/epochs/:id/proof/:address.