ktms

module
v1.6.0-beta.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0

README

Keystone. The Model Switchboard. (KTMS)

CI Buf Go Report Card Release Docker Image

A self-hosted LLM gateway and model switchboard. Your services talk to one endpoint; Keystone holds the vendor API keys, decides which model answers each request, falls back when a vendor is down, enforces rate and spend limits, and records usage.

  • One place for vendor keys. OpenAI, Anthropic (Claude), Google Gemini, DeepSeek and TypeSafe credentials live encrypted in Postgres (AES-256-GCM). Client services never see them.
  • Routing by rules, not code. Clients send an effort (LOW / MEDIUM / HIGH) and free-form attributes; an ordered rule list maps them to a model chain with fallbacks. Rules are changed at runtime over an admin RPC, with no redeploy.
  • Throttling. Per-model requests-per-minute and daily token budgets, per API key and per end user, backed by Redis.
  • More than text. Text generation with structured (JSON-schema) output, asynchronous batch generation at 50% discount, speech synthesis, image generation and typed judgments.
  • OpenAI-compatible HTTP API. Point any OpenAI SDK or tool at http://host:8080/v1 and get routing, fallbacks and limits for free.
  • Usage reporting. Optionally forwards one event per call to a gRPC usage sink.
  • Small footprint. Single Go binary: ~67 MB image, ~7 MiB idle RAM, under 1 ms of added latency.
  • Web console. ktms-admin manages models, rules and keys in the browser.

Contents

How it works

 your services ──gRPC (Bearer ktms_…)──▶ KTMS ──▶ OpenAI / Anthropic / Gemini / DeepSeek / TypeSafe
                                          │  │
                                 Postgres ┘  └ Redis (limits)      └─▶ usage sink (optional)
                                 config, keys, encrypted vendor keys

Two gRPC services share port 9090:

Service Purpose Needed scope
ktms.v1.GatewayService GenerateText, GenerateTextStream, SubmitBatch, GetBatch, ListBatchResults, CancelBatch, SynthesizeSpeech, GenerateImage, Judge, JudgeStream, EvaluateRollouts, GetEmbeddings, ListModels generate
ktms.v1.AdminService models, rules, vendor keys, API keys, key rotation, ExplainRoute admin; admin:read for the Get* and List* reads and ExplainRoute; agents:read for the agents' cards and conversations

Port 8080 serves the health probes (/healthz, /readyz) and the OpenAI-compatible HTTP API under /v1. gRPC server reflection is on, so grpcurl works without the .proto files.

Quick start (Docker Compose)

The fastest way to try Keystone is one command. It needs Docker with the Compose plugin, runs the published images (no clone, no build) with Postgres and Redis on localhost only, asks for an OpenAI, Anthropic or Gemini key unless one is in the environment, and prints your first request:

curl -fsSL https://ai-process.dev/install.sh | sh

It keeps its files in ~/ktms (a generated keyring, admin and client keys in .env), and a re-run keeps them while it adds new vendor keys or moves to a new release. Options such as --port, --version and --uninstall are in sh -s -- --help; the script is scripts/install.sh.

From a clone instead, Docker Compose builds the image from source. With AUTO_SEED enabled (default in compose), the proxy automatically initializes default models, routing rules, and a ready-to-use client key (ktms_dev_default_key).

1. Launch the stack
git clone https://github.com/ai-process/ktms.git
cd ktms

# Pass your vendor API key (OpenAI, Gemini, or Anthropic)
OPENAI_API_KEY=sk-... docker compose up -d --build

# Verify readiness
curl -s http://localhost:8080/readyz     # {"status":"ready"}

Alternatively, run ./scripts/quickstart.sh for an interactive setup that generates master encryption and bootstrap keys, configures .env, and launches the stack.

[!NOTE] With AUTO_SEED=true (enabled by default in Compose), the proxy automatically syncs vendor API keys from environment variables (OPENAI_API_KEY, GEMINI_API_KEY, ANTHROPIC_API_KEY) to the client key named dev-default (or $SEED_CLIENT_NAME) on every startup.

2. Make your first call

You can immediately query the proxy using the OpenAI-compatible HTTP endpoint:

curl -s http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer ktms_dev_default_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"auto","messages":[{"role":"user","content":"Say hi in three words."}]}'

Or using the official OpenAI Python SDK:

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key="ktms_dev_default_key")
response = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Say hi in three words."}],
)
print(response.choices[0].message.content)

The response includes canonical observability headers (X-KTMS-Vendor, X-KTMS-Rule, X-KTMS-Placement) along with backwards-compatible X-LLM-Proxy-* headers.

With an AI assistant

In Claude Code, the plugin deploys and configures the stack for you: install it, run /ktms:assess to see where Keystone fits your system and what to do first, /ktms:deploy for Compose or Helm with Keystone, light-tokenmeter and ktms-admin, and /ktms:integrate to connect a service.

claude plugin marketplace add ai-process/ktms && claude plugin install ktms@ai-process

For other assistants, Prompts for AI assistants covers the same tasks, starting with Deploy the stack.


Setting it up from scratch with ktms init

To start without auto-seeding, set AUTO_SEED=false and let the ktms command ask for the rest:

ktms init                       # a context named local; ktms init prod for another one

It asks for the Admin API address and the admin key (BOOTSTRAP_ADMIN_KEY, dev-bootstrap in Compose), keeps the key in the keychain or a 0600 file under a context it makes current, then for the vendor keys (OpenAI, Gemini, Anthropic; OPENAI_API_KEY and the like are offered when set) and a client key name. It writes ktms.yaml with the default models and rules of those vendors, shows the plan and applies it:

+ model gpt-4o-mini
+ api key my-app
+ vendor key my-app/openai
~ rules (4 rules, replaced together)
Plan: 5 to create, 1 to change, 0 to delete.
Apply these changes? [y/N] y

New client keys. They are shown only now; store them in your secret manager:
  my-app: ktms_…

The vendor keys go to Keystone and stay out of the file; the client key is kept in the context too, so a test call needs nothing more. From then on the file is the configuration:

ktms chat "Say hi"              # the answer, then rule, model and vendor
ktms config plan -f ktms.yaml   # after editing it
ktms config apply -f ktms.yaml

On a Keystone that has models already, ktms init writes them to ktms.yaml instead. The same steps one by one are ktms context set, ktms keys mint, ktms vendor-keys set and ktms config apply. Once real keys exist in production, unset BOOTSTRAP_ADMIN_KEY.

Resource footprint

KTMS is a single Go process with no interpreter. It adds well under a millisecond per request and needs tens of megabytes where Python gateways such as LiteLLM Proxy need gigabytes.

Metric KTMS (Go) LiteLLM Proxy (Python, 4 workers) Difference
Docker image, linux/amd64 (uncompressed / pull) ~67 MB / ~21 MB (slim) ~1.3 GB / ~420 MB ~20x smaller
Idle memory (RSS) ~7 MiB ~2.5 GiB ~370x less
Memory at a steady 200 RPS ~20 MiB, no drift ~2.9 GiB ~145x less
Peak memory over the whole run (up to 4,000 RPS) < 50 MiB ~3.0 GiB ~60x less
Added latency, p50 / p99 ~0.6 ms / ~1.1 ms ~8.5 ms / 27–229 ms ~13x lower p50
Added time to first token (streaming), p50 ~0.7 ms ~160 ms no chunk buffering in either
Max sustained throughput (< 1% errors) ≥ 4,000 RPS (highest step tested) ~250 RPS ≥ 16x higher
CPU time per 1,000 requests ~0.63 s ~9.7 s ~15x less
Time to readiness (container start → first proxied answer) ~170 ms ~32 s ~190x faster

The same stand also measured Bifrost, Portkey, Kong AI Gateway, Apache APISIX, Higress, Envoy AI Gateway, agentgateway, New API and Plano. KTMS is in the same class as the compiled and Nginx/Envoy-based gateways: all of them add under 1.3 ms at p50 and sustain 4,000 RPS. Among them, only agentgateway matches KTMS's memory footprint, while agentgateway, APISIX and Kong add less latency (0.25–0.35 ms) and use less CPU per request. The Node.js and Python gateways (Portkey, LiteLLM) saturate at 500 and 250 RPS.

Measured in October 2026 on Google Cloud: each gateway alone on a dedicated c3-standard-4 (4 vCPU, 16 GB, x86_64), k6 on a second VM, and a mock OpenAI upstream (0 ms for latency, 10 ms for throughput). LiteLLM v1.106.0 ran with master key only and no database. Memory is the gateway container's cgroup RSS; Postgres and Redis are not counted. Two independent runs agreed within ~10%, except LiteLLM p99. The stand, every gateway config and the run scripts are in bench/.

Installation

Docker image

Release images are published to Docker Hub for linux/amd64 and linux/arm64 in two variants:

  • coyl/ktms:latest (also tagged :slim or pinned :<version> / :<version>-slim): default lightweight image (~67 MB uncompressed, ~21 MB download). A clean Alpine container without ffmpeg, optimized for text generation, chat, reasoning, embeddings, and OpenAI TTS.
  • coyl/ktms:audio (or pinned :<version>-audio): extended image (~195 MB uncompressed, ~69 MB download) with ffmpeg installed for Gemini speech synthesis (transcoding raw PCM into OGG/Opus voice messages).
docker pull coyl/ktms:latest      # default lightweight image (~67 MB)
docker pull coyl/ktms:audio       # with ffmpeg for Gemini speech synthesis (~195 MB)

X.Y (and X.Y-slim, X.Y-audio) follows the newest release of its line; latest, slim and audio follow the newest release of all. The changelog lists what each release changed.

If Gemini TTS is requested without ffmpeg available (e.g. on the default slim image), the proxy returns gRPC Codes.Unimplemented / HTTP 501 Not Implemented: ffmpeg_missing: ffmpeg is required for Gemini speech synthesis: install ffmpeg or use the 'coyl/ktms:audio' container image. OpenAI TTS produces native Opus directly and works on both variants without ffmpeg.

The image runs as a non-root user (appuser, UID 10001), exposes 8080 (HTTP) and 9090 (gRPC), and is configured entirely through environment variables. From 1.0.0, images are signed with cosign and carry an SBOM and provenance, and the release's checksums.txt is signed: see Verifying a Release.

You need a Postgres database (required; 16 is what the compose file uses) and a Redis (optional, see below; 6.2 or newer for the local node pool's usage log):

# generate the at-rest encryption keyring once and keep it safe
export KTMS_ENCRYPTION_KEYS="1:$(head -c 32 /dev/urandom | base64)"
export BOOTSTRAP_ADMIN_KEY="$(head -c 24 /dev/urandom | base64 | tr -d '/+=')"

docker run -d --name ktms \
  -p 9090:9090 -p 8080:8080 \
  -e DATABASE_URL='postgres://user:pass@db-host:5432/ktms?sslmode=require' \
  -e KTMS_ENCRYPTION_KEYS \
  -e BOOTSTRAP_ADMIN_KEY \
  -e REDIS_ADDR=redis-host:6379 \
  coyl/ktms:latest

A minimal Compose service for your own stack:

services:
  ktms:
    image: coyl/ktms:latest
    restart: unless-stopped
    environment:
      DATABASE_URL: postgres://ktms:${DB_PASSWORD}@db:5432/ktms?sslmode=disable
      KTMS_ENCRYPTION_KEYS: ${KTMS_ENCRYPTION_KEYS}
      REDIS_ADDR: redis:6379
    ports:
      - "9090:9090"
    depends_on: [db, redis]

On Kubernetes, point the liveness probe at GET :8080/healthz and the readiness probe at GET :8080/readyz (checks the database). Run as many replicas as you like; they share Postgres and Redis and pick up configuration changes within ROUTING_RELOAD_SECONDS.

Helm chart (Kubernetes)

An official Helm v3 chart is published with every release at oci://ghcr.io/ai-process/charts/ktms (source in deploy/helm/ktms). The chart version is the release version, and it runs the image of the same version:

# 1. Create a secret with your database URL and encryption keys
kubectl create secret generic ktms-secrets \
  --from-literal=DATABASE_URL='postgres://ktms:pass@db:5432/ktms?sslmode=require' \
  --from-literal=KTMS_ENCRYPTION_KEYS="1:$(head -c 32 /dev/urandom | base64)" \
  --from-literal=BOOTSTRAP_ADMIN_KEY="$(head -c 24 /dev/urandom | base64 | tr -d '/+=')"

# 2. Install the release
helm install ktms oci://ghcr.io/ai-process/charts/ktms --version 1.5.0 \
  --set secrets.existingSecret=ktms-secrets \
  --set ingress.enabled=true \
  --set "ingress.hosts[0].host=proxy.example.com" \
  --set "ingress.hosts[0].paths[0].path=/" \
  --set "ingress.hosts[0].paths[0].pathType=Prefix"

The chart supports non-root security context (appuser:10001), HorizontalPodAutoscaler, Prometheus Operator ServiceMonitor, and separate gRPC Ingress configuration. See deploy/helm/ktms/README.md for full configuration parameters.

Building from source

Requires Go 1.26+ (and ffmpeg on PATH if you use speech).

make build                       # → bin/ktms-server and bin/ktms-node
export DATABASE_URL=postgres://ktms:ktms@localhost:5432/ktms?sslmode=disable
export KTMS_ENCRYPTION_KEYS="1:$(head -c 32 /dev/urandom | base64)"
./bin/ktms-server

Or build the image yourself: docker build -t ktms .

Node agent

bin/ktms-node runs next to a local inference engine (ollama, vllm, tgi, llamacpp, freetoken), registers it with Keystone and sends heartbeats with the loaded models, GPU metrics and the engine's load: running and waiting requests from the /metrics of vLLM, TGI and llama.cpp (start llama-server with --metrics), and from FreeToken's /v1/stats. Ollama has no such API, so its nodes report no queue. It authenticates with a node key minted with the MintNodeKey admin RPC. KTMS dials the node, so the node must be reachable; the agent can do that itself:

export NODE_KEY=...                       # or --node-key-file
./bin/ktms-node --proxy-addr proxy.example.com:443 --tls \
  --engine ollama --expose=cloudflared    # needs cloudflared 2026.10.3 or newer

With --expose=cloudflared the agent starts a localhost gateway in front of the engine and a Cloudflare quick tunnel in front of the gateway, registers the tunnel address, and registers again whenever the tunnel restarts and its address changes. The gateway checks a per-process access key (sent to the proxy as the node auth token, never printed) and forwards only POST /v1/chat/completions and GET /v1/models; pulling or deleting models is not reachable. Slow responses are kept alive with padding so the tunnel's 100 s idle cutoff does not drop them. Quick tunnels have no SLA; for a stable address use --expose=none and your own --endpoint. cloudflared older than 2026.10.3 trusts the eMudhra intermediates whose keys leaked in October 2026: the agent warns about it at start and ktms-node doctor reports it; re-running install-node.sh updates it.

Flags: --proxy-addr (default 127.0.0.1:50051), --tls (dial over TLS with the system CA roots, for a proxy behind a TLS load balancer; off by default), --node-id (defaults to the hostname), --node-name, --engine (ollama, vllm, tgi, llamacpp, freetoken, decisions, generic), --base-weight, --max-concurrent. --judge-model (repeat or comma-separate) tags specific models on the node with the judge capability for local JEPA / decision evaluation, routing /v1/decisions and Judge calls to them. --only-model (repeat it or comma-separate) reports only the named models to the proxy, for an engine that shares its model directory with other tools; by default every model the engine has is reported. If the node key restricts allowed_models, the proxy ignores (and logs) other reported models and registers the node with the allowed ones; it refuses only when none is allowed. --engine-url is where the agent itself polls the engine (default http://127.0.0.1:11434); --endpoint is the address reported to the proxy, which dials it (default: --engine-url). --expose is none (default; you supply --endpoint) or cloudflared, with --cloudflared-path to pick the binary. NODE_AUTH_TOKEN is the bearer the engine itself requires, if any.

The agent keeps a usage log for the machine's owner in --stats-dir (default ~/.ktms-node/stats, or $KTMS_HOME/stats): every minute it pulls the requests the proxy sent here with PullNodeUsage (time, model, tokens, outcome, level; no user, key or text) into requests-YYYY-MM-DD.jsonl, and writes its own sample (schedule level, state, proxy and engine load, GPU) to samples-YYYY-MM-DD.jsonl. Closed days are gzipped and kept for --stats-retention (default 90d, 0 = forever); --stats=false turns it off. Against a proxy without the log it keeps only the samples. doctor checks that the log moves.

The owner reads it with the same binary, offline and without the node key:

ktms-node stats                      # last 24 hours by hour
ktms-node stats --since 7d           # by day
ktms-node stats --since 2026-10-01 --by model --format csv
ktms-node stats --tail               # requests as they arrive

Columns: requests, errors (the node failed or timed out; refused and abandoned requests are not counted), tokens in and out, busy (the share of time at least one request ran; parallel requests count once), cooldown given, paused (the share of minutes at 0%), minutes without data (the agent was not running), TTFT p50/p95 and output tokens per second. Stretches the proxy no longer had when the agent pulled are listed as missing. --format json|csv, --until, and --dir for a log elsewhere.

If a node does not show up or serves badly, ktms-node doctor takes the same flags (plus --model) and says in plain words what is wrong: engine not answering, model missing or not loaded, Ollama on the CPU (an Intel build under Rosetta on Apple Silicon), proxy unreachable or needing/not needing --tls, a node key that is wrong or bound to another node id, or a tunnel that cannot come up. It also asks the proxy to vet the exact models the agent would report against the key's allowed_models (nothing is stored). It exits 1 if it finds a problem.

A node on someone's own machine can follow a load schedule in local time with --schedule-file FILE, one rule per line:

# [days] HH:MM-HH:MM level
23:00-07:00 100
07:00-10:00 50
mon-fri 10:00-23:00 0

Days are mon..sun, ranges (mon-fri) and lists (sat,sun); without them a rule applies daily. A window past midnight belongs to the day it starts on, 24:00 ends a day, uncovered time is 100%, and overlapping rules are an error. The level is the share of time the node spends on proxy requests: the proxy gives it ceil(max_concurrent × level) slots and, where that is more than the level, holds the slot idle after each request (one slot at 50%: a 20 s request, then 20 s idle). At 0% the proxy sends nothing, the gateway answers 503 after a 5 s grace, and once the proxy has no requests left on the node the agent unloads the served models from Ollama; it loads them again --warmup (default 5m) before the next window. The tunnel and heartbeats keep running, so a pause ends at once. The agent reads the file every 30 s and on a change, keeps the last valid schedule if an edit breaks it, and runs at 100% without the file. A proxy that predates capacity levels routes at 100% (the agent logs a warning). ktms-node schedule --file FILE checks a file and prints the level now and the next switch; doctor reports it too. Engines other than Ollama only stop being routed to.

The owner can override the schedule for a while from the same machine:

ktms-node status                     # level, where it comes from, requests, models, next switch
ktms-node pause 10m                  # 0% for 10 minutes (default 10m), then the schedule again
ktms-node pause --until-next         # until the schedule's next switch (at most 24 h)
ktms-node pause 10m --now            # also cut the requests running now; their callers get an error
ktms-node level 30 --for 1h          # 30% for an hour
ktms-node resume                     # back to the schedule now

A manual level lasts from 1 minute to 24 hours, ends by the wall clock (a Mac that slept past the end resumes on waking) and survives an agent restart (~/.ktms-node/override.json, see --state-dir). A manual pause does not warm the models before it ends, so the memory stays free for the owner; they load right after it, and the proxy falls back meanwhile (--warm keeps the schedule's behaviour). These commands, and the desktop tray, talk to the running agent over a Unix socket, --control-socket (default ~/.ktms-node/control.sock, empty turns it off). It is owner-only: mode 0600, and a caller running as another user is refused. Its HTTP/JSON API (GET /v1/status, GET /v1/events as server-sent events, POST/DELETE /v1/override, GET/PUT /v1/schedule, POST /v1/schedule/preview, GET /v1/stats, GET/PUT /v1/settings) never returns the node key or any token.

Desktop tray. install-node.sh --tray adds ktms-node-tray: a menu bar app on macOS (~/Applications/Keystone Node.app, started at login, no Dock icon) or a tray icon on Linux (StatusNotifierItem; GNOME needs the AppIndicator extension). The icon shows the level and where it comes from, a countdown during a manual pause, and a warning when the agent or KTMS does not answer. Its menu pauses for 5, 10 or 30 minutes or until the next switch, limits the node to 50, 30 or 10% for an hour, cuts running requests, resumes the schedule and turns "Schedule set by KTMS" on or off. Its window, a page served on 127.0.0.1 that only the tray's one-time link opens, edits the schedule as a week grid or as text with a live preview, shows the usage log for 24 hours, 7 and 30 days, and turns notifications (pause over, agent silent, schedule invalid) on or off. The tray is a client of the control socket and keeps no state of its own beyond ~/.ktms-node/tray.json; quitting it leaves the node running.

Schedule set by KTMS. With --schedule-source=ktms, or ktms-node settings --schedule-source=ktms (the tray's "Schedule set by KTMS") at run time, the node takes its schedule and load from KTMS and ignores its local file, which stays in place for when the setting is turned off. KTMS keeps one schedule per node id (ktms nodes schedule set|show|list|delete, the node_schedules file section, SetNodeSchedule in the Admin API), validated with the agent's parser, plus an optional override of 1 minute to 24 hours (ktms nodes schedule override, SetNodeCapacityOverride). It reaches the node with a heartbeat within about 15 seconds (KTMS re-reads a node's schedule at most every 10 s). The node stores the last one it got in ~/.ktms-node/managed-schedule, so a restart without the proxy keeps it; before the first one it runs at 100%. The owner's own pause or level on the machine still wins over both. ListNodes shows each node's schedule_source, capacity_source and capacity_until.

While the proxy refuses the node (wrong key, other node id, no allowed model) the agent does not exit and does not open a tunnel: it checks with the proxy first, then retries with a pause that grows to five minutes and says why in the log, so a supervisor never restarts it into a fresh Cloudflare tunnel per attempt.

FreeToken

FreeToken runs large MoE models on one consumer NVIDIA RTX (30 to 50 series, Linux or Windows; not on a Mac, AMD or Intel): expert weights stay in RAM and the GPU caches the experts in use. ft serve runs one model per process:

export FREETOKEN_API_KEY=...              # optional; ft serve and the agent both read it
ft serve --model <hf-id or path> --served-model-name qwen3.6-35b
./bin/ktms-node --proxy-addr proxy.example.com:443 --tls --engine freetoken

With --engine freetoken the agent polls http://127.0.0.1:1919, FreeToken's default, and sends FREETOKEN_API_KEY as its bearer; the proxy uses it too unless NODE_AUTH_TOKEN is set. The model is reported with its context length from /v1/models and as loaded once /health says ok; while the weights load it is reported cold, and a server whose /health says error reports no models. The load comes from requests.active in /v1/stats, which counts every admitted request: those above --engine-max-running (FreeToken's --max-running-requests, default 4) count as waiting, so a node busy with other clients hands calls to the next model in the chain. --max-concurrent, what the proxy sends here, defaults to 4 too. FreeToken rejects response_format, so its nodes get the schema in the prompt; declare no_structured_output on the model as well, and the router checks the reply and retries a wrong shape. no_thinking sends reasoning_effort: none. ktms-node doctor checks that ft is installed, the server answers and takes the key, and the model is loaded. install-node.sh installs Ollama only; install FreeToken yourself.

Production checklist
  • TLS. The server speaks plaintext gRPC. Terminate TLS in front of it (Envoy, nginx with grpc_pass, a service mesh, a cloud load balancer) or keep it on a private network. Node agents that reach the proxy through that TLS endpoint run with --tls.
  • Back up the encryption keyring, apart from the database: without KTMS_ENCRYPTION_KEYS the stored vendor keys are unreadable. See Backups.
  • Mint real keys, then unset BOOTSTRAP_ADMIN_KEY. It is accepted as an implicit admin key.
  • Run Redis if you want limits. With REDIS_ADDR empty, RPM, token budgets and spend limits are not enforced (the proxy fails open by design, and does the same if Redis errors; set SPEND_LIMITS_FAIL_MODE=closed to refuse spend-limited calls instead).
  • Give each client its own API key (and its own vendor credentials), so spend, models and revocation are per client.
  • Mind your Postgres connection count. Each replica holds up to DB_MAX_CONNS (default 25) connections.
  • Back up Postgres. All configuration lives there; see Backups.
  • Keep internal ports internal. Expose gRPC (:9090, clients and node agents) and HTTP (:8080). Metrics (:9091) belong on the cluster network. /healthz and /readyz share :8080 with the API, and /readyz reports database state, so block both paths at a public ingress.
  • Network policies. Ingress: clients, node agents, the console, Prometheus (:9091) and the kubelet probes. Egress: the vendors, Postgres, Redis, the collector, the nodes, and what you configured (OTEL_EXPORTER_OTLP_ENDPOINT, SENTRY_DSN, LATENT_ENCODER_URL).
  • TLS to dependencies. Use sslmode=require for Postgres and USAGE_COLLECTOR_TLS for the collector. The Redis client has no TLS: keep Redis on a private network with REDIS_PASSWORD.
  • Least-privilege keys. generate for clients; admin:read for dashboards, assistants and anything that only looks; admin only for the console and operators.
  • Spend limits on every client key (Spend limits).
  • Secrets. Keep KTMS_ENCRYPTION_KEYS, DATABASE_URL and USAGE_COLLECTOR_TOKEN in a secret manager; the collector token needs only light-tokenmeter's write scope. Rotate the keyring with Rotating the encryption key.
  • Nodes. Leave NODE_ALLOW_LOCALHOST_ENDPOINTS=false outside development.
  • Alerts. Wire up the suggested alerts; Redis and the collector fail open, so without them a dead limiter or lost accounting goes unnoticed.

Configuration reference

Everything is an environment variable; see env.example.

Variable Default Description
DATABASE_URL – Required. Postgres connection string.
KTMS_ENCRYPTION_KEYS / LLMPROXY_ENCRYPTION_KEYS – Required. Versioned keyring 1:<base64 32 bytes>[,2:…]. The highest version encrypts new writes; all listed versions can decrypt.
GRPC_ADDR :9090 gRPC listen address (data and admin planes).
GRPC_MAX_RECV_MSG_SIZE 16777216 Maximum gRPC message receive size in bytes (16MB).
HTTP_ADDR :8080 HTTP listen address (health probes and the OpenAI-compatible API).
HTTP_API_ENABLED true Serve the /v1 API on HTTP_ADDR; false leaves only the health probes.
MCP_GATEWAY_ENABLED true Serve the MCP gateway (/mcp, /mcp/{server}) on HTTP_ADDR.
AGENTS_ENABLED true Serve the agents' inbox, the gateway's built-in server agents.
AGENTS_MAX_DEPTH 5 How deep a chain of tasks in one conversation may go.
AGENTS_MAX_MESSAGES_PER_CONTEXT 200 Messages in one conversation.
AGENTS_MAX_MESSAGE_BYTES 65536 Size of one message.
AGENTS_TOOL_RPM 60 Calls of each agents tool per key and minute; 0 is unlimited. Needs Redis.
AGENTS_RETENTION 720h Tasks unchanged this long are deleted; 0 keeps them.
AGENTS_CORRECTION_WINDOW 168h How long after its last change an ended task takes a correction of its answer.
AGENTS_MAX_PINNED 200 Tasks one agent may pin with pin_task; pinned tasks outlive AGENTS_RETENTION.
AGENTS_RECORDING metadata What the journal keeps by default: off, metadata or full.
AGENTS_JOURNAL_MAXLEN 100000 Events the journal's Redis stream keeps; the oldest go first.
AGENTS_JOURNAL_MAX_EVENT_BYTES 16384 Text of one full event; longer text is cut and hashed.
AGENTS_JOURNAL_REDIS_URL – A separate Redis for the journal (redis://...); empty uses REDIS_ADDR.
HTTP_API_TIMEOUT 5m Read/write timeout for one HTTP API call (it waits on an upstream model).
METRICS_ADDR :9091 Internal HTTP listen address for Prometheus /metrics and /healthz. Empty disables the metrics server.
LOG_LEVEL info zerolog level.
SHUTDOWN_TIMEOUT 30s Graceful drain time on SIGTERM.
DB_MAX_CONNS / DB_MIN_CONNS 25 / 5 Postgres pool bounds.
DB_TIMEOUT 3s Bounds connecting to Postgres (unless the DSN sets connect_timeout) and each per-request query such as key checks, so a hung database fails calls fast.
AUTH_STALE_IF_ERROR 5m While Postgres is unreachable, a key that verified within this window keeps working (a revocation made just before the outage can lag by at most this much). 0 refuses every call during an outage.
ROUTING_RELOAD_SECONDS 60 How often replicas poll for config changed elsewhere.
AUTO_MIGRATE true Apply embedded migrations at startup (guarded by an advisory lock, safe for rolling deploys). Off means applying migrations/*.sql by hand.
MIGRATE_TIMEOUT 3m Bound for the whole migration run.
AUTO_SEED false Automatically populate initial models, rules, and client key when starting against an unconfigured database (enabled in docker-compose).
SEED_CLIENT_KEY empty Pre-configured client API key (e.g. ktms_dev_default_key) for auto-seed; generated if unset.
SEED_CLIENT_NAME dev-default Name assigned to the auto-seeded client API key.
SEED_OPENAI_KEY / OPENAI_API_KEY empty OpenAI API key to encrypt and register during auto-seeding.
SEED_GEMINI_KEY / GEMINI_API_KEY empty Google Gemini API key to encrypt and register during auto-seeding.
SEED_ANTHROPIC_KEY / ANTHROPIC_API_KEY empty Anthropic API key to encrypt and register during auto-seeding.
REDIS_ADDR empty Redis for throttling. Empty disables limits.
REDIS_PASSWORD / REDIS_DB empty / 0 Redis auth and database.
REDIS_KEY_PREFIX empty Namespace for counters when Redis is shared (compose uses dev).
REDIS_TIMEOUT 500ms Per-command Redis read/write timeout (dials get twice as long, one retry). With the fail-fast guard, about what a hung Redis adds to a request before limits fail open.
BUDGET_GRACE_MARGIN 0.05 Fraction of daily token budget allowed as grace overdraft (0.05 = 5%, 0.0 or negative disables grace overdraft).
SPEND_LIMITS_FAIL_MODE open What spend limits do when Redis errors: open lets calls through unmetered (logged), closed refuses calls under a spend limit with UNAVAILABLE throttled:spend_unavailable.
SPEND_DEFAULT_OUTPUT_RESERVE_TOKENS 4096 Output tokens priced into a spend reservation when the call sets no max tokens.
CIRCUIT_BREAKER_ENABLED true Enable upstream Circuit Breaker to fail-fast during provider outages without network IO.
CIRCUIT_BREAKER_FAIL_THRESHOLD 5 Consecutive upstream failures (5xx, network timeouts) before tripping circuit to Open.
CIRCUIT_BREAKER_COOLDOWN 30s Cooldown period before allowing a single probe request in Half-Open state.
UPSTREAM_REQUEST_TIMEOUT 0 (disabled) Upstream vendor timeout (total per-attempt deadline for unary/media; per-chunk idle watchdog for streaming). Stalls trip breaker and advance fallback chains.
VENDOR_COOLDOWN_MAX 5m Longest pause honoured when a vendor answers 429/503 with Retry-After. 0 disables pauses.
BATCH_POLL_SECONDS 60 Background polling interval in seconds for checking unfinished batch jobs.
BATCH_MAX_ITEMS 10000 Maximum number of items allowed in a single batch submission.
RESULTS_RETENTION_DAYS 7 Retention period for completed batch results before cleanup (accepts integer days, e.g. 7, or Go duration string, e.g. 168h).
NODE_HEARTBEAT_INTERVAL_SECONDS 5 Node agent heartbeat reporting interval in seconds.
NODE_HEARTBEAT_TTL_SECONDS 15 Heartbeat TTL in seconds before an idle node transitions to OFFLINE.
NODE_RETENTION_TTL_DAYS 1 Retention period in days for inactive/offline nodes before garbage collection.
NODE_ALLOW_LOCALHOST_ENDPOINTS false Allow loopback/link-local endpoints for local inference nodes (useful in tests/dev).
NODE_USAGE_RETENTION_HOURS 168 Hours each node's request log (time, model, tokens, outcome; no user, key or text) is kept in Redis (6.2+) for the node's agent to pull with PullNodeUsage; a node with no requests for that long loses its log, and revoking its key deletes it. 0 turns the log off.
BOOTSTRAP_ADMIN_KEY empty Implicit admin key for creating the first real key. Unset afterwards.
USAGE_COLLECTOR_HOST empty host:port of a gRPC usage sink. Empty disables reporting.
USAGE_COLLECTOR_TLS false Reach the sink over TLS, e.g. light-tokenmeter on Cloud Run (host:443).
USAGE_COLLECTOR_TOKEN empty Bearer token sent to the sink on every call; for light-tokenmeter, a token with the write scope. Empty sends none.
USAGE_PROJECT_ID ktms Default project name on usage events.
SENTRY_DSN empty Error reporting; empty disables it.
SENTRY_ENVIRONMENT / SENTRY_RELEASE development / empty Sentry metadata.
SENTRY_ENABLE_TRACING false Sentry performance tracing.
SENTRY_DEBUG false Sentry SDK debug logging.
SENTRY_WITH_BREADCRUMBS false Attach recent log lines to Sentry events as breadcrumbs.
OTEL_SERVICE_NAME ktms Service name reported in OpenTelemetry traces.
OTEL_EXPORTER_OTLP_ENDPOINT empty OTLP gRPC endpoint (host:port) for distributed tracing; empty disables remote export.
CACHE_ENABLED false Enable prompt and completion exact-match cache in Redis.
CACHE_TTL 1h Expiration time for cached responses.
CACHE_MAX_BYTES 1048576 Maximum size in bytes of a single cached response (1MB). Responses exceeding this are not cached.
GUARDRAILS_ENABLED false Enable input/output security guardrails (PII masking and prompt injection detection).
GUARDRAILS_PII_ACTION block Action on detected PII: block (reject with guardrail_violation) or redact (mask with placeholders and desanitize in output).
GUARDRAILS_PROMPT_INJECTION_ACTION block Action on detected prompt injection: block or log.
GUARDRAILS_LATENT_ENABLED false Experimental. With GUARDRAILS_ENABLED, also screen prompts in latent space against injection, jailbreak, prompt-leak and exfiltration centroids (see Latent guardrails).
GUARDRAILS_LATENT_THRESHOLD 0.70 Cosine similarity to a malicious centroid at which a prompt is flagged. Tuned for the local encoder; recalibrate for a JEPA encoder.
GUARDRAILS_LATENT_ACTION block block (reject with guardrail_violation) or log.
GUARDRAILS_LATENT_CATEGORIES_FILE empty JSON array of extra categories (name, phrases, and/or precomputed centroids of {model, vector}), added to the built-in set.
SEMANTIC_CACHE_ENABLED false Experimental. Serve enable_cache requests whose final user message is semantically close to a cached one. Needs Redis; uses CACHE_TTL and CACHE_MAX_BYTES.
SEMANTIC_CACHE_SIMILARITY_THRESHOLD 0.95 Minimum cosine similarity for a semantic hit.
SEMANTIC_CACHE_MAX_ENTRIES 256 Vectors kept (and scanned per lookup) for one exact context; older entries are trimmed.
LATENT_ENCODER_URL empty Experimental. OpenAI-compatible /v1/embeddings endpoint, e.g. a node serving a JEPA encoder. Empty uses the local hash encoder; failures fall back to it.
LATENT_ENCODER_MODEL jepa-text Model name sent to the encoder; also namespaces cached vectors.
LATENT_ENCODER_API_KEY empty Bearer token for the encoder, if it needs one.
LATENT_ENCODER_TIMEOUT 50ms Bound for one encoder call before falling back to the local encoder.
LATENT_LOCAL_ENCODER_DIMS 512 Width of the local hash encoder's vectors.
LATENT_ENCODER_CACHE_SIZE 4096 In-memory LRU of recent embeddings, so guardrails and cache encode a prompt once. 0 disables it.

Setting it up: keys, models, rules

All of this is done with the admin service (AdminService) using a key that has the admin scope. Mutations are validated in a transaction together with the existing config, so an invalid state can never become active. Prefer a browser or a terminal to grpcurl? See the web console and the command-line tool.

Configuration as a file

Everything below can also live in a YAML file in your repository and be applied with the ktms command, which plans the changes first, like terraform plan:

export KTMS_ADDR=localhost:9090 KTMS_ADMIN_KEY=ktms_...   # admin; admin:read is enough for plan and export
ktms config export -o ktms.yaml      # start from what is running
ktms config plan -f ktms.yaml        # what would change; --detailed-exitcode exits 3 on drift
ktms config apply -f ktms.yaml       # asks before applying; --yes for scripts
# yaml-language-server: $schema=https://raw.githubusercontent.com/ai-process/ktms/main/schema/ktms-config.schema.json
api_version: ktms.ai-process.dev/v1alpha1
models:
  - {id: gpt-4o-mini, vendor: openai, efforts: [EFFORT_LOW, EFFORT_MEDIUM, EFFORT_HIGH],
     price_in_per_mtok: 0.15, price_out_per_mtok: 0.6, price_cache_read_per_mtok: 0.075}
rules:
  - {name: default, priority: 1, use: [gpt-4o-mini]}
organizations:
  - name: acme
    teams: [{name: search, monthly_budget_usd: 100, allowed_models: [gpt-4o-mini]}]
api_keys:
  - {name: billing, policy: {rpm_limit: 120, tpm_limit: 200000}}
  - {name: search-bot, organization: acme, team: search}
vendor_keys:
  - {api_key_name: billing, vendor: openai, plain_key_env: OPENAI_KEY_BILLING}
spend_limits:
  - {subject_type: key, subject_id: billing, period: day, limit_usd: 5}
user_rate_limits:
  - {user_id: "*", model_id: gpt-4o-mini, rpm: 30}
  • Models, rules, user rate limits and key policies are the Admin API's own messages (ModelSpec, RuleSpec, …) with the same field names; a model or rule without enabled is enabled. The JSON Schema is schema/ktms-config.schema.json (also ktms config schema), a full example schema/example.yaml.
  • Only the sections in the file are managed; a missing section is left alone. --prune deletes what a managed section does not list, except admin and admin:read keys, which it only reports.
  • Secrets never go in the file. A vendor key is read from the variable named in plain_key_env and compared with the stored one by fingerprint (VendorKeyInfo.fingerprint); a new client key is printed once, after apply. Exported vendor keys point at KTMS_VENDOR_KEY_<KEY>_<VENDOR>. MCP credentials (mcp_credentials) work the same way with value_env, exported as KTMS_MCP_CREDENTIAL_<SERVER>.
  • What the Admin API cannot change is refused before anything is applied: a key's scopes or team, or a revoked key named again. Mint a key under another name instead.
  • A team's cap is its monthly_budget_usd; spend_limits holds key and user caps.
  • node_schedules lists the schedules KTMS sends to nodes that hand theirs to it (node_id and schedule, the rules as text); temporary overrides are not configuration.
  • Steps run in dependency order (organizations, models, keys, vendor keys, rules, limits, then deletes). If one fails, the earlier ones stay applied; plan again to see what is left.
1. API keys
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" \
  -d '{"name":"billing-service","scopes":["generate"]}' \
  localhost:9090 ktms.v1.AdminService/MintAPIKey

Keys look like ktms_<hex> (keys minted before the rename start with llm_ and keep working). Only a hash is stored, and the plaintext is returned once. Scopes: generate (data plane), mcp (the MCP gateway), admin (implies everything) and admin:read (the Admin API reads: GetConfig, GetSpend, GetTeam, GetTeamSpend, ExplainRoute and every List*; nothing that changes state) and agents:read (the agents' cards and conversations, messages included: ListAgents, ListAgentTasks, GetAgentTask; nothing else). A read key sees every tenant, so it cannot be bound to an organization or team. GetConfig never returns a vendor key itself, only its fingerprint. ListAPIKeys and RevokeAPIKey do what they say.

A key can carry its own policy, on top of its team's and each model's rpm, at minting ("policy": {...}) or later with SetAPIKeyPolicy (it replaces the policy; the key's next call uses it):

grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" \
  -d '{"name":"billing-service","policy":{"allowed_models":["gpt-4o-mini"],"rpm_limit":120,"tpm_limit":200000}}' \
  localhost:9090 ktms.v1.AdminService/SetAPIKeyPolicy
  • allowed_mcp: the MCP servers and tools a key with the mcp scope may use; see MCP gateway.
  • data_classification: restricted keeps every call of the key on self-hosted nodes; see Restricted data.
  • allowed_models: the models the key may call; empty allows any. A chain keeps only the models both the key's and its team's lists allow; an override outside them, or a chain with none left, is PERMISSION_DENIED (the message names key or team).
  • rpm_limit: calls per minute over all models, a minute of them as burst.
  • tpm_limit: tokens per minute over all models (GCRA weighted by tokens). A call books its input estimate when admitted and its real input + output when it ends, so an underestimate is paid for by the next calls; a call larger than a minute of tokens never fits and is refused with FAILED_PRECONDITION request_too_large (HTTP 400). A cache hit counts as a call, with no tokens.
  • Both rates count text generation, embeddings, judgments, speech and images (media calls by their real tokens only), not batches. Refusals are RESOURCE_EXHAUSTED throttled:key_rpm / throttled:key_tpm with RetryInfo (HTTP 429 with Retry-After). Like every limit they need Redis and fail open without it.
2. Vendor keys (per client)

A vendor credential is assigned to one API key and encrypted at rest. A client can only use models of vendors it holds a credential for, and ListModels reflects that. The exception is the local vendor: models served by registered nodes need no vendor key, so every client can call them.

grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" \
  -d '{"api_key_name":"billing-service","vendor":"google","plain_key":"AIza..."}' \
  localhost:9090 ktms.v1.AdminService/SetVendorKey

Vendors: openai, anthropic, google, deepseek (OpenAI-compatible API, default endpoint https://api.deepseek.com), groq (OpenAI-compatible API, default endpoint https://api.groq.com/openai/v1), mistral (OpenAI-compatible API, default endpoint https://api.mistral.ai/v1), azure (Azure OpenAI Service; endpoint is required, e.g. https://<resource>.openai.azure.com with optional ?api-version=...; model id maps to deployment name; content filter violations automatically advance to the next fallback candidate), bedrock (Amazon Bedrock Converse/ConverseStream API; key format is <access_key_id>:<secret_access_key>[:<region>[:<session_token>]], defaulting region to us-east-1; model id maps to Bedrock foundation model or inference profile ARN/ID; optional endpoint overrides the base URL; content filter and guardrail interventions automatically advance to fallback candidate), ollama (any self-hosted OpenAI-compatible server such as Ollama or llama.cpp; the model's endpoint is required, e.g. https://host/v1, and the vendor key is the server's own) and typesafe (judgments only). Use DeleteVendorKey to remove one.

3. Models
{ "model": {
    "id": "gemini-2.5-flash",              // referenced by rules and model_override; also sent to the vendor
    "vendor": "google",
    "endpoint": "",                         // optional base-URL override (Azure, self-hosted gateways…)
    "efforts": ["EFFORT_LOW", "EFFORT_MEDIUM"],
    "capabilities": ["google_search"],
    "rpm": 600,                             // per client key per minute, 0 = unlimited
    "price_in_per_mtok": 0.30,              // for usage reporting
    "price_out_per_mtok": 2.50,
    "price_in_peak_per_mtok": 0,            // for vendors with peak-hour pricing, else 0
    "price_out_peak_per_mtok": 0,
    "price_in_batch_per_mtok": 0,           // batch pricing; 0 defaults to half normal price
    "price_out_batch_per_mtok": 0,
    "price_cache_read_per_mtok": 0.075,     // cached input; 0 = vendor default (see below)
    "price_cache_write_per_mtok": 0,
    "daily_tokens_per_key": 50000000,       // per API key per UTC day, 0 = unlimited
    "daily_tokens_per_user": 500000,        // per attributes["user_id"] (or svc:<key_name> fallback) per UTC day
    "languages": {"source": ["*"], "target": ["*"]}, // required on translate models: the pairs served
    "price_per_million_chars": 0,           // translate: per million source characters, added to tokens
    "enabled": true } }

(The // comments are for illustration; strip them from real JSON.) Send it to UpsertModel.

When price_in_batch_per_mtok and price_out_batch_per_mtok are 0, the proxy automatically defaults them to half the regular price (price_in_per_mtok / 2, price_out_per_mtok / 2).

Prompt caching. Input served from or written to the vendor's prompt cache is priced at price_cache_read_per_mtok and price_cache_write_per_mtok (standard rates; peak and batch calls scale them like input). At 0 the vendor default applies, chosen never to undercharge: for anthropic, 0.1x input to read and 1.25x to write (the 5-minute cache); for bedrock, 0.25x and 1.25x (Claude reads at 0.1x and Nova at 0.25x there); for every other vendor, the input price. Set the read rate for OpenAI, Gemini and DeepSeek models to get their discount; the seeded OpenAI and Gemini models carry theirs. KTMS reads the cached part from each vendor (cache_read_input_tokens and cache_creation_input_tokens, prompt_tokens_details.cached_tokens, DeepSeek's prompt_cache_hit_tokens, Gemini's cachedContentTokenCount, Bedrock's cacheRead/WriteInputTokens), bills spend limits by it, sends it to light-tokenmeter as cache_read_tokens and cache_write_tokens, and returns it in TokensUsage.cache_read / cache_write and, over HTTP, usage.prompt_tokens_details.cached_tokens.

Capabilities a model may declare:

Capability Meaning
batch Can serve asynchronous batch text generation requests (SubmitBatch).
google_search Can serve requests with enable_google_search.
no_thinking The model reasons by default and bills the reasoning as output (DeepSeek V4, Gemma 4 on Ollama). The proxy turns it off: thinking: {"type": "disabled"} for DeepSeek, reasoning_effort: "none" for Ollama, and for local models when the serving node runs Ollama (other engines get no switch). OpenAI-compatible vendors only.
no_sampling The model's API rejects sampling fields Keystone would otherwise send it (see Sampling): it gets none of temperature, top_p, stop and seed, and responses report them as ignored.
sampling The model takes its vendor's sampling fields although its id says otherwise (see Sampling): an Azure deployment name, a Claude alias, a reasoning model run without reasoning.
prompt_cache Anthropic only: puts cache_control breakpoints after the tools, the system prompt and, in a multi-turn conversation, the latest turn, so repeated prefixes are read at 0.1x input. A new prefix is written at 1.25x, so set it on models whose instructions or conversations repeat.
vision Can process image parts in multimodal messages (ImagePart).
audio_input Can process audio parts in multimodal messages (AudioPart).
pdf_input Can process PDF document parts in multimodal messages (DocumentPart).
tts Can serve SynthesizeSpeech.
image Can serve GenerateImage.
judge Can serve Judge (TypeSafe jev-latest, OpenAI gpt-6-luna).
translate Can serve Translate: a chat model by prompt, a node MT model natively. Declares languages (see Translation).
embedding_input A judge model that scores raw latent vectors in JudgeRequest.state_media (EmbeddingPart).
4. Rules
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"rules":[
  {"name":"spanish-high",  "priority":30, "effort":"EFFORT_HIGH",
   "attributes":{"language":"es"}, "use":["gemini-2.5-pro","gpt-4o"], "enabled":true},
  {"name":"low-default",   "priority":20, "effort":"EFFORT_LOW",
   "use":["deepseek-chat","gpt-4o-mini"], "enabled":true},
  {"name":"anything-else", "priority":1,
   "use":["gpt-4o-mini"], "enabled":true}
]}' localhost:9090 ktms.v1.AdminService/ReplaceRules
  • The highest priority matching rule wins; priorities and names are unique.
  • A rule matches when its effort equals the request's (unspecified = any effort) and every attribute in the rule equals the request's attribute.
  • use is the ordered fallback chain, cheaper-first by convention. If the first model is throttled or its vendor is unavailable, the next is tried.
  • weights (optional) enables weighted traffic routing (Canary or A/B testing). If specified, weights must contain exactly one positive integer per model in use (e.g. [90, 10]). When an explicit canary_key or user_id attribute is provided, distribution is deterministic and sticky per key (salted per rule name). If omitted or when canary_mode: "random" is passed, routing uses weighted random distribution. When weights is omitted, routing is strictly deterministic (100% on use[0]).
  • shadow_model and shadow_percentage (optional) enable Shadow Routing (Request Mirroring): asynchronously duplicates a sample (0.0–100.0%) of incoming traffic to a candidate shadow model in the background without affecting caller latency or responses. Shadow calls are non-billable to end-users (recorded at 0 nano-USD cost in usage events), carry is_shadow: "true" and shadow: "true" metadata, do not count against caller spend limits or daily token budgets, respect model RPM limits and global concurrency semaphores (512 for shadow, 256 for evaluation) to protect proxy resources and upstream providers, and never trip circuit breakers for primary traffic.
  • shadow_eval (optional boolean, requires shadow_model): enables automated quality evaluation comparing primary vs shadow model outputs using an internal Judge model asynchronously in the background. The judge evaluates preference (primary, shadow, or tie) and quality scores without adding latency to the primary caller, respects judge model RPM and circuit breakers without consuming caller spend limits, and exports results via Prometheus metrics (ktms_shadow_judge_evaluations_total and ktms_shadow_judge_scores).
  • Validation requires a catch-all (no attribute predicates) for every effort, so no request can ever fall through. anything-else above covers all three at once.
  • ReplaceRules swaps the whole set atomically; there is no per-rule CRUD.

Inspect the live configuration with GetConfig.

5. Spend limits (optional)

Hard caps in USD across all models, kept in nano-USD (1 USD = 1 000 000 000) to avoid float drift. Calls are priced from the model's price_*_per_mtok (peak and batch rates included):

# $50 a day for the "support-bot" key, and $20 a month for every end user by default.
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"spend_limit":
  {"subject_type":"key","subject_id":"support-bot","period":"day","limit_nanousd":50000000000}}' \
  localhost:9090 ktms.v1.AdminService/SetSpendLimit
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"spend_limit":
  {"subject_type":"user","subject_id":"*","period":"month","limit_nanousd":20000000000}}' \
  localhost:9090 ktms.v1.AdminService/SetSpendLimit
  • subject_type is key (API key name) or user (attributes["user_id"], or svc:<key_name>); subject_id * is the default for every subject of that type. period is a UTC day or month.
  • Before each model in the chain is called, its worst case (estimated input × the dearer of the input and cache-write price + max tokens × output price, or SPEND_DEFAULT_OUTPUT_RESERVE_TOKENS without max tokens) is reserved atomically on every applicable counter by one Redis Lua script; when the call ends the reservation is settled to the actual cost. A call that would cross any cap is refused, so a cheaper model further down the chain may still be tried.
  • Batches are checked at submission (refused once a cap is reached) and charged at the batch rate when results arrive.
  • Refusals are RESOURCE_EXHAUSTED throttled:spend_key_daily, throttled:spend_key_monthly, throttled:spend_user_daily or throttled:spend_user_monthly, with a RetryInfo until the period resets (HTTP: 429 insufficient_quota plus Retry-After).
  • ListSpendLimits, DeleteSpendLimit and GetConfig show and lift caps. Every usage event carries its cost_nanousd, which light-tokenmeter sums per day, model, user or job.
  • GetSpend shows where a subject stands now, from the counters that refuse calls: for a key ({"subject_type":"key","subject_id":"billing-service"}), user or team, one window per period with spent_nanousd, limit_nanousd, remaining_nanousd and resets_at. Spend is counted only under a cap (a * limit counts for every subject): an uncapped window says metered: false, and a cap set mid-period counts from then on. A new cap shows once the config reloads: right away on the replica that set it, within ROUTING_RELOAD_SECONDS on the others.
6. Organizations and teams (optional)

Group keys into organizations and teams to give each team its own models and monthly budget:

grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"name":"acme"}' \
  localhost:9090 ktms.v1.AdminService/CreateOrganization
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"org_id":"<org id>","name":"search",
  "monthly_budget_usd":500,"allowed_models":["gpt-5.1","local-qwen3-32b"]}' \
  localhost:9090 ktms.v1.AdminService/CreateTeam
grpcurl -plaintext -H "authorization: Bearer $ADMIN_KEY" -d '{"name":"search-service",
  "scopes":["generate"],"org_id":"<org id>","team_id":"<team id>"}' \
  localhost:9090 ktms.v1.AdminService/MintAPIKey
  • A team key may call only the team's allowed_models (empty means any). A model override outside them is PERMISSION_DENIED; a chain is filtered to them, and a chain with none left is PERMISSION_DENIED. The proxy's own judge calls (auto effort, shadow evaluation) are not restricted. ListModels (and /v1/models) lists only the allowed models.
  • UpdateTeam replaces the team: send every field, since an omitted budget or model list lifts the cap or the restriction.
  • monthly_budget_usd is a spend limit on all the team's keys together (UTC month), refused with RESOURCE_EXHAUSTED throttled:spend_team_monthly. It is set only through CreateTeam/UpdateTeam (0 lifts it); SetSpendLimit refuses subject_type: "team". GetTeamSpend reads the enforced counter, which runs only while a budget is set.
  • data_classification: restricted keeps every call of the team's keys on self-hosted nodes; see Restricted data.
  • Rules can match org_id and team_id. The proxy sets them from the key; callers cannot.
  • Deleting a team or organization detaches revoked keys and is FAILED_PRECONDITION while active keys remain: revoke or move them first.
Speech, image and judgment rules

These modalities carry no effort. The proxy routes them at EFFORT_LOW with a reserved attributes["modality"] set to tts, image, judge or translate, and only rules that name that modality can match, over models that declare the matching capability:

{"name":"speech", "priority":100, "attributes":{"modality":"tts"},
 "use":["gemini-2.5-flash-preview-tts"], "enabled":true}

Without such a rule the call fails with FAILED_PRECONDITION no_capable_model.

Rotating the encryption key

New writes use the highest version in KTMS_ENCRYPTION_KEYS; every listed version can decrypt.

  1. Add a new version and deploy it to every replica: 1:<old>,2:<new>. Until all replicas run the new keyring, do not add or change vendor keys (or the SEED_*_KEY values with AUTO_SEED): a replica still on 1:<old> cannot read a key written with version 2. It logs an error, counts ktms_dependency_errors_total{dependency="keyring"}, and keeps the credential it already had for that client and vendor; a replica that starts in that state has none, so that client's requests skip that vendor's models (the next model in the chain serves, or no_vendor_key if none can). The rest of the config keeps loading either way. A keyring that can read no vendor key at all is refused at startup: that is a wrong keyring, not a rotation.
  2. Call RotateEncryption once, on any replica. It re-encrypts every vendor key with version 2 (in one transaction that locks the rows it rewrites, so a key set at the same moment is not lost) and the auth tokens of registered nodes, and returns how many it changed; calling it again changes nothing. If a vendor key needs a version the keyring lacks, it fails with FAILED_PRECONDITION naming the key and changes nothing.
  3. Remove version 1 and redeploy, but only when no database backup you may restore still holds keys encrypted with it (see Backups). Until then, leaving it listed is harmless. Cached responses encrypted with a removed version are treated as cache misses.

TestRotateEncryptionLetsTheOldKeyGo runs this sequence against Postgres in CI.

Run a node

Lend a machine's GPU to the proxy: an Apple Silicon Mac, or Linux with an NVIDIA card (or just a CPU, slowly). Three steps; no tunnels or service files to write by hand, and no admin rights on macOS.

1. Get a key. Whoever runs the proxy mints one key per machine, bound to a node id, and gives you the key (ktms_node_...), the node id and the proxy address. They also need a local model whose id is exactly the Ollama tag the node serves (G and ADMIN as in the quick start; drop -plaintext for a proxy behind TLS):

# admin, once per machine
$G "$ADMIN" -d '{"name":"anna-mac","node_id":"anna-mac"}' \
  proxy.example.com:443 ktms.v1.AdminService/MintNodeKey      # plain_key shown once

# admin, once per model; no_thinking turns Gemma's reasoning off
$G "$ADMIN" -d '{"model":{"id":"gemma4:26b-a4b-it-qat","vendor":"local",
  "efforts":["EFFORT_LOW","EFFORT_MEDIUM"],"capabilities":["no_thinking"],
  "queue_timeout_ms":2000,"enabled":true}}' \
  proxy.example.com:443 ktms.v1.AdminService/UpsertModel

2. Run one command on the machine (replace the three values), and paste the key when asked. The input is hidden and the key is stored in a file only you can read:

curl -fsSL https://raw.githubusercontent.com/ai-process/ktms/vX.Y.Z/scripts/install-node.sh | sh -s -- \
  --proxy proxy.example.com --node-id anna-mac --model gemma4:26b-a4b-it-qat

It downloads Ollama, the model and the node agent, starts them as background services and keeps the machine awake, and waits until the node has registered. Linux services start at boot. On a Mac they are login items: after a reboot the node comes back when its user logs in, so turn on automatic login for an unattended Mac. The model is large, so the first run takes a while. Add --max-concurrency N for a machine that can serve more than one request at a time (default 1).

3. Check. The last lines say Node anna-mac is running and registered. The proxy admin confirms with ListNodes that the node is HEALTHY and has the model.

To keep the machine free while its owner works, add --schedule FILE with load levels by local time (format above, under the node agent). The installer checks the file and copies it to ~/.ktms-node/schedule; edit that file later and the agent picks it up, no re-run needed. The last lines then show the level now and the next switch (Schedule: now 0%, next 23:00 100%), and during a pause the installer does not load the model into memory. The Stats: line gives the command that shows how the machine was used (~/.ktms-node/bin/ktms-node stats).

Add --tray for the menu bar (macOS) or tray (Linux) app that pauses the node for a while and edits its schedule without touching the file (see Desktop tray above). A re-run without --tray keeps it.

Run the same command again to update; a re-run only restarts what changed, and a new agent that fails to start is rolled back. Remove everything with the same command plus --uninstall (downloaded models stay in ~/.ollama).

A machine that ran the node before the rename (llm-proxy-node, in ~/.llm-proxy-node) is taken over by the same command. Its node key is reused when it was minted for the same --node-id (for another id you are asked first), and its Ollama build instead of a download. Once everything else is in place, right before the new Ollama starts, its services (com.llm-proxy.* launch agents, llm-proxy-* user units) are stopped for good and their files and the old directory moved to ~/.ktms-node.backup/<time>/; an install that fails before that leaves the old node running. --dry-run shows this plan and changes nothing; --keep-legacy leaves the old directory in place, though its services still go, or two nodes would share one id and port. --uninstall retires old services too; backups survive it unless you add --purge-backups.

The installer also refuses to run Ollama on an address another process already listens on (another Ollama, say), since the model would be pulled into that one; it names the process. Stop it, or set KTMS_OLLAMA_ADDR=127.0.0.1:PORT.

A translation node

--engine mt installs ktms-mt instead of Ollama: a Python venv with CTranslate2, the Opus-MT models converted to int8, and a service on 127.0.0.1:11436; the node agent then runs with --engine mt. Its models declare only translate, so they serve Translate and never a chat. Python 3.11–3.13 must be installed.

curl -fsSL https://raw.githubusercontent.com/ai-process/ktms/vX.Y.Z/scripts/install-node.sh | sh -s -- \
  --proxy proxy.example.com --node-id anna-mt --engine mt --model opus-mt-de

The admin registers the model like any local one, with its pairs:

- id: opus-mt-de
  vendor: local
  efforts: [EFFORT_LOW]
  capabilities: [translate]
  languages: {source: ["*"], target: [de]}

opus-mt-de translates English, French, Spanish and Italian with direct Opus-MT pairs and every other language through a multilingual model; ktms-mt catalog lists the others (single pairs, M2M100, MADLAD-400). Any server that speaks the same /v1/models and /v1/translate contract can take its place behind --engine mt. When the node stops, the translate rule falls through to the next model of its chain, a chat model translating by prompt.

Measured on an Apple M1 Pro (10 cores, 32 GB), opus-mt-de, int8 on the CPU, greedy decoding (beam_size = 1, the default), hotel reviews of 183 characters on average in English, French, Spanish, Italian and Dutch:

Path Reviews per minute Characters per second
ktms-mt bench, batches of 128 1,869 5,670
Through Keystone: ktms translate, 128 texts per call, one client 1,475 4,480
The same, two clients at once 1,097 3,330
ktms-mt bench, beam_size = 2 900 2,730
ktms-mt bench, device = "mps" (CTranslate2's experimental Apple GPU backend), beam 2 536 1,630

Throughput follows characters: reviews of 400 characters go at about 670 a minute through Keystone. One stream of large batches is fastest; CTranslate2 already uses every core.

If the node does not show up

Run the same command with --doctor. It says in plain words what is wrong: Ollama not answering, the model missing or not loaded, Ollama on the CPU (an Intel build running through Rosetta), the proxy unreachable or --tls wrong, a key that is wrong or belongs to another node id, or a tunnel that cannot come up (outbound port 7844 blocked). Logs are in ~/.ktms-node/logs/.

Symptom Cause and fix
Killed: 9, or "Apple could not verify..." The agent came through a browser download, which macOS quarantines. Use the curl command above; curl and brew files are not quarantined.
Very slow, GPU idle Intel Ollama through Rosetta, or no NVIDIA driver. The installer uses its own native Ollama and says when it ignores an Intel one.
key belongs to node "x" The key is bound to one machine. Use the node id it was minted for, or ask for a new key.
model "x" not permitted for this node key The key's allowed_models excludes a model on the machine. The installer reports only --model; for a hand-run agent pass --only-model.
Needs a new key Re-run with --new-key.
127.0.0.1:11435 is taken by … Another process holds the node's Ollama port. Stop it, or pick another address with KTMS_OLLAMA_ADDR.
Address changed after a restart Expected: quick tunnel addresses change, and the node re-registers by itself.
Node is offline after a Mac reboot macOS starts the services at login, not at boot. Log in, or enable automatic login (System Settings > Users & Groups).
The machine sleeps The services keep it awake while they run, on a Mac only while plugged in. Closing a laptop lid still sleeps it.

Installs on Linux need zstd (Ubuntu: sudo apt install zstd) and a normal login session (systemd user services). The installer script's other options are in install-node.sh --help.

Web console

ktms-admin is a small web UI for everything in the previous section: models, routing rules, client API keys, vendor credentials and a playground for trying a prompt through your rules. It can also show usage and spend per project, user and model when paired with light-tokenmeter. Sign-in is a password, Google or any OpenID Connect provider, GitHub, or an access proxy (oauth2-proxy, Authelia, Cloudflare Access, …), limited to an e-mail allowlist with wildcards (*@example.com).

# 1. a key for the console
grpcurl -plaintext -H "authorization: Bearer $BOOTSTRAP_ADMIN_KEY" \
  -d '{"name":"console","scopes":["admin"]}' localhost:9090 ktms.v1.AdminService/MintAPIKey

# 2. a password hash for you (asks twice)
docker run --rm -it coyl/ktms-admin:latest hash-password you@example.com

# 3. run it (its README covers the other sign-in methods)
docker run -d -p 8081:8080 \
  -e PUBLIC_URL=https://admin.example.com \
  -e ADMIN_USERS='you@example.com:$argon2id$…' -e ALLOWED_EMAILS='you@example.com' \
  -e SESSION_SECRET="$(openssl rand -base64 36)" \
  -e KTMS_ADDR=ktms:9090 -e KTMS_ADMIN_KEY=ktms_… \
  coyl/ktms-admin:latest

Command-line tool

ktms covers the everyday admin jobs without grpcurl and the proto: the configuration as a file, keys, spend and limits, nodes, a test call and health checks. Keys never go on the command line, and none is printed except a new one, once.

# a release archive (darwin/linux, amd64/arm64, signed checksums), or:
go install github.com/ai-process/ktms/cmd/ktms@latest
docker run --rm -e KTMS_ADDR -e KTMS_ADMIN_KEY coyl/ktms-cli:latest models list

A context names a Keystone and where its keys live, so a command needs no flags:

ktms init prod                          # asks for all of it, see Quick start; or by hand:
ktms context set prod --addr ktms.example.com:443 --tls --http https://ktms.example.com
ktms context store-key prod             # reads the admin key without echo, into the keychain
ktms context store-key prod --client    # a generate key for ktms chat
ktms context use prod

Contexts live in ~/.config/ktms/config.yaml (KTMS_CONFIG). A key source is env:VAR, file:PATH or keychain (macOS Keychain, or the secret service through secret-tool on Linux); store-key --file keeps the key in a 0600 file instead. Flags win over KTMS_ADDR, KTMS_TLS, KTMS_HTTP, KTMS_ADMIN_KEY and KTMS_API_KEY, which win over the current context; a context named with --context or KTMS_CONTEXT wins over them, so its keys never go elsewhere.

ktms health                                   # /healthz, /readyz and the server's version
ktms models list; ktms rules list             # -o json or -o yaml for scripts
ktms keys mint billing --team acme/search --rpm 120
echo "$OPENAI_KEY" | ktms vendor-keys set billing openai
ktms keys policy set billing --allowed-models gpt-4o-mini --tpm 200000
ktms chat "Say hi"                            # the answer, then rule, model and vendor
ktms translate --to de reviews.txt            # one translation per line, see Translation
ktms limits set key billing day 5             # USD per UTC day; * sets the default
ktms spend get key billing                    # or user ID, team org/team
ktms route explain billing --effort high      # where it would go, and why
ktms nodes list; ktms nodes drain gpu-1
ktms nodes schedule set mac --file week.txt   # for nodes that hand their schedule to KTMS
ktms nodes schedule override mac --level 0 --for 2h
ktms node-keys mint gpu-1 --node-id gpu-1 --allowed-cidrs 10.0.0.0/8
ktms keys revoke billing                      # asks first; --yes for scripts
ktms rotate-encryption
ktms mcp-servers list                         # the MCP gateway's servers, see MCP gateway
ktms completion zsh > "${fpath[1]}/_ktms"     # also bash and fish
ktms mcp                                      # tools for AI assistants, see below

ktms route explain KEY --effort low [--attr k=v] [--placement P] [--model M] asks Keystone where such a request would go: the rule, every model of its chain kept or dropped with the reason (no vendor key, key or team policy, capability, placement, circuit open, vendor paused, no node), and what a call would try. It runs Keystone's own routing (AdminService/ExplainRoute), calls no vendor or judge and spends nothing; rate limits and budgets are not checked. With --auto and no effort it shows the judge's fallback, medium. --modality translate --from fr --to de (or judge, tts, image) explains that RPC's route instead, models without the language pair dropped as language_pair.

admin:read is enough for everything that only reads (list, get, route explain, config plan|export). Exit codes follow the error contract: 0 done, 1 failed, 2 bad usage, 3 plan has changes (--detailed-exitcode), 4 not authenticated or not allowed, 5 not found, 6 rejected as invalid, 7 unavailable or throttled (retry later).

MCP server for AI assistants

ktms mcp gives Claude Code, Claude Desktop, Cursor and other MCP clients Keystone's tools, so an assistant reads and changes the configuration itself instead of writing grpcurl scripts. It uses the same contexts and keys as the CLI; the key never reaches the assistant.

  • Read tools (the only ones by default): get_config (the live configuration as a ktms config file), list_models, list_rules, list_keys, list_vendor_keys, list_teams, list_spend_limits, get_spend, list_nodes, list_node_keys, explain_route, health. An admin:read key is enough.
  • Write tools, offered only with --allow-write: config_plan (a ktms config file in, the plan and a plan_id out; nothing changes) and config_apply (applies that plan once, in the session that planned it, and only if the live configuration has not changed since), mint_key (the key comes back once, marked for the user), revoke_key, set_key_policy, set_spend_limit, delete_spend_limit, drain_node. Vendor keys in a plan come from the MCP server's environment, never from the chat, and only from variables named KTMS_VENDOR_KEY_* (the names ktms config export writes), so an assistant cannot point plain_key_env at the admin key or another secret. Each tool carries readOnlyHint / destructiveHint.
  • Resources ktms://config (the live configuration) and ktms://schema (the file's JSON Schema); prompts configure_routing, integrate_service, local_node, production_review.
  • Names and values from Keystone are other people's text: the tools say so, and an assistant should treat them as data.

Claude Code (.mcp.json in a repository, or claude mcp add ktms -- ktms mcp), Claude Desktop (claude_desktop_config.json) and Cursor (.cursor/mcp.json) take the same entry:

{
  "mcpServers": {
    "ktms": { "command": "ktms", "args": ["mcp"], "env": { "KTMS_CONTEXT": "prod" } }
  }
}

Add "--allow-write" to args to let the assistant plan and apply changes. Over the network, ktms mcp --listen 127.0.0.1:8765 serves streamable HTTP at /mcp; every request needs Authorization: Bearer <token> with the token from KTMS_MCP_TOKEN (16+ characters, e.g. openssl rand -hex 32). It speaks plain HTTP: keep it on localhost or behind a TLS proxy.

{
  "mcpServers": {
    "ktms": { "type": "http", "url": "http://127.0.0.1:8765/mcp",
              "headers": { "Authorization": "Bearer ${KTMS_MCP_TOKEN}" } }
  }
}
Claude Code plugin and skills

plugin/ is a Claude Code plugin with both MCP servers (ktms mcp, tokenmeter mcp, read-only) and skills for the usual tasks: seeing how Keystone fits a system, connecting a service, configuring routing, deploying, adding a local node, migrating from LiteLLM, usage reports and a production review.

claude plugin marketplace add ai-process/ktms && claude plugin install ktms@ai-process

The skills are plain SKILL.md folders, so other agents that read Agent Skills can use them too; see plugin/README.md. CI fails when a skill names a command, flag, MCP tool or README section that does not exist. Assistants without skills or MCP can use the prompts, which cover the same tasks.

Calling Keystone

Authenticate every call with authorization: Bearer ktms_… (legacy llm_… keys supported) gRPC metadata.

You can import the committed Go stubs directly from this repository:

go get github.com/ai-process/ktms/gen/ktms/v1
Client SDKs

Generate a client for your language from proto/ktms/v1 (below). Pre-built SDKs on the Buf Schema Registry are planned and will be announced in the CHANGELOG when they are published.

Local Client Generation

Generate client stubs with buf, without installing local compiler plugins:

make proto     # Go stubs in gen/
make proto-py  # Python stubs in gen/python/
make proto-ts  # TypeScript stubs in gen/ts/
GenerateText
{
  "effort": "EFFORT_MEDIUM",
  "attributes": {"language": "es", "user_id": "u-42", "task": "summary"},
  "base_system_instruction": "You are a concise assistant.",
  "request_instruction": "Answer in one paragraph.",
  "messages": [{"role": "MESSAGE_ROLE_USER", "text": "Summarize …"}],
  "response_schema": {"type": "object",
    "properties": {"summary": {"type": "string"}}, "required": ["summary"]},
  "max_output_tokens": 800,
  "action_id": "app.summarize"
}
Field Notes
effort Routing tier (EFFORT_LOW, EFFORT_MEDIUM, EFFORT_HIGH).
attributes Opaque key/values matched by rules and copied to usage events. user_id charges the per-user daily budget (falls back to svc:<key_name> for service callers without user_id). usage_project overrides the usage project (see below).
base_system_instruction, request_instruction System content never travels as a message.
response_schema Unset = free text. Set = JSON output in that shape: types string, integer, number, boolean, object, array, plus required, nullable, enum, format and additional_properties. Vendors with structured output enforce it; for the others the proxy checks the reply (types, required keys, enum values, extra keys when additional_properties is false), retries once and then falls back. A format a vendor rejects is passed to the model in the description instead.
tools, tool_choice Function/tool declarations with JSON Schema parameters. tool_choice specifies auto, required, none, or a specific named function. In non-streaming calls, arguments of strict tools are checked against their parameters, since not every vendor enforces strict; a call outside them is retried once and then falls back. Streamed tool calls reach the caller as they arrive and are not checked.
messages Chat history. Supports MESSAGE_ROLE_USER, MESSAGE_ROLE_ASSISTANT, and MESSAGE_ROLE_TOOL (with tool_call_id). User messages support multimodal parts (ContentPart: text, image, audio, document). Assistant messages can carry tool_calls.
max_output_tokens Ceiling, 0 = none. Output tokens are the expensive ones; set it.
temperature, top_p, stop, seed Sampling, passed to the model that serves the call; unset leaves its default. temperature 0..2, top_p 0..1, up to 16 non-empty stop sequences; other values get INVALID_ARGUMENT. See Sampling.
enable_google_search Only Google models declaring google_search can serve it.
action_id Free-form label for usage attribution.
model_override Exact model id; bypasses rules but not throttles.
placement Compute locality preference: nodes_first, nodes_only, cloud_first, cloud_only.
data_classification restricted keeps the call and the judge calls it makes on self-hosted nodes, with no cloud fallback; see Restricted data.
auto_route Enables smart model auto-selection; when effort is unpinned, evaluates task complexity via Judge.
effort_specified Signals caller explicitly pinned effort (skips dynamic Judge evaluation).

The response carries choices, token usage, resolved_model, resolved_vendor, matched_rule (empty on override), structured details (Choice objects with text, tool calls, and finish reasons), cached status, routing metadata: route_mode, placement, judge_score, and judge_model, and ignored_parameters: the sampling fields the resolved model did not get.

Sampling

temperature, top_p, stop and seed reach the model that serves the call, as far as its API takes them. A field the model does not take is left out of its request rather than failing the call, and the response names it, as it names a value the model got changed: ignored_parameters in gRPC (on the first chunk of a stream), the X-KTMS-Ignored-Params header over HTTP. Fallback models are judged one by one, so the list describes the model that answered.

Vendor Takes
openai, azure All four, up to 4 stop sequences. Reasoning models (an id starting with o<digit>, gpt-5, codex-mini or computer-use, as a whole or after a /, - or _, except gpt-5-chat) take none: they reject temperature, top_p and stop, and seed is not confirmed for them.
groq All four, up to 4 stop sequences.
google All four, up to 5 stop sequences.
ollama, local All four.
deepseek temperature, top_p, stop (up to 16).
mistral temperature (capped at 1.5), top_p, stop.
anthropic, Claude on bedrock stop, without sequences of whitespace alone. Claude 4.6 and earlier also take temperature (capped at 1) and top_p; from Claude 4 on, not both: with both set, only temperature. Claude 4.7 and later accept only the default sampling, so they get neither.
Other bedrock models temperature (capped at 1), top_p, up to 4 stop sequences.
bedrock application inference profiles (ARNs) Up to 4 stop sequences: the ARN does not say which model is behind it. With sampling declared, also temperature (capped at 1) or top_p, not both, which every family takes.

A temperature above a vendor's cap is lowered to it and reported as temperature. When a vendor takes fewer stop sequences than the call has, it gets the first ones and stop is reported. The model is told apart by its id. When the id misleads, declare a capability: no_sampling for a model whose API rejects a field this table sends, sampling for one that takes what its vendor's table row allows, such as an Azure deployment of a chat model named like a reasoning one. Sampling is part of the response cache key. Batch results do not carry ignored_parameters. Over HTTP, empty stop sequences are dropped before the call.

GenerateTextStream

Server-streaming RPC rpc GenerateTextStream(GenerateTextStreamRequest) returns (stream GenerateTextChunk).

  • Takes a GenerateTextStreamRequest containing the standard GenerateTextRequest.
  • Streams incremental GenerateTextChunk items containing delta (token text), tool_call_chunks, resolved_model, resolved_vendor, matched_rule, cached, route_mode, placement, judge_score, judge_model, ignored_parameters, and final finish_reason with token usage.
  • Fallback guarantee: If upstream models fail before the first chunk is sent, the router falls back to the next model in the rule's chain. Once the first chunk is transmitted to the caller, the stream is locked to that model to guarantee integrity.
  • Usage events and daily token budgets are settled on stream completion.
Batch text generation

For offline or bulk workflows (such as page generation, backfills, or data pipelines) that prioritize cost over latency, batch generation processes thousands of items asynchronously through vendor Batch APIs (e.g., Google Gemini Batch API) at a 50% discount compared to interactive calls. Jobs typically finish within 24 hours (vendor timeout at 48 hours).

  • Route resolution: Resolved once upon submission. The proxy selects the first model in the rule's chain that has the batch capability, a vendor key for the client (not needed for local models), and available daily budget. Once submitted, there is no vendor fallback; per-item outcomes are returned to the client.
  • Privacy: Prompts are streamed/submitted to the vendor and never persisted in the proxy database.
  • Throttling & Accounting: RPM limits do not apply to batches. Daily token budgets are checked at submission (rejecting if already exhausted) and tokens are deducted when final item results arrive. Results are retained for RESULTS_RETENTION_DAYS (default 7 days; accepts integer days or Go duration strings).
SubmitBatch

Takes a list of items (custom_id unique within the batch, and standard GenerateTextRequest), batch-level effort and attributes, and an optional client_batch_id for idempotency (resubmitting with the same client batch ID returns the existing batch):

grpcurl -plaintext -H "authorization: Bearer ktms_<key>" -d '{
  "effort": "EFFORT_LOW",
  "attributes": {"task": "catalog_enrichment"},
  "client_batch_id": "job-2026-10-07-001",
  "items": [
    {
      "custom_id": "item-1",
      "request": {
        "messages": [{"role": "MESSAGE_ROLE_USER", "text": "Extract attributes from product 1"}],
        "response_schema": {"type": "object", "properties": {"color": {"type": "string"}}, "required": ["color"]}
      }
    },
    {
      "custom_id": "item-2",
      "request": {
        "messages": [{"role": "MESSAGE_ROLE_USER", "text": "Extract attributes from product 2"}],
        "response_schema": {"type": "object", "properties": {"color": {"type": "string"}}, "required": ["color"]}
      }
    }
  ]
}' localhost:9090 ktms.v1.GatewayService/SubmitBatch

Returns a Batch with status BATCH_STATE_PENDING (or BATCH_STATE_RUNNING), total item count, model, and creation timestamp.

GetBatch

Polls batch state (PENDING, RUNNING, SUCCEEDED, FAILED, CANCELLED, EXPIRED) and progress counts (total, done, failed):

grpcurl -plaintext -H "authorization: Bearer ktms_<key>" -d '{
  "batch_id": "0199c0a0-6228-7e10-91b5-685d996cbfa1"
}' localhost:9090 ktms.v1.GatewayService/GetBatch
ListBatchResults

Pages through completed items with page_token (and next_page_token). Each item contains its custom_id and either a successful response (GenerateTextResponse with choices, usage, resolved_model, resolved_vendor, matched_rule) or an error (BatchItemError with gRPC code, message, and error reason):

grpcurl -plaintext -H "authorization: Bearer ktms_<key>" -d '{
  "batch_id": "0199c0a0-6228-7e10-91b5-685d996cbfa1",
  "page_size": 100
}' localhost:9090 ktms.v1.GatewayService/ListBatchResults
CancelBatch

Requests cancellation of an unfinished batch job at the vendor. The batch remains RUNNING until vendor confirmation is polled: already completed items preserve their results, while remaining unfinished items fail with reason batch_cancelled.

grpcurl -plaintext -H "authorization: Bearer ktms_<key>" -d '{
  "batch_id": "0199c0a0-6228-7e10-91b5-685d996cbfa1"
}' localhost:9090 ktms.v1.GatewayService/CancelBatch
SynthesizeSpeech, GenerateImage

SynthesizeSpeech takes text (read verbatim) and an optional language and returns OGG/Opus audio. GenerateImage takes a prompt and returns image bytes with a mime_type. Both accept attributes, action_id and model_override, and need the rules described above.

Judge

Judge answers typed questions about one state instead of writing text, so you get calibrated probabilities to branch on rather than prose to parse. All questions in a call are answered against the same state in parallel.

Type Question Answer
JUDGE_TYPE_NOUL Does this hold? probability of yes (noul, 0–1)
JUDGE_TYPE_CHOICE Which one? choice plus full probabilities and confidence
JUDGE_TYPE_SCORE How much? probability-weighted position on ordered levels
{
  "state_json": "{\"message\":\"My card was charged twice!\"}",
  "questions": {
    "angry": {"type": "JUDGE_TYPE_NOUL", "instructions": "Is the author upset?"},
    "topic": {"type": "JUDGE_TYPE_CHOICE", "instructions": "What is this about?",
              "options": {"billing": "payments and charges", "bug": "software defects"}}
  }
}

Multimodal state goes in state_media (text, image, audio, embedding parts), alongside or instead of state_json. Each media kind narrows the chain to judge models that also declare vision, audio_input or embedding_input; TypeSafe takes JSON state only. Image URLs must be https and are forwarded, not fetched. Guardrails inspect text parts only. Limits: 32 parts, 16 MiB of media, 16384 embedding dimensions.

Supported decision vendors are TypeSafe (jev-latest, billing input only) and OpenAI (gpt-6-luna, billing input and output tokens); both write no text and declare the judge capability.

Translation

Translate (HTTP POST /v1/translate, same JSON) translates a batch of texts. The client always makes the same call; which engine translates is the translate rule's choice — a local MT model on a node, any chat model by prompt, a cloud MT vendor — with the usual fallback, limits, guardrails and usage reporting. Comparing engines is a config change, not a code change.

{"texts": ["Great location, friendly staff.", "Chambre trop petite."],
 "source_language": "",          // BCP-47; empty detects each text
 "target_language": "de",
 "format": "text",               // or "html": tags and attributes are kept
 "attributes": {"pipeline": "reviews"}, "action_id": "reviews.translate"}

The reply holds one translations[] entry per text, in order (text, detected_source_language), with resolved_model, matched_rule and usage (tokens as the engine counts them, and characters). Limits: 128 texts and 256 KiB per call. Blank texts come back as they are, and a source_language equal to the target returns the texts without a call.

Models that translate declare the translate capability and the pairs they serve; a model without the request's pair is skipped, not tried. languages is required on a translate model ("*" for any). A tag covers its longer variants: "de" takes "de-AT", "zh-Hant" takes "zh-Hant-TW".

models:
  - id: opus-mt-de              # a node MT model (ktms-mt, see Run a node)
    vendor: local
    efforts: [EFFORT_LOW]
    capabilities: [translate]
    languages: {source: [en, fr, es, it, nl, pt, pl, ru, tr], target: [de]}
  - id: gemma4:26b              # any chat model, by prompt
    vendor: local
    efforts: [EFFORT_LOW]
    capabilities: [translate]
    languages: {source: ["*"], target: ["*"]}
  - id: gemini-2.5-flash
    vendor: google
    efforts: [EFFORT_LOW, EFFORT_MEDIUM]
    capabilities: [translate]
    languages: {source: ["*"], target: ["*"]}
    price_in_per_mtok: 0.30
    price_out_per_mtok: 2.50
rules:
  - name: translate             # local MT first, then the local LLM, then the cloud
    priority: 170
    attributes: {modality: translate}
    use: [opus-mt-de, gemma4:26b, gemini-2.5-flash]
  - name: translate-gemma       # a pipeline that asks for Gemma by an attribute
    priority: 171
    attributes: {modality: translate, engine: gemma}
    use: [gemma4:26b, gemini-2.5-flash]

A chat model translates through a prompt Keystone writes: the texts go as JSON data, the reply is structured output with one item per text, and Keystone checks the count and the ids before answering. A reply that does not line up is retried once on the same model, then the chain moves on. Batches are split into chunks of up to 32 texts.

Usage goes to light-tokenmeter like any call: action_id defaults to translate, tokens are reported as usual, the character count goes in meta.characters, and cost_nanousd adds price_per_million_chars (for engines billed by character) to the token price. A failed attempt bills its tokens only; the characters are billed once, by the translation delivered. Spend limits count both. ktms translate --to de [--from en] [FILE|-] translates a file line by line, and ktms route explain KEY --modality translate --from fr --to de shows which models a pair reaches.

Embeddings

Vector representations of text. Vector space isolation is strictly enforced: auto model routing and cross-model fallbacks are disallowed to prevent corrupting downstream vector indices. Models must declare the embedding capability.

grpcurl -plaintext -H "authorization: Bearer ktms_<key>" -d '{
  "model": "text-embedding-3-small",
  "inputs": ["Hello world", "Search document"]
}' localhost:9090 ktms.v1.GatewayService/GetEmbeddings
ListModels

Returns the models the calling key can use (those whose vendor it holds a credential for), with efforts, capabilities, rpm and prices.

OpenAI-compatible HTTP API

The data plane is also available as HTTP/JSON in the OpenAI wire format, so existing OpenAI SDKs, LangChain, curl and similar tools work by changing the base URL and key. It runs on HTTP_ADDR (default :8080) and shares everything with gRPC: the same ktms_… API keys (scope generate), vendor credentials, rules, fallbacks, throttling and usage reporting. Set HTTP_API_ENABLED=false to turn it off.

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key="ktms_<your key>")
reply = client.chat.completions.create(
    model="auto",                                # let the rules choose
    messages=[{"role": "user", "content": "Say hi in three words."}],
)
print(reply.choices[0].message.content, reply.model)   # `model` is the one that answered
curl -s localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer ktms_<your key>" -H "Content-Type: application/json" \
  -d '{"model":"auto","messages":[{"role":"user","content":"Say hi in three words."}]}'
Endpoints
Endpoint Maps to Notes
POST /v1/chat/completions GenerateText / GenerateTextStream Text, multimodal parts (image_url, input_audio, file/document), structured output, tools, and SSE streaming.
POST /v1/embeddings GetEmbeddings Vector embeddings with float or base64 encoding format and optional dimensionality.
POST /v1/audio/speech SynthesizeSpeech Returns OGG/Opus audio.
POST /v1/images/generations GenerateImage Returns b64_json.
POST /v1/decisions Judge OpenAI Decisions API format (predicate, choice, score). input is JSON state, or an array of input_text/text, input_image/image_url, input_audio and input_embedding parts for multimodal state.
POST /v1/decisions/rollouts EvaluateRollouts Batch trajectory and rollout candidate evaluation for agentic workflows with candidate ranking.
POST /v1/judge Judge Protobuf JSON of JudgeRequest / JudgeResponse.
POST /v1/translate Translate Protobuf JSON of TranslateRequest / TranslateResponse (see Translation).
GET /v1/models, GET /v1/models/{id} ListModels Only models the key holds a vendor credential for.

Authentication is Authorization: Bearer ktms_… (legacy llm_… keys are also supported). Response headers provide full observability:

  • X-KTMS-Vendor and X-KTMS-Rule: vendor and matching rule that served the request.
  • X-KTMS-Placement: infrastructure placement (nodes_first, nodes_only, cloud_first, cloud_only).
  • X-KTMS-Route-Mode: routing mechanism (direct, override, auto-judge, auto-default, auto-effort).
  • X-KTMS-Judge-Score and X-KTMS-Judge-Model: evaluation confidence and judge model ID when judge evaluates task complexity.
  • X-KTMS-Ignored-Params: the sampling fields the serving model did not get, comma-separated (see Sampling). It has no X-LLM-Proxy-* twin.

[!NOTE] Backwards Compatibility: All legacy X-LLM-Proxy-* response headers are dual-emitted alongside X-KTMS-* with standard RFC 8594 Deprecation: true, Sunset, and Warning headers to provide seamless zero-downtime migration for existing clients.

Choosing a model and placement

The model parameter supports smart auto-routing and infrastructure placement:

model Behaviour
auto (or empty) Smart auto-selection with default nodes_first placement. Evaluates prompt complexity via Judge (or uses reasoning_effort if provided).
auto:low, auto:medium, auto:high Pinned effort level with nodes_first placement.
auto:nodes-first, auto:nodes-only, auto:cloud-first, auto:cloud-only Auto-routing with explicit compute locality policy.
auto:<placement>:<effort> (e.g. auto:nodes-only:high) Combined placement policy and pinned effort level.
anything else Exact registry model ID (model_override): bypasses rules but not throttles.
Infrastructure Placement Policies
  • nodes_first (default): Prefers registered self-hosted compute nodes (VendorLocal, VendorOllama), falling back to cloud providers if unavailable or busy.
  • nodes_only (aliases: nodes, private, self_hosted): Restricts routing strictly to self-hosted nodes. Fails if no capable node is available. A preference of the request; for a guarantee that also covers the judge and holds per key or team, see Restricted data.
  • cloud_first: Prefers managed cloud providers, falling back to registered nodes.
  • cloud_only: Excludes self-hosted nodes completely.

Placement precedence:

  1. X-KTMS-Placement (or legacy X-LLM-Proxy-Placement) HTTP header
  2. Request body "placement": "..."
  3. Model suffix (e.g. model: "auto:nodes-only")
  4. Default: nodes_first
Fast Judge Auto-Selection

When model: "auto" is requested without an explicit effort level, the proxy evaluates prompt complexity using the configured Judge model (such as jev-latest or OpenAI Decisions) with a 250ms deadline. If the judge is not configured or times out, it safely falls back to standard medium effort without dropping the request.

Effort precedence:

  1. Pinned effort in model string (e.g. auto:low)
  2. reasoning_effort request field (low, medium, high) — skips judge evaluation
  3. Dynamic Judge evaluation (fails open to medium)

Routing attributes (what rules match on, and what usage events carry) come from OpenAI's own fields: metadata is passed through as attributes, user becomes attributes["user_id"] (unless metadata.user_id is set), and metadata.action_id is used as the usage action label.

{"model": "auto:nodes-first", "user": "u-42", "metadata": {"subject": "french", "action_id": "app.quiz"},
 "messages": [{"role": "user", "content": "Translate: good morning"}]}
Chat completions: what is supported
Request field Support
messages system/developer messages become the system instruction, user/assistant the conversation. Content is a string or text parts.
max_tokens, max_completion_tokens Output ceiling (max_output_tokens). Set one: an unbounded answer is the costliest failure.
response_format json_schema is enforced: type (incl. number and ["string","null"]), properties, items, required, enum, format and boolean additionalProperties are carried. Enums on non-string types, and formats a vendor rejects, reach the model as a hint in the description. Other keywords (minimum, pattern, oneOf, ...) are dropped. json_object asks for JSON in the prompt.
stream Real incremental SSE streaming backed by gRPC server streaming. Once the first chunk is emitted, stream is locked to the selected model; stream_options.include_usage is honoured on completion.
reasoning_effort, user, metadata Routing, as above.
enable_google_search Extension: let a Google model ground the answer in search.
n Must be 1.
tools, tool_choice Supported. Function declarations with JSON Schema parameters. tool_choice supports "auto", "none", "required", or specific function names.
role: "tool" Supported. Tool responses with tool_call_id.
Image/audio content parts Rejected with 400.
temperature, top_p, stop, seed Passed to the model as far as it takes them; the rest is named in X-KTMS-Ignored-Params (see Sampling). stop is a string or an array.
logprobs, penalties… Accepted and ignored.

The response is a standard chat.completion with finish_reason ("stop", "tool_calls", "length", or "content_filter") and token usage.

Speech and images

POST /v1/audio/speech takes model, input and an optional language extension (pronunciation hint, e.g. "Serbian"); voice, response_format and speed are ignored. The body of the response is OGG/Opus whatever format was requested; check the Content-Type.

POST /v1/images/generations takes model and prompt; n must be 1 and response_format: "url" is rejected (the image is returned as data[0].b64_json).

Both need rules for the tts / image modality.

Errors

Errors use OpenAI's shape, {"error": {"message", "type", "param", "code"}}, and the proxy's error contract shows up in code:

HTTP type code examples
400 invalid_request_error output_truncated, no_capable_model, unsupported_language_pair, no_vendor_key, not_configured; validation messages
401 / 403 authentication_error / permission_error missing or invalid key, missing scope
429 rate_limit_error throttled:rpm, throttled:key_rpm, throttled:key_tpm, throttled:budget_key, throttled:budget_user (with Retry-After)
429 insufficient_quota insufficient_quota; the message names the cap, e.g. throttled:spend_key_daily. Retry-After is the seconds until the period resets (UTC midnight or month start).
501 server_error ffmpeg_missing (Gemini speech synthesis invoked without ffmpeg on PATH or in container)
502 server_error vendor_error
503 server_error vendors_unavailable, auth_unavailable, storage_unavailable, throttled:spend_unavailable (with SPEND_LIMITS_FAIL_MODE=closed): retry with backoff

OpenAI SDKs retry 429 and 5xx automatically; for throttled:* and vendor_error you may want to disable that (max_retries=0) and handle them yourself.

Anthropic Messages API

POST /v1/messages serves the Anthropic Messages API on HTTP_ADDR, so Claude Code and the Anthropic SDKs run through Keystone. It is the same pipeline as /v1/chat/completions: the same keys (scope generate), rules, fallback, limits, guardrails, cache, X-KTMS-* headers and usage events. The model that answers may be any vendor's, and the answer is still a Messages response. The Messages API is experimental in 1.x.

Claude Code, with a KTMS key:

export ANTHROPIC_BASE_URL=http://localhost:8080
export ANTHROPIC_AUTH_TOKEN=ktms_<your key>        # or ANTHROPIC_API_KEY
export ANTHROPIC_MODEL=auto                        # or a registry model id
export ANTHROPIC_DEFAULT_HAIKU_MODEL=auto:low      # Claude Code's background calls
claude

Claude Code warns once that it does not recognize the model auto; that is expected. It asks for up to 32,000-64,000 output tokens, which Keystone passes on as max_tokens; if a model your rules pick caps output lower, set CLAUDE_CODE_MAX_OUTPUT_TOKENS to fit it.

The Anthropic SDKs need only the base URL and the key:

import anthropic

client = anthropic.Anthropic(base_url="http://localhost:8080", api_key="ktms_<your key>")
reply = client.messages.create(model="auto", max_tokens=1024,
                               messages=[{"role": "user", "content": "Say hi in three words."}])
print(reply.content[0].text, reply.model)   # `model` is the one that answered
Request field Support
model As on chat completions: auto, auto:<effort>, auto:<placement>, or a registry model id.
system A string or text blocks, joined into the system instruction.
messages user and assistant turns with text, image and document blocks (base64 or https url sources; text and content documents become text), tool_use and tool_result (text and images). thinking, redacted_thinking and server-tool blocks in assistant turns are dropped: another model cannot continue them.
tools, tool_choice Client tools with input_schema (the same JSON Schema subset as response_format); tool_choice auto, any, tool and none. Server tools such as web_search are rejected with 400.
max_tokens Required, the output ceiling.
stream SSE events message_start, ping, content_block_start, content_block_delta (text_delta, input_json_delta), content_block_stop, message_delta and message_stop. message_start carries an input token estimate; message_delta carries the vendor's counts.
metadata.user_id attributes["user_id"], as user on chat completions; other metadata keys are attributes, and action_id is the usage action label. Claude Code's user_id names a session: Keystone keeps its account (or, without one, its device) as the user and puts the session in attributes["session_id"], so per-user limits and reports span sessions.
enable_cache, placement Extensions, as on chat completions; so are the X-KTMS-Cache and X-KTMS-Placement request headers (set them in Claude Code with ANTHROPIC_CUSTOM_HEADERS).
temperature, top_p, stop_sequences Passed to the model as on chat completions (see Sampling). A stop sequence ends the answer with stop_reason end_turn.
top_k Accepted, sent to no model, and named in X-KTMS-Ignored-Params.
thinking, service_tier Accepted and ignored.

The key goes in x-api-key or Authorization: Bearer. The response's stop_reason is end_turn, tool_use, max_tokens or refusal, and usage splits cache_read_input_tokens and cache_creation_input_tokens out of input_tokens. Tool call ids are the vendor's, or toolu_… when the vendor gives none.

POST /v1/messages/count_tokens answers with the estimate the token budgets reserve, not a tokenizer's count: about four characters per token, tool definitions included, a flat allowance per image or document (url sources are not fetched), and the X-KTMS-Token-Count: estimate header.

Errors use the Messages shape, {"type": "error", "error": {"type", "message"}}, with the HTTP statuses of the table above: invalid_request_error (400), authentication_error (401), permission_error (403), not_found_error (404), rate_limit_error (429, with Retry-After), overloaded_error (503), timeout_error (504) and api_error for the rest. The machine-readable reason, such as throttled:rpm, is the message. An error after the stream has started arrives as an error event.

MCP gateway

Keystone is also a gateway for MCP servers. Claude Code, Cursor and other MCP clients reach the servers you register through Keystone, with a KTMS key: the gateway holds each server's credential, shows a key only the servers and tools it may use, limits calls per tool and accounts for every call in light-tokenmeter and Prometheus. The MCP gateway is experimental in 1.x.

  • /mcp aggregates every server the key may use; tools are named server__tool.
  • /mcp/{server} is one server, with its tools named as the server names them.

Both are Streamable HTTP on HTTP_ADDR, stateless, so any replica answers any request. Set MCP_GATEWAY_ENABLED=false to turn them off. Upstream servers may speak Streamable HTTP or SSE. Only tools are proxied; resources and prompts are not.

# register a server and give it the header the gateway sends it
ktms mcp-servers set github --url https://api.githubcopilot.com/mcp/ --tool-rpm 60
echo "Bearer $GITHUB_TOKEN" | ktms mcp-servers credential set github
ktms mcp-servers tools github          # what it offers, as allowed_mcp entries

# a key for the agent: the mcp scope, two tools of github and all of docs
ktms keys mint coding-agent --scopes mcp --allowed-mcp github/create_issue,github/search_code,docs

# Claude Code
claude mcp add --transport http ktms https://ktms.example.com/mcp --header "Authorization: Bearer ktms_<key>"
  • Servers are MCPServerSpec: name (1-32 of a-z, 0-9, -), url, transport (streamable_http or sse), enabled, allow_private, timeout_ms (one call, default 60 s), max_response_bytes (one result, default 4 MiB) and tool_rpm. Manage them with UpsertMCPServer and DeleteMCPServer, ktms mcp-servers, the mcp_servers section of a configuration file or the console. GetConfig lists them.
  • Credentials: SetMCPCredential stores a header and its value (Authorization: Bearer ... by default), encrypted with the vendor keys' keyring and re-encrypted by RotateEncryption. The gateway adds it to every request it sends the server. It is never returned, logged, put in usage events or passed to clients; GetConfig shows its header and fingerprint. In a file it is mcp_credentials with value_env.
  • Access: a key needs the mcp scope. allowed_mcp on the key's policy and on its team lists servers (github) and single tools (github/create_issue); empty allows any, and a tool must pass both lists. Others are not listed, and calling one is the JSON-RPC error -32003 permission_denied (/mcp/{server} for a server outside the lists is HTTP 403); the upstream is not called.
  • Addresses: a server whose host is or resolves to a loopback, private, link-local or metadata address is refused at registration and at every connection, unless allow_private is set for a server on your own network. http:// is allowed only then. The gateway ignores HTTPS_PROXY for these connections.
  • Limits and failures: above tool_rpm (per client key and tool, needs Redis), past the timeout, with a result over max_response_bytes or with the server down, the call returns a tool error (isError) that says so, for the model to act on. A server that cannot list its tools is left out of /mcp, not the others.
  • Accounting: each forwarded call is a usage event with vendor mcp, model server__tool, no tokens, its duration, status and tool_call (server, tool, result size); user_id comes from the X-KTMS-User header, or is svc:<key name>. Metrics: ktms_mcp_tool_calls_total, ktms_mcp_tool_call_duration_seconds, ktms_mcp_response_bytes.
Agents

Agents of different people (Claude Code, Codex and others) talk to each other through the gateway's built-in server agents. Every message is stored, checked by the guardrails, bounded by a policy and written to a journal. The agents' inbox is experimental in 1.x.

An agent is a client key with the mcp scope and a card. Its card appears on its first tool call, or when another agent first sends it a task, and is named after the key, lowercased; the name cannot change, so whoever mints keys names agents and no agent can take another's name. register_agent describes it. A key that may only talk to agents has allowed_mcp: [agents]:

ktms keys mint alice-agent --scopes mcp --allowed-mcp agents --owner alice@example.com
claude mcp add --transport http ktms https://ktms.example.com/mcp/agents --header "Authorization: Bearer ktms_<key>"

Clients. KTMS does not wake an agent: each turn calls inbox (or outbox for answers to its own tasks). ktms agents run takes the turns for an agent that answers tasks:

KTMS_AGENT_KEY=ktms_<key> ktms agents run --runner claude --dir ~/src/ktms --http https://ktms.example.com

It waits on the inbox (since and wait) over an outgoing connection, so the machine may sit behind NAT and a task is taken as soon as it is sent; a KTMS before 1.6, which cannot wait, is polled every --interval (15s). Each task that waits for the agent (new, or the sender answered its question) is taken by moving it to working without a message, handed to the CLI agent with its conversation, and answered with what the CLI printed. The CLI ends with STATUS: completed, STATUS: input-required (a question for the sender), STATUS: needs_owner (a question for its own person) or STATUS: failed; a run that fails, prints nothing or outlasts --task-timeout (30m; agy takes 15–20 minutes over a 50 KB diff) fails the task without passing the CLI's errors on. A task a guardrail holds waits for its person and is not taken.

  • The presets are read-only: claude with only Read, Grep and Glob and no MCP servers, codex in its read-only sandbox, agy in plan mode. Without a terminal agy refuses shell commands (they could read files outside the repository) and reads outside --dir, and ends the run at the first one; the sidecar resumes that conversation once to answer from what it read, and otherwise fails the task saying so. Put what agy would need a command for, such as the diff, in the message. --cmd runs your own program with the prompt on stdin. --model picks the preset's model, such as --runner agy --model gemini-3.8-flash-high. Paths under --dir are made relative in the answer.

  • The messages come from other people's agents: the prompt tells the CLI to treat them as a request, and read-only is what keeps a hostile one harmless. Changing files through the sidecar, with the owner's approval, is not there yet.

  • --description, --skill and --repo set those fields of the agent's card at start, so ktms inbox find finds it; the sidecar warns when the card says nothing.

  • Tasks are answered one at a time; run one ktms agents run per agent key. While --pause-file exists no task is taken. --once answers what is due and exits, for cron.

  • Claude Code works interactively after claude mcp add. A headless turn needs the server's tools allowed, here with the server named agents in an MCP config file that reads the key from the environment:

    # agents.json: {"mcpServers":{"agents":{"type":"http","url":"https://ktms.example.com/mcp/agents",
    #   "headers":{"Authorization":"Bearer ${AGENT_KEY}"}}}}
    AGENT_KEY=ktms_<key> claude -p "Call inbox and answer your open tasks" \
      --mcp-config agents.json --strict-mcp-config --allowedTools "mcp__agents"
    
  • agy (Google Antigravity CLI) stores the header, and so the key, in its configuration in plain text. Without a terminal it refuses MCP calls unless --settings allows them:

    agy mcp add --header "Authorization: Bearer ktms_<key>" ktms-agents https://ktms.example.com/mcp/agents
    agy --settings '{"permissions":{"allow":["mcp(ktms-agents)","mcp(ktms-agents/*)"]}}' \
      -p="Call inbox and answer your open tasks"
    

    Shell commands stay refused in that mode whatever --settings allows; give a reviewing agent the diff as a file (--add-dir) rather than asking it to run git.

Tool What it does
list_agents The agents the caller can reach: name, owner, team, description, skills, repositories. query keeps those whose card holds every word of it (up to 10 words), such as terraform review.
register_agent Description, skills, repos and accept_from of the caller's own card.
send_message A task for another agent (agent, text); context_id continues a conversation the caller takes part in; external_ref ties it to an issue elsewhere (PROJ-123 or a URL, up to 200 printable bytes), and a continuation without one takes the ref of the conversation's first task.
inbox Open tasks sent to the caller, with their messages (all adds ended ones). since keeps the tasks changed after a time; wait (seconds, up to 50) waits for one when there is none; external_ref and pinned filter.
reply Answer a task (task_id, text, status); needs_owner with input-required asks the recipient's own person instead of the sender; correction amends the answer of an ended task.
get_task A task the caller sent or received, with its messages.
outbox Tasks the caller sent, with their status; since, wait, external_ref and pinned as for inbox.
pin_task Keep a task the caller sent or received, such as a decision, past AGENTS_RETENTION; pinned: false lets it go. Each side pins for itself, up to AGENTS_MAX_PINNED tasks.

Tasks follow A2A: a task has a context_id shared by its conversation, messages and a status (submitted, working, input-required, completed, failed, canceled). Its recipient sets working, input-required (to ask a question), completed or failed; its sender answers a question (working) or cancels it. An ended task takes no more messages.

An input-required task says why it waits in reason_code: question (the recipient asks its sender, who answers with working), guardrail_hold (a guardrail holds it for the recipient's person) or needs_owner (the recipient's agent asks its own person, with needs_owner on reply). In the last two the sender can only cancel. ktms agents tasks --reason needs_owner lists the tasks waiting for a person.

Owners and corrections. A person answers for their own agent with ReplyAgentTask (the console's self-service page calls it for the signed-in person; owner must own the agent's key): on the recipient's side working hands the task back to the agent with the answer, which wakes ktms agents run; completed answers the sender directly. Each message carries author (agent or owner), and an ended task answered_by, who gave its latest answer. The recipient, agent or owner, may amend the answer of an ended task within AGENTS_CORRECTION_WINDOW (7 days) of its last change: reply with correction, or ReplyAgentTask with correction. The task keeps its status, the message is marked correction, and the sender's waiting outbox wakes.

From a terminal. ktms inbox acts as one agent with its key (KTMS_AGENT_KEY or --agent-key-file) over the same MCP server, for the agent's person or a script:

export KTMS_AGENT_KEY=ktms_<key> KTMS_HTTP=https://ktms.example.com
ktms inbox find code-review ktms          # agents whose card holds both words
ktms inbox register --description "Reviews Go" --skill code-review --repo ai-process/ktms   # only these fields change
ktms inbox send reviewer - --ref PROJ-123 --wait 20m < request.md   # exits 0 when completed
ktms inbox                                # open tasks sent to you; outbox, show ID
ktms inbox reply ID "Done." ; ktms inbox pin ID

Waiting instead of polling. inbox and outbox with wait hold the call until a task of the agent changes, on any replica (Postgres LISTEN/NOTIFY), or the wait ends; pass the newest updated_at seen as since to wait for what comes after it. The wait is capped at 50s and 5s under HTTP_API_TIMEOUT.

  • Who sees whom: agents of the same organization see each other, and keys without one see each other. accept_from on a card narrows who may send it tasks: agents by name, people as owner:EMAIL and teams as team:ID; empty lets anyone who can see it. A key of another organization is never reached, nor its card written.
  • Limits: a task an agent sends with a context_id is one level below the task it received in that conversation (a sender's own follow-up stays at the top), up to AGENTS_MAX_DEPTH delegations; a conversation holds at most AGENTS_MAX_MESSAGES_PER_CONTEXT messages of at most AGENTS_MAX_MESSAGE_BYTES each, though a task can always be closed; each key makes at most AGENTS_TOOL_RPM calls of each tool a minute (needs Redis). A refusal is a tool error that says why.
  • Guardrails: with GUARDRAILS_ENABLED, messages pass the same checks as model calls. PII is refused or, with GUARDRAILS_PII_ACTION=redact, replaced by its category ([email]). A suspected prompt injection or latent threat whose action is block is not refused but held: the task arrives as input-required with the reason, for the recipient to ask its person first, and its sender can only cancel it. A flagged reply keeps its status and carries the reason (guardrail note: ...) for the other side.
  • Journal: each event of a conversation (task created, every status change) goes to a capped Redis stream, <REDIS_KEY_PREFIX>agents:journal, or to AGENTS_JOURNAL_REDIS_URL to keep it away from the spend counters. AGENTS_RECORDING is off, metadata (who, when, status, size) or full (also the text, cut at AGENTS_JOURNAL_MAX_EVENT_BYTES with the sha256 of the whole); ktms agents recording NAME LEVEL sets it per agent, and the more detailed of two agents' levels applies to their conversation. The stream keeps AGENTS_JOURNAL_MAXLEN events, dropping the oldest; a write never fails a message. Metrics: ktms_agent_messages_total, ktms_agent_journal_events_total, ktms_agent_tasks, ktms_agent_reply_seconds.
  • Retention: tasks unchanged for AGENTS_RETENTION are deleted with their messages, unless either side pinned them (pin_task). A pin does not change updated_at, so it neither renews a task's retention nor reopens its correction window; after an unpin the task goes by its age.
  • Operators see the cards and conversations with ListAgents, ListAgentTasks (both with admin:read or agents:read), GetAgentTask (the messages: agents:read or admin, never admin:read) and ktms agents (ktms agents tasks AGENT --ref PROJ-123 --pinned); an auditor gets an agents:read key (ktms keys mint auditor --scopes agents:read), which reads conversations and nothing else; a key's owner (MintAPIKey, --owner) is how ktms-admin lists a person's own keys and agents.
ktms agents list --owner alice@example.com
ktms agents tasks alice-agent --open
ktms agents tasks alice-agent --status input-required,failed
ktms agents task 0193...

--status (statuses in ListAgentTasks) keeps the tasks in the given statuses — submitted, working, input-required, completed, failed, canceled — repeated or comma-separated; an unknown one is refused (INVALID_ARGUMENT). With --open, only the open statuses among them count.

A2A. Agents that speak A2A (JSON-RPC over HTTP, protocol 0.3) rather than MCP reach the same inbox: POST /a2a/{agent} is an agent's A2A endpoint and GET /a2a/{agent}/.well-known/agent-card.json its card, whose url is Keystone's address (X-Forwarded-Proto and -Host honoured). Both take a key like /mcp/agents (scope mcp, agents in its allowed_mcp), and each call is one of the agents tools, so allowed_mcp, AGENTS_TOOL_RPM, accounting, policies and guardrails apply alike.

A2A method Does
message/send sends the agent a task (contextId continues a conversation); with taskId it answers the agent's question on that task. configuration.blocking waits up to 25s for the task to end or ask.
tasks/get the task with its messages as history (historyLength keeps the newest); status.message is the agent's last message.
tasks/cancel cancels a task the caller sent.

Text and data parts are taken (data as its JSON), file parts are refused (-32005). Streaming and push notifications are not offered (-32004): poll tasks/get.

Turn the inbox off with AGENTS_ENABLED=false. An upstream registered as agents is refused.

Concepts

Effort is the only routing field the proxy understands; everything else about a request's purpose travels in free-form attributes, so the proxy knows nothing about your product.

Throttling & Wait Buffer:

  • GCRA Rate Limiting: Enforces requests-per-minute (rpm) via the Generic Cell Rate Algorithm in Redis, with configurable burst capacity (rpm_burst, defaulting to rpm - 1 to preserve classic fixed-window behavior, < 0 to disable burst). Key limits and user limits are evaluated atomically.
  • User & Model Overrides: Configure fine-grained limits per user and model with SetUserRateLimit (supports * wildcards for user-wide or model-wide defaults).
  • Spend Limits: Hard USD caps per API key and per end user (UTC day or month), reserved atomically before each call and settled to the actual cost; see Spend limits.
  • Daily Token Budgets: Limits tokens per API key (daily_tokens_per_key) and per end-user (daily_tokens_per_user, UTC day). A configurable grace margin (budget_grace_margin, default 5%, < 0 to strictly disable) absorbs requests that exceed the budget on their last call without refusing them mid-stream.
  • Inference Pool Wait Buffer: For local node pools, requests waiting for free slots use jittered backoff polling up to queue_timeout_ms. Interactive requests (non-service callers) are automatically capped to 250ms to safeguard Time-To-First-Token (TTFT). Little's Law predictive admission evaluates the queue depth (KTMS's own leases plus other clients' requests waiting in the engine: what the heartbeat saw waiting beyond KTMS's leases on that node; node selection weighs the same load) and estimated duration, immediately falling back to the next model in the chain if expected wait time exceeds the timeout. A slot lease is renewed for as long as its call runs, unary or streaming, so a long generation never frees a slot the node is still using.
  • A throttled model is skipped in the fallback chain; if none of the chain is available the caller gets RESOURCE_EXHAUSTED.

Upstream Circuit Breaker:

  • Monitors consecutive upstream failures (5xx server errors, network connection drops, connect/TLS handshake timeouts, and upstream timeouts via UPSTREAM_REQUEST_TIMEOUT) per (vendor, model). Caller client aborts and overall client deadline cancellations do not trip the circuit breaker, but vendor stalls beyond UPSTREAM_REQUEST_TIMEOUT (total duration for unary/media, per-chunk idle watchdog for streaming) reliably trip it and trigger immediate fallback to the next candidate model.
  • When consecutive failures reach CIRCUIT_BREAKER_FAIL_THRESHOLD (default 5), the circuit trips to OPEN.
  • Requests skip degraded upstream models immediately, without a network round trip.
  • After CIRCUIT_BREAKER_COOLDOWN (default 30s), transitions to HALF_OPEN and admits a single probe request. Success restores CLOSED; failure re-opens the circuit.
  • If all models in the chain have open circuits, the caller receives UNAVAILABLE vendors_unavailable.

Vendor back-off (Retry-After):

  • Vendor SDKs run with their built-in retries off, so one router attempt is exactly one vendor call. The router retries a 429, 5xx or transport failure (connection reset, EOF on a reused connection) once on the same model before falling back, for text, media and streaming calls (a stream only until its first chunk).
  • The vendor's wait is read from Retry-After or retry-after-ms (OpenAI-compatible APIs, Azure, Anthropic) and from google.rpc.RetryInfo (Gemini). Bedrock throttling carries no wait, so it is retried and falls back as before.
  • A wait of up to 1s is honoured in place before the same-model retry. A longer one on a 429 or 503 pauses the model for that client's vendor credential, up to VENDOR_COOLDOWN_MAX, and the chain moves on. Clients sharing one vendor key share the pause; other credentials are unaffected. With pauses off, the ordinary same-model retry applies. Shadow traffic respects pauses but never starts one.
  • Paused models are skipped without a network round trip (ktms_fallbacks_total{reason="cooldown"}; pauses started are counted in ktms_vendor_cooldowns_total). Pauses live in each replica's memory, like circuit breakers, so each replica learns of a pause from its own first rejected call.
  • If every model in the chain is paused, the caller receives UNAVAILABLE vendors_unavailable with a gRPC RetryInfo for the soonest pause to end; the OpenAI-compatible HTTP API returns it as a Retry-After header. When any model failed for another reason, no wait is promised: it may serve the next request.
  • Local node models are never paused: the node pool has its own admission control.

Usage reporting. When USAGE_COLLECTOR_HOST is set, every call emits an event (vendor, model, tokens, duration, status, action_id, rule name and your attributes as metadata) to a gRPC service implementing usage-proto/usage/v1/usage.proto. Events are attributed to the project named by the usage_project attribute, falling back to the API key's name. Any service implementing RecordUsageEvent(s) works as a sink. When light-tokenmeter has authentication on, give KTMS a token with the write scope in USAGE_COLLECTOR_TOKEN; a refused token is logged as an error and events are held and retried.

Restricted data

data_classification: restricted keeps a call on your self-hosted nodes: no cloud vendor sees it, and when the nodes fail there is no fallback to the cloud. Set it on a request, or on a key's policy or a team, where it holds for every call of the key whatever the request says:

ktms keys mint scribe --team clinic/notes --data-classification restricted
ktms keys policy set billing --data-classification restricted   # unrestricted lifts it
organizations:
  - name: clinic
    teams:
      - name: notes
        data_classification: restricted   # every key of the team

SetAPIKeyPolicy and UpdateTeam replace the rest of the policy or team, but keep the current data_classification when the field is empty: a client that predates it, such as an older console, cannot lift a restriction by leaving it out. unrestricted lifts it, and the configuration file does that when the field is removed. ktms keys list shows a key's restriction, or its team's as restricted (team).

For a single call, set data_classification: restricted in the request (GenerateTextRequest, data_classification on /v1/chat/completions), or send the X-KTMS-Data-Classification: restricted header over HTTP or x-ktms-data-classification gRPC metadata, which also covers embeddings, judgments, speech and images. A request can restrict a call, not lift its key's or team's restriction; a value other than restricted or empty is INVALID_ARGUMENT. A restricted call:

  • routes as placement: nodes_only: the chain keeps the node models (VendorLocal, VendorOllama) and is FAILED_PRECONDITION no_capable_model without one; when every node fails it is UNAVAILABLE vendors_unavailable. nodes_first and cloud_first become nodes_only, cloud_only is INVALID_ARGUMENT, and a model_override of a cloud model is FAILED_PRECONDITION.
  • asks the auto-effort judge and the shadow evaluation judge only on nodes, and falls back to the default effort, or skips the evaluation, when no node can judge.
  • skips the response cache, so its answer cannot be served to another key.
  • for embeddings, judgments, speech and images is FAILED_PRECONDITION without a node model; batches go to vendors' batch APIs and need a node model too, or are refused.
  • carries data_classification: restricted in its usage event's meta; callers cannot set that attribute themselves.

Nodes are models of vendor local (registered ktms-nodes) and ollama, an Ollama endpoint you configure: point ollama models only at servers you run. Restricted data also does not cover what is sent outside the model chain: LATENT_ENCODER_URL (latent guardrails and the semantic cache) and MCP servers. Point the encoder at your own infrastructure.

Semantic cache and latent guardrails

Both project text into a representation space with the encoder at LATENT_ENCODER_URL (a JEPA or any embedding model behind an OpenAI-compatible /v1/embeddings), falling back to a local, lexical hash encoder that folds case, homoglyphs, zero-width and spaced-out letters.

  • Semantic cache. After an exact-cache miss, a request with enable_cache compares its final user message (or request_instruction when there are no messages) with earlier ones whose everything else — key, config version, system prompt, history, tools, effort, attributes and every number in the message — matches exactly. A hit at SEMANTIC_CACHE_SIMILARITY_THRESHOLD returns cached: true and usage metadata cache_match=semantic with cache_similarity. Prompts containing PII and responses with tool calls are never cached; with guardrails on, the message is screened before a near match is served. Vectors and responses are encrypted at rest.
  • Latent guardrails. Each prompt is scored against category centroids; the nearest one at GUARDRAILS_LATENT_THRESHOLD blocks (or logs) with guardrail_violation: latent <category> intent detected. An unreachable encoder fails open after the regex checks have run.
  • Overhead. The local encoder runs in-process; measure screening and semantic lookup cost on your hardware with go test -bench . ./internal/latent ./internal/guardrails ./internal/cache. A remote encoder adds its own latency, bounded by LATENT_ENCODER_TIMEOUT.

Error contract

RPC status codes
gRPC status Reason What to do
NOT_FOUND – Unknown batch ID.
INVALID_ARGUMENT guardrail_violation Blocked due to detected prompt injection, PII, or a latent-space threat match. Clean input before retrying.
INVALID_ARGUMENT bad_request: <message> Empty batch, duplicate custom_id, too many items (> BATCH_MAX_ITEMS), or invalid parameters.
UNAVAILABLE vendors_unavailable The whole chain failed: retry with backoff. When vendors paused every model in the chain with Retry-After, details carry RetryInfo (HTTP: Retry-After header): wait that long first.
UNAVAILABLE auth_unavailable, storage_unavailable Postgres is unreachable for a key check (past AUTH_STALE_IF_ERROR) or a batch or admin write. Retry with backoff.
RESOURCE_EXHAUSTED throttled:rpm, throttled:budget_key, throttled:budget_user Back off; don't hot-retry. Daily budget exhausted before batch submission.
RESOURCE_EXHAUSTED throttled:key_rpm, throttled:key_tpm The key's own rpm_limit or tpm_limit. Details carry RetryInfo: wait that long.
FAILED_PRECONDITION request_too_large The call needs more tokens than a minute of the key's tpm_limit; it never fits. Shrink it or raise the limit.
RESOURCE_EXHAUSTED throttled:spend_key_daily, throttled:spend_key_monthly, throttled:spend_user_daily, throttled:spend_user_monthly, throttled:spend_team_monthly Spend limit reached. Details carry ErrorInfo{reason: "insufficient_quota"} and RetryInfo until the period resets.
UNAVAILABLE throttled:spend_unavailable Spend counters unreachable with SPEND_LIMITS_FAIL_MODE=closed. Retry with backoff.
PERMISSION_DENIED model "<id>" not allowed for <key|team>, no models in chain allowed for <key|team>, model access denied by key or team policy (batch) The key's or its team's allowed_models excludes that model.
FAILED_PRECONDITION output_truncated Raise max_output_tokens; don't retry as-is.
FAILED_PRECONDITION no_capable_model No model in the matched chain can serve this request, modality, or batch capability.
FAILED_PRECONDITION unsupported_language_pair Translate: models of the chain can translate, but none declares this pair. Add a model for it or a * fallback.
FAILED_PRECONDITION no_vendor_key The client key holds no credential for the chain's vendors. local models never cause this.
FAILED_PRECONDITION not_configured No models/rules configured yet.
FAILED_PRECONDITION batch not configured Batch processing is not enabled/configured on the proxy (no batch store).
UNIMPLEMENTED ffmpeg_missing Gemini speech synthesis invoked without ffmpeg on PATH or running slim container. Install ffmpeg or switch to :audio image.
INTERNAL vendor_error Terminal vendor failure.
UNAUTHENTICATED / PERMISSION_DENIED – Missing/invalid key, or key lacks the scope.

When to retry: UNAVAILABLE with backoff, and RESOURCE_EXHAUSTED after the wait it carries (RetryInfo, HTTP Retry-After) or with backoff when it carries none. Every other code fails the same way until the request or the configuration changes.

Batch item error reasons

Items returned in ListBatchResults that failed have an error containing a gRPC code, a message, and one of these reasons:

Reason When What to do
output_truncated Item reached max_output_tokens or candidate finish limit. Re-submit item with higher max_output_tokens.
vendor_error Vendor error occurred during processing or structured schema validation failed. Retry synchronously or resubmit in another batch.
batch_expired Vendor batch expired (after 48 h) before the item finished. Re-submit the item.
batch_cancelled Batch was cancelled before the item completed. Re-submit if still needed.

API stability

From 1.0, KTMS follows semantic versioning: within 1.x nothing in the public API below changes incompatibly. Breaking changes wait for 2.0, except the one removal announced before 1.0 and listed under Deprecated. Minor releases add features; patch releases fix bugs.

What is public
Surface What stays compatible in 1.x
gRPC ktms.v1 (GatewayService, AdminService, NodeRegistryService) No RPC, field, enum value or message is removed or renamed, and no field number or type changes. New RPCs and fields may appear. buf breaking checks every pull request.
HTTP /v1 (OpenAI-compatible) and X-KTMS-* headers Paths, request and response fields, and header names, except the experimental ones listed below. New fields and endpoints may appear.
Probes and metrics endpoints /healthz and /readyz on HTTP_ADDR, /metrics and /healthz on METRICS_ADDR.
Error contract The status codes and reason strings in Error contract and the HTTP Errors table keep their meaning. New reasons may appear, so treat unknown ones like their status code.
Environment variables Names, defaults and meaning of every variable in Configuration reference.
Database Any 1.x release upgrades the database of an earlier one by itself (AUTO_MIGRATE, forward-only). No manual SQL steps. The tables are not an API: read and write through the RPCs.
Prometheus metrics Names and labels of ktms_* metrics, except the experimental ones listed below.
Usage events (usage.v1) Field numbers and meaning, checked by buf breaking like ktms.v1.
Node agent A 1.x agent works with any 1.x gateway, in both directions.
Helm chart (oci://ghcr.io/ai-process/charts/ktms) Value names and meaning; new values may appear. The chart version is the release version.

Not public, and free to change in any release: Redis key layout, log message text, the internal/ Go packages, CLI output of the tools in cmd/ other than their flags, and benchmark numbers.

Experimental

These work and are supported, but may change in a minor release. The change is then listed in the CHANGELOG. In the proto they carry an Experimental: comment.

  • The configuration file format (api_version: ktms.ai-process.dev/v1alpha1) and the ktms command, until the format reaches v1.

  • The judge's effort choice: when a request is auto-routed without a pinned effort (model: "auto" or auto:<placement> over HTTP without reasoning_effort; auto_route with effort_specified false in gRPC), a judge picks the effort. Its choice, the auto-judge and auto-default values of route_mode, judge_score, judge_model and the X-KTMS-Judge-Score and X-KTMS-Judge-Model headers are experimental. Auto-routing with a pinned effort (auto:<effort>, auto:<placement>:<effort>, route mode auto-effort), placement policies, model_override and the X-KTMS-Route-Mode header are stable.

  • Shadow traffic and its evaluation: the shadow_model, shadow_percentage and shadow_eval rule fields, and the ktms_shadow_* metrics.

  • Latent features: the semantic cache (SEMANTIC_CACHE_*), latent guardrails (GUARDRAILS_LATENT_*), the encoder settings (LATENT_*), and embeddings in judge state (EmbeddingPart, input_embedding over HTTP).

  • JudgeStream and EvaluateRollouts, and POST /v1/decisions/rollouts over HTTP.

  • The MCP gateway: /mcp, the MCPServerSpec RPCs, allowed_mcp, the mcp_servers and mcp_credentials file sections, the ktms_mcp_* metrics and ToolCall in usage events.

  • The agents' inbox: the agents MCP server and its tools, ListAgents, SetAgentRecording, ListAgentTasks, GetAgentTask, AGENTS_*, the journal's event format and the ktms_agent_* metrics.

  • The Anthropic Messages API: POST /v1/messages, POST /v1/messages/count_tokens and the X-KTMS-Token-Count header.

Compatibility with ktms-admin and light-tokenmeter
KTMS ktms-admin light-tokenmeter Notes
1.x 1.x 1.x With light-tokenmeter's authentication on, set USAGE_COLLECTOR_TOKEN to a write token.
1.x 0.4.x 0.1.x Works without authentication; no compatibility promise before 1.0.

ktms.v1 and usage.v1 stay compatible within 1.x, so any 1.x release of each works with any 1.x release of the others.

Upgrading
  • Within 1.x, upgrade in one step from any earlier 1.x release: migrations run forward at startup and cover every release in between. scripts/upgrade-test.sh upgrades from the previous release and from the oldest release of the same major on every pull request and before each release.
  • Rolling upgrades work within 1.x: migrations only add, so replicas of the previous release keep serving on the migrated database while new ones start; the upgrade test runs the old release again after the new one migrated. A change that cannot be additive waits for 2.0. Until the roll finishes, old replicas do not apply what the new release added (for example a new kind of limit), so finish rolls promptly.
  • Downgrades are not supported. Migrations have no down step; to go back, restore the database backup taken before the upgrade.
  • From 0.x, go through 0.14.x to 1.0, then to later 1.x releases: 0.x made no compatibility promise, and only the 0.14 → 1.0 step is tested (by the 1.0 release).
  • Read the CHANGELOG before upgrading: it lists every change in Added, Changed, Deprecated, Removed, Fixed and Security.
Deprecated

Deprecated names keep working through 1.x, log a warning (or count in ktms_legacy_headers_requests_total), and are removed in 2.0. One exception is announced in advance:

Deprecated Use instead Removed
LLMPROXY_ENCRYPTION_KEYS KTMS_ENCRYPTION_KEYS 2.0
X-LLM-Proxy-* request and response headers X-KTMS-* On their Sunset date, 2027-03-01, as already announced in the responses

Operations

  • Health: GET :8080/healthz (process up, with its version) and GET :8080/readyz (database reachable).
  • Logs: structured JSON via zerolog. Vendor keys, client keys and the keyring are never logged.
  • Config changes are picked up by all replicas within ROUTING_RELOAD_SECONDS; the replica that handled the admin call applies them immediately.
  • Upgrades: migrations run at startup under a database advisory lock, so rolling deploys are safe. Roll forward by deploying a newer image tag; see Upgrading.
  • Graceful shutdown: on SIGTERM in-flight calls get SHUTDOWN_TIMEOUT to finish.
Backups and the encryption keyring
  • Back up Postgres. It holds everything durable: client keys (as hashes), vendor keys (encrypted), models, rules, limits, tenants, node keys and batches. Use pg_dump or your provider's snapshots, and test a restore.
  • Back up the keyring separately. KTMS_ENCRYPTION_KEYS is not in the database. Keep it in a secret manager, not next to the dumps: a dump plus the keyring is every vendor key in plaintext. If the keyring is lost, every stored vendor key is lost, and each has to be set again with SetVendorKey; client keys, config and limits survive.
  • Keep old key versions as long as the backups that need them. A dump taken before a rotation can only be read with the versions it was written with.
  • Restore: stop every replica, restore the dump, flush the gateway's Redis keys (the REDIS_KEY_PREFIX namespace), then start a release at least as new as the one that took the dump. The flush matters: cached responses are keyed by the config version, which the restore rolls back, so later changes would reuse version numbers whose cache entries belong to another config. An older release could run on the restored schema but would ignore what was added after it, so do not restore with one.
  • Redis is not backed up. It holds rate, token and spend counters, the response and semantic caches, and the node pool: registrations (with encrypted node auth tokens), heartbeats, slot leases, the node circuit breaker and drain flags. Losing it resets the current day's and month's counters (spend limits then allow up to one more full cap in the current period), empties the caches, drops every node from routing until its next heartbeat re-registers it, and clears drain flags, so drained nodes take traffic again.
When a dependency fails

Failed calls to Redis, Postgres and the usage collector are counted in ktms_dependency_errors_total{dependency,operation}; alert on it. Nothing below needs a restart: the Redis pool, the Postgres pool and the collector connection reconnect by themselves. After 3 consecutive Redis transport failures, Redis calls fail fast without network IO for 2s, then are tried again, so a hung Redis costs a request about one REDIS_TIMEOUT, not one per command.

Dependency What breaks Behaviour Why
Redis RPM limits, daily token budgets Fail open: calls proceed unmetered. A metering outage should not become a service outage.
Redis Spend limits SPEND_LIMITS_FAIL_MODE: open (default) proceeds unmetered; closed refuses calls under a limit with UNAVAILABLE throttled:spend_unavailable. Money: the operator chooses.
Redis Settling reservations The counter keeps the reserved estimate instead of the actual usage until its window resets: a key can hit its limit early, or late when a call used more than was reserved. Reported, not repaired: the window reset clears it.
Redis Response and semantic cache Fail open: treated as a miss; storing is skipped. A cache is an optimisation.
Redis Local node pool The pool's state is unknown, so pool models are skipped and the chain falls back (e.g. to cloud models); the vendor circuit breaker is not tripped. A node whose state cannot be read is left out, never assumed idle. Node registration returns UNAVAILABLE (the agent retries); the sweep never evicts a node on a read error. Never overbook a node; keep serving from the rest of the chain.
Usage collector (light-tokenmeter) Usage events Queued in memory (10,000 events) and sent in batches; while the collector is down they are held and retried, then delivered exactly once (event IDs deduplicate). Events the collector refuses are dropped one by one, not with their batch. Drops are counted in ktms_usage_events_dropped_total{reason} (queue_full, rejected, shutdown, closed). Shutdown flushes for up to 5s. Requests never wait on it. Accounting must not slow down or lose traffic.
Postgres API key and node key checks Keys that verified within AUTH_STALE_IF_ERROR (5m) keep working; any other key is refused with UNAVAILABLE auth_unavailable. Each check is bounded by DB_TIMEOUT (3s). Routing, limits and vendor keys serve from the last snapshot, so traffic of known keys continues. /readyz stays 200 with "status":"degraded" until AUTH_STALE_IF_ERROR has passed since Postgres last answered, then reports 503 (every cached key has expired); with AUTH_STALE_IF_ERROR=0 it reports 503 at once and every call is refused. Availability for known keys. Revocation still applies on the next request whenever Postgres answers; only one made just before an outage can lag, by at most the window.
Postgres Routing config reload The last good snapshot keeps serving; each poll or rebuild is bounded to 30s and retried every ROUTING_RELOAD_SECONDS.
Postgres Admin RPCs, batches UNAVAILABLE storage_unavailable (cause logged). A batch whose idempotency check fails is refused rather than submitted, so it is never paid for twice.
Postgres Startup Migrations and the first config load must succeed (the first load within 30s); otherwise the process exits and the orchestrator restarts it. Nothing to serve without a first snapshot.
Metrics and alerts

Prometheus metrics are served on METRICS_ADDR (:9091) at /metrics; the Helm chart can add a ServiceMonitor. KTMS's own metrics start with ktms_; the standard Go runtime and process metrics are served next to them.

Metric Labels What it tells you
ktms_requests_total vendor, model, effort, status (success, error, canceled) Upstream calls and their outcome; canceled is a caller that went away.
ktms_request_duration_seconds vendor, model, effort Upstream call duration, histogram; for a stream, the whole stream.
ktms_time_to_first_token_seconds vendor, model Time to the first streamed chunk, histogram.
ktms_tokens_total vendor, model, type (input, output) Tokens consumed.
ktms_cache_tokens_total vendor, model, type (read, write) The part of input read from or written to the prompt cache.
ktms_active_requests vendor, model Upstream calls in flight.
ktms_throttled_total reason (rpm, key_rpm, key_tpm, mcp_tool_rpm, budget_key, budget_user, spend_key_daily, spend_key_monthly, spend_user_daily, spend_user_monthly, spend_team_monthly, spend_unavailable) Calls refused by a limit; spend_unavailable is Redis down with SPEND_LIMITS_FAIL_MODE=closed.
ktms_fallbacks_total from_model, to_model, reason Moves down a fallback chain.
ktms_circuit_breaker_state vendor, model 0 closed, 1 open, 2 half-open.
ktms_circuit_breaker_transitions_total, ktms_circuit_breaker_rejections_total vendor, model (+ from, to) Breaker flaps and the calls it refused.
ktms_vendor_cooldowns_total vendor, model Pauses started from a vendor Retry-After.
ktms_dependency_errors_total dependency (redis, postgres, usage_collector, keyring, mcp), operation Failed calls to a dependency; keyring is a stored vendor key that no keyring version decrypts.
ktms_usage_events_dropped_total reason (queue_full, rejected, shutdown, closed) Usage events that never reached light-tokenmeter.
ktms_batch_jobs_total, ktms_batch_items_total vendor, state Batch progress.
ktms_batch_poll_duration_seconds vendor Vendor batch polling, histogram.
ktms_shadow_requests_total, ktms_shadow_judge_evaluations_total, ktms_shadow_judge_scores Shadow traffic and its judged quality.
ktms_legacy_headers_requests_total header Clients still sending X-LLM-Proxy-* headers.
ktms_mcp_tool_calls_total server, tool, status (success, tool_error, denied, throttled, timeout, too_large, upstream_error, cancelled) Tool calls through the MCP gateway.
ktms_mcp_tool_call_duration_seconds server Tool call duration, histogram.
ktms_mcp_response_bytes server Tool result size, histogram.
ktms_agent_messages_total outcome (sent, replied, held, denied_<reason>) Messages between agents; held is a task a guardrail holds for a person.
ktms_agent_tasks status Agent tasks in each status, read from Postgres every minute; every replica reports the same total, so aggregate with max.
ktms_agent_reply_seconds role (agent — the recipient, user — the sender), status How long a task waited for a reply since its last change.
ktms_agent_journal_events_total result (written, error) Writes to the agents' journal.

The throttled and usage_events_dropped reasons are exported at 0 from startup, so increase() catches the first event after a restart. Suggested alerts (tune thresholds to your traffic; job is your scrape job, with the chart's ServiceMonitor the Service name, e.g. prod-ktms):

groups:
  - name: ktms
    rules:
      - alert: KTMSUpstreamErrorRate
        expr: sum by (vendor, model) (rate(ktms_requests_total{status="error"}[5m]))
          / sum by (vendor, model) (rate(ktms_requests_total{status!="canceled"}[5m])) > 0.05
        for: 10m
      - alert: KTMSTimeToFirstTokenP99
        expr: histogram_quantile(0.99, sum by (le, vendor, model) (rate(ktms_time_to_first_token_seconds_bucket[5m]))) > 10
        for: 15m
      - alert: KTMSCircuitOpen
        expr: max by (vendor, model) (ktms_circuit_breaker_state) == 1
        for: 5m
      - alert: KTMSDependencyErrors
        expr: sum by (dependency) (rate(ktms_dependency_errors_total[5m])) > 0
        for: 5m
      - alert: KTMSUsageEventsDropped
        expr: sum by (reason) (increase(ktms_usage_events_dropped_total[15m])) > 0
      - alert: KTMSSpendLimitReached
        expr: sum by (reason) (increase(ktms_throttled_total{reason=~"spend_(key|user|team)_.+"}[15m])) > 0
      - alert: KTMSDown
        expr: up{job="ktms"} == 0
        for: 2m

KTMSDependencyErrors and KTMSUsageEventsDropped matter most: Redis and the collector fail open, so the calls keep succeeding and only these metrics show that limits or accounting stopped. A long stream is not slow, so latency alerts use the time to the first token. Also probe /readyz: it turns degraded, then 503, during a Postgres outage.

Sizing

From the benchmark: about 0.65 s of CPU per 1,000 proxied requests, under 50 MiB of memory at 4,000 RPS, no drift at a steady load. Upstream time does not cost CPU, so size by request rate, not by how long vendors take to answer.

  • CPU: plan 1 vCPU per 1,000 RPS, which leaves headroom for TLS, logging and streaming. Guardrails, the semantic cache and the latent encoder add CPU per call; measure them with go test -bench (see Semantic cache and latent guardrails).
  • Memory: memory follows the bodies in flight, not RPS. The chart's default (256 MiB request, 512 MiB limit) covers thousands of concurrent short calls; raise it for large prompts, images or audio (GRPC_MAX_RECV_MSG_SIZE is 16 MiB per message).
  • Replicas: run at least two behind a load balancer; they share nothing but Postgres and Redis. Each replica keeps its own circuit breakers and vendor pauses.
  • Postgres: replicas × DB_MAX_CONNS (25 by default) must fit under max_connections with room for migrations and the other services. Routing serves from a snapshot reloaded every ROUTING_RELOAD_SECONDS, so the hot path costs one indexed key lookup per call; size Postgres for that query rate.
  • Redis: a few commands per call when limits, caches or the node pool are on, each bounded by REDIS_TIMEOUT. Keep it in the same zone as the gateway; one small instance serves every replica.

Development

make test       # go test -race ./...
make build      # binary in bin/
make proto      # regenerate gen/ after editing proto/ (needs buf)
make proto-py   # generate Python client stubs in gen/python/
make proto-ts   # generate TypeScript client stubs in gen/ts/
make clients    # regenerate the usage-sink stubs from usage-proto/
docker compose up --build   # local stack with Postgres + Redis

usage-proto/usage/v1/usage.proto is a byte-for-byte copy of light-tokenmeter's, which owns it. CI fails when the copy or the stubs generated from it drift; copy the file over and run make clients.

Layout: cmd/server (entrypoint), internal/router (rule matching, fallback, admission), internal/registry (validated config snapshots), internal/llm (vendor-neutral chat model and adapters), internal/throttle (Redis limits), internal/grpcapi (RPC handlers), internal/crypto (keyring), migrations/ (embedded SQL).

Pull requests are welcome; read CONTRIBUTING.md first. Maintainers release as described in RELEASING.md.

Roadmap

What we build now, next and later, and what is not planned, is in ROADMAP.md. Requests and ideas are welcome as issues.

License

Apache 2.0

Directories

Path Synopsis
bench
mock-upstream command
Command mock-upstream is a deterministic OpenAI-compatible upstream for gateway benchmarks.
Command mock-upstream is a deterministic OpenAI-compatible upstream for gateway benchmarks.
cmd
ktms command
Command ktms administers a Keystone (KTMS) gateway: its configuration as a file, keys, spend, limits and nodes through the Admin API, and a test call and health checks through the HTTP API.
Command ktms administers a Keystone (KTMS) gateway: its configuration as a file, keys, spend, limits and nodes through the Admin API, and a test call and health checks through the HTTP API.
node-agent command
node-tray command
Command ktms-node-tray shows a local node's level in the menu bar or tray and lets its owner pause it, limit it and edit its schedule.
Command ktms-node-tray shows a local node's level in the menu bar or tray and lets its owner pause it, limit it and edit its schedule.
server command
gen
internal
agentrun
Package agentrun answers an agent's inbox with a local CLI agent: it polls the MCP server "agents" with the agent's key, hands each task that waits for the agent to a command (claude, codex, agy or the user's own) and sends what the command printed back as the reply.
Package agentrun answers an agent's inbox with a local CLI agent: it polls the MCP server "agents" with the agent's key, hands each task that waits for the agent to a command (claude, codex, agy or the user's own) and sends what the command printed back as the reply.
agents
Package agents lets the agents of different people talk through Keystone: an agent is a client key with a card, and a task is one request from one agent to another, delivered to the recipient's inbox (MCP server "agents").
Package agents lets the agents of different people talk through Keystone: an agent is a client key with a card, and a task is one request from one agent to another, delivered to the recipient's inbox (MCP server "agents").
apikeys
Package apikeys mints and verifies the caller credentials stored in the api_key table.
Package apikeys mints and verifies the caller credentials stored in the api_key table.
audio
Package audio transcodes the raw PCM emitted by Gemini TTS into the OGG/Opus container chat apps such as Telegram render as a voice message.
Package audio transcodes the raw PCM emitted by Gemini TTS into the OGG/Opus container chat apps such as Telegram render as a voice message.
cooldown
Package cooldown holds back upstream targets whose vendor answered 429 or 503 with Retry-After, so routing skips them without network IO until it expires.
Package cooldown holds back upstream targets whose vendor answered 429 or 503 with Retry-After, so routing skips them without network IO until it expires.
crypto
Package crypto encrypts vendor LLM keys at rest with AES-256-GCM.
Package crypto encrypts vendor LLM keys at rest with AES-256-GCM.
declarative
Package declarative keeps a Keystone configuration in a file and applies it through the Admin API.
Package declarative keeps a Keystone configuration in a file and applies it through the Admin API.
grpcapi
Package grpcapi implements the proto services on top of proxydb, the registry, and the router.
Package grpcapi implements the proto services on top of proxydb, the registry, and the router.
latent
Package latent projects text into a representation space (JEPA or any embedding encoder) for semantic caching and latent guardrails.
Package latent projects text into a representation space (JEPA or any embedding encoder) for semantic caching and latent guardrails.
llm
mcpgw
Package mcpgw is Keystone's MCP gateway: clients reach registered MCP servers through /mcp with a KTMS key, and the gateway holds the servers' credentials, enforces each key's allowed_mcp and tool_rpm, and accounts for every tool call.
Package mcpgw is Keystone's MCP gateway: clients reach registered MCP servers through /mcp with a KTMS key, and the gateway holds the servers' credentials, enforces each key's allowed_mcp and tool_rpm, and accounts for every tool call.
nodetray
Package nodetray is the desktop tray's logic, kept apart from the GUI so it is tested without one: what the menu bar shows for an agent status, the notifications state changes raise, the icons and the settings page.
Package nodetray is the desktop tray's logic, kept apart from the GUI so it is tested without one: what the menu bar shows for an agent status, the notifications state changes raise, the icons and the settings page.
panicsafe
Package panicsafe turns a panic into a reported, recoverable error instead of a silent process death.
Package panicsafe turns a panic into a reported, recoverable error instead of a silent process death.
pricing
Package pricing turns token usage into money.
Package pricing turns token usage into money.
proxydb
Package proxydb is the pgx data layer: api keys, the model registry, routing rules, encrypted vendor keys, and the config version counter.
Package proxydb is the pgx data layer: api keys, the model registry, routing rules, encrypted vendor keys, and the config version counter.
redisguard
Package redisguard fails Redis commands fast while Redis is unreachable.
Package redisguard fails Redis commands fast while Redis is unreachable.
rediskeys
Package rediskeys builds every Redis key this service uses, so one Redis can host several deployments behind REDIS_KEY_PREFIX.
Package rediskeys builds every Redis key this service uses, so one Redis can host several deployments behind REDIS_KEY_PREFIX.
router
Package router picks a rule for a request and walks its model chain.
Package router picks a rule for a request and walks its model chain.
safefetch
Package safefetch validates and downloads external media URLs with SSRF protection.
Package safefetch validates and downloads external media URLs with SSRF protection.
spendlimit
Package spendlimit names the monetary limit subjects, periods and refusal reasons, and computes the UTC calendar windows they reset on.
Package spendlimit names the monetary limit subjects, periods and refusal reasons, and computes the UTC calendar windows they reset on.
throttle
Package throttle enforces per-model RPM via GCRA, daily token budgets and monetary spend limits in Redis, shared across replicas.
Package throttle enforces per-model RPM via GCRA, daily token budgets and monetary spend limits in Redis, shared across replicas.
Package migrations carries this service's own DDL, embedded so the binary applies it at startup instead of waiting for an operator to run psql.
Package migrations carries this service's own DDL, embedded so the binary applies it at startup instead of waiting for an operator to run psql.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL