Keystone. The Model Switchboard. (KTMS)

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 Go binary: ~67 MB image, ~7 MiB idle RAM,
under 1 ms of added latency.
Features
- One place for vendor keys. OpenAI, Anthropic, Google Gemini, DeepSeek and TypeSafe
credentials live encrypted in Postgres (AES-256-GCM); clients never see them.
→ Vendor keys,
rotating the encryption key
- Routing by rules, not code. Clients send their needs (
medical: basic) and free-form
attributes; models declare competencies. Ordered rules map them to a model chain with
fallbacks, and a request no rule takes gets the cheapest models that fit. All of it changes at
runtime without a redeploy.
→ Models and rules,
Concepts
- Configuration as code. One YAML file, planned and applied like Terraform.
→ Configuration as a file
- Limits and money. Requests per minute, daily token budgets and hard USD caps per key and per
end user; organizations and teams. → Spend limits,
Organizations and teams
- Resilience. Same-model retry,
Retry-After back-off, circuit breakers and fallback across
vendors. → Concepts, When a dependency fails
- More than text. Streaming, structured (JSON-schema) output, batches at 50% discount, speech,
images, typed judgments, embeddings and translation. → Calling Keystone
- Drop-in APIs. OpenAI-compatible
/v1, the OpenAI Responses API and the Anthropic Messages
API: point an existing SDK, Codex CLI or Claude Code at Keystone. → HTTP APIs
- Your own hardware. Ollama, vLLM, llama.cpp, FreeToken or a translation engine on a Mac or a
GPU box, with cloud fallback; restricted data never leaves them.
→ Nodes, Restricted data
- MCP gateway and agents' inbox. MCP servers behind one key with per-tool limits, and an inbox
where agents send each other tasks. Experimental. → MCP gateway,
Agents
- Usage and observability. One event per call to light-tokenmeter, Prometheus metrics and
ready-made alerts. → Operations
- Console, CLI and assistants. The ktms-admin web console, the
ktms CLI, an MCP server and a
Claude Code plugin. → Console, CLI and AI assistants
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, Translate, 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 |