Management API

Everything the console does, from code. Model calls stay on the Model API. This API runs the account.

Who calls it

The base is https://app.opencheap.si/api/v1. Agents send their wallet session (kla_…). Backends send an agent key (kl_rs_…) made in the console under API keys › Agents. Model calls stay at https://api.opencheap.si/v1. The contract is one OpenAPI 3.1 document at /api/v1/openapi.json.

The shape

Inside v1 we only ever add: new endpoints, new optional fields. Ignore fields you do not know. Lists return { data, nextCursor }. Pass nextCursor back as cursor. Errors carry a stable code, a requestId and, when there is one, next: the call that fixes it.

Send an Idempotency-Key on creates so a retry never applies twice. Recording a customer payment requires one.

Limits: an account makes at most 60 changes a minute and 1,000 an hour, across the API, MCP and the console (429 rate_limited, wait the Retry-After). Each kind of record has a cap per account, such as 500 model keys or 10,000 customers (409 limit_reached, the message names it).

export OPENCHEAP=https://app.opencheap.si/api/v1
export KEY=kl_rs_…   # or an agent session, kla_…

curl -s $OPENCHEAP/whoami -H "Authorization: Bearer $KEY"
curl -s $OPENCHEAP/customers -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: user_8f2k1" -d '{ "name": "Northwind Labs", "externalRef": "user_8f2k1" }'

Budgets

A budget caps spend at our price on the whole account, or on one key: daily, weekly, monthly and lifetime limits (at least one), in UTC windows (the week starts Monday; lifetime counts from when that limit was set). hard_stop refuses the key's requests with a 402 at a limit; alert_only never blocks. Both send one email per limit and window when the spend crosses alertPct (80 by default, 0 for none). A key's monthly limit is its monthlyLimitUsd, the same cap POST /keys sets. The most a request can cost (its prompt, plus max_tokens of output, or the longest answer the model allows when it sets none) is checked against what is left before the request is sent, counting requests still running, so a key stops just before its limit; a request that a dearer provider finishes after a failure can end a little over. Free models are not stopped by a budget.

GET    /budgets                the account's budget and every own key's, each limit with its spend this window
GET    /budgets/account        the account's budget (404 without one)
PUT    /budgets/account        set or replace it
DELETE /budgets/account        204
GET    /keys/{id}/budget       one key's budget (404 without one)
PUT    /keys/{id}/budget       set or replace it; limits.monthly is the key's monthlyLimitUsd
DELETE /keys/{id}/budget       204; the key's monthly limit goes too
curl -s -X PUT $OPENCHEAP/keys/$KEY_ID/budget -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{ "limits": { "daily": 5, "monthly": 50 }, "alertPct": 80, "mode": "hard_stop" }'
# → { "scope": "key", "limits": { "daily": 5, "weekly": null, "monthly": 50, "lifetime": null }, "status": "ok",
#     "windows": [ { "window": "daily", "limitUsd": 5, "spentUsd": 1.27, "resetsAt": "2026-10-19T00:00:00Z", … }, … ] }

Instances

An instance is a machine for one model, sized like a share of an 8-GPU node: while it runs, the account's calls to that model cost nothing per token, within its spec. Billed per second from the balance, one minute at least.

GET   /instances/types       what can be started: machines per model (class, size, spec, limits, price)
POST  /instances             { model, gpuClass?, size?, hours?, instanceName? }: start (201; 402 insufficient_balance, 409 conflict)
GET   /instances             running ones first
GET   /instances/{id}        billed up to now
PATCH /instances/{id}        { hours }: change the timer (null: none)
POST  /instances/{id}/stop   stop it now
POST  /instances/dedicated-requests  { gpuClass, region, model, hoursPerMonth, note? }: ask for dedicated hardware

Gateways

A gateway is a named config a request runs through. A chat gateway routes the calls your code sends: fallbacks between models, retries, a response cache, a rate limit, a time limit and the models allowed. A decision gateway is your own classifier: it keeps a decision model and the questions, so a call sends only the text. The config is the object the console saves (snake-case fields, validated the same way); unknown fields are dropped and a bad one answers 400 naming the field.

A chat gateway's guardrails field turns on checks before the model is called: prompt injection, personal data and topics, each set to flag, block (a 446, never billed) or, for personal data, mask. The shape, the defaults and the 446 are on Guardrails.

GET    /gateways                                 list: name, kind, default model, counts
POST   /gateways                                 create from a config (201; 409 when the name is taken)
GET    /gateways/{name}                          the stored config and the keys it is the default of
PUT    /gateways/{name}                          replace the config (a different name renames it)
DELETE /gateways/{name}                          204; its keys go back to no gateway
GET    /gateways/{name}/stats?range=24h|7d       the console's counters: requests, p95, errors, each rule
GET    /gateways/{name}/decisions?range=24h|7d   a decision gateway's answers per question
PATCH  /keys/{id}  { "gateway": "<name>" | null }   a key's default gateway
PATCH  /keys/{id}  { "speed": { "sort": "throughput" } | null }   a key's default speed (a gateway's is "speed" in its config)
PATCH  /keys/{id}  { "priceFallover": false | true | null }   a key's price fall-over (null: the default, on; see /errors#fallover)
PATCH  /keys/{id}  { "minDiscount": 90 | null }   a key's minimum discount under the list price (a gateway's is "min_discount"; see /index-methodology#min-discount)
PATCH  /keys/{id}  { "ioRatio": 15 | "15:1" | null }   a key's input:output ratio (a gateway's is "io_ratio"; see /speed#io-ratio)
PATCH  /account  { "defaultGateway": "<name>" | null }   the account's default: every own key without one

A chat gateway with a fallback and a one-hour cache:

curl -s $OPENCHEAP/gateways -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: gw-support-bot" -d '{
    "name": "support-bot",
    "default_model": "claude-sonnet-5",
    "strategy": { "mode": "fallback" },
    "targets": [{ "model": "claude-sonnet-5" }, { "model": "kimi-k3" }],
    "cache": { "mode": "simple", "max_age_s": 3600 }
  }'

A decision gateway with three questions. Its default_model must be a decision model (kind: "decisions" in GET /api/v1/models); a question is a choice with 2 to 64 options, a score (optionally with levels, lowest first) or noul, yes or no.

curl -s $OPENCHEAP/gateways -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -d '{
    "name": "support-triage",
    "kind": "decision",
    "default_model": "pplx-decider-v1.2",
    "instructions": "Support messages; the last message is the one to classify.",
    "questions": {
      "frustrated": { "type": "noul", "instructions": "Is the customer frustrated?" },
      "topic":      { "type": "choice", "options": ["refund", "bug", "question"] },
      "urgency":    { "type": "score", "options": ["can wait", "this week", "today"] }
    }
  }'

Attach a gateway to a key as its default, or name it per request with the x-klinn-gateway header. A key with a gateway of its own is locked to it, and so is a key under an account default gateway with guardrails: a header naming another gateway is a 403 gateway_locked. Otherwise the header wins. Deleting a gateway sends its keys back to no gateway. Every change reaches requests within about a minute, a replaced config, a rename, a delete or a key's new default alike: the router keeps a key's gateway and the gateway list for about 60 seconds, so until then a request may still run through the old config.

curl -s -X PATCH $OPENCHEAP/keys/$KEY_ID -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -d '{ "gateway": "support-bot" }'          # { "gateway": null } clears it

curl https://api.opencheap.si/v1/chat/completions -H "Authorization: Bearer $OPENCHEAP_API_KEY" \
  -H "x-klinn-gateway: support-bot" -H "Content-Type: application/json" \
  -d '{ "model": "default", "messages": [{ "role": "user", "content": "Hello" }] }'

curl -s "$OPENCHEAP/gateways/support-bot/stats?range=7d" -H "Authorization: Bearer $KEY"
curl -s "$OPENCHEAP/gateways/support-triage/decisions?range=24h" -H "Authorization: Bearer $KEY"

Delegates

Agent keys and wallets an owner added are delegates. They can do everything except add wallets or create more agent keys.