skip to content
2026#typescript#gateway#llm

kPool

Gives each tool its own virtual key and budget, and spreads its requests across the LLM subscriptions, API keys, and model servers you already have.

The problem

Once more than one tool talks to more than one LLM provider, key management degenerates into a spreadsheet: which key belongs to which tool, which account is near its quota, who spent what. kPool puts one gateway in front of subscription accounts for Anthropic, OpenAI (including the Codex WebSocket transport), xAI, and Muse Code, pay-as-you-go API keys, and self-hosted vLLM and Strata servers. Each tool gets its own virtual key, and every request it makes lands in one ledger with its tokens and cost.

Architecture

kPool is a Bun workspace (server, shared contracts, and a React admin dashboard) that compiles into a single executable per target platform with the dashboard embedded. It doesn't bundle a database: it needs an external PostgreSQL and a persistent encryption key of at least 32 random bytes, and it won't start without either. Losing the key makes every stored secret unreadable, provider credentials included, so back it up separately from the database.

It speaks Anthropic Messages, OpenAI Responses and Chat Completions, and an Ollama-compatible facade. OpenAI and Anthropic Platform keys and self-hosted servers sit behind a separate OpenCode endpoint with its own model discovery, so a Codex or Claude Code request never spends an API key. kPool used to translate one provider's protocol into another's and no longer does: apart from the Ollama facade, each route serves only providers that speak its protocol natively, and a clearly provider-owned model sent to the wrong endpoint is rejected instead of silently forwarded.

Virtual keys, users, and groups each carry model allow and deny lists, RPM/TPM limits (requests and tokens allowed per minute) (optionally per provider), and spending caps in total and per week; a key can also cap its request count. All of it is checked before the provider call, and in-memory pre-call reservations (estimated cost held against a budget while the request runs) let concurrent requests see each other's pending spend. Usage lands in a ledger with token counts and costs (exportable as CSV), and admin actions and config changes each have their own audit log.

previous_response_id, session IDs, and Codex turn state bind follow-up turns to the account that started the conversation. kPool rotates to another account only on a retryable failure and only before the response is committed, never mid-conversation; swapping accounts halfway through is how an agent loses the thread (it already forgets enough). Selection weighs backoff state, quota-pressure bands, and in-flight load. Optional reserve thresholds keep the last slice of an account's 5-hour or weekly allowance for keys allowed to drain it (OpenAI and xAI apply only the weekly reserve). An API key that runs out of credits is held for an hour while requests move to the next one; once every eligible key is held, subsequent requests get 429 out_of_credits without retrying them. Admission control (a gate that caps work entering the system) caps active inference requests, open streams included, at 256 by default, and excess requests fail immediately with 503 instead of waiting in a queue.

For Codex WebSockets, the gateway proxies the upgrade, injects upstream credentials into the handshake, and checks every keyed frame for model access, budget, and rate limits before forwarding it. Codex token_count frames feed the same ledger as HTTP traffic. Binary or unparseable keyed frames fail closed.

Boundaries

kPool only routes the accounts and keys you already have; it adds no quota of its own. Client tokens are stripped before forwarding and upstream credentials never reach clients; discovered local credentials join the routing pool only after an explicit, audited import. Ledger costs are estimates from pinned LiteLLM token rates and leave out subscription fees. Whether a provider's terms allow routing a subscription through a gateway is yours to check.

Run one kPool process per Postgres database: each process keeps its own in-memory copy of keys and sessions, so a second one misses the first's revocations and overwrites its ledger and broker state. Optional TOTP two-factor auth is built in. SSO, deployment packaging, SDK releases, and published binaries are out of scope for now, and kPool doesn't launch or manage Codex, Claude Code, or Grok CLIs as child processes.