A backend-agnostic local inference server. One stable OpenAI/Anthropic-compatible API in front; pluggable engines (llama.cpp, MLX, Ollama, …) behind it — install and run only what a model needs. Ships as a single Go binary and a container image, so the same server runs on your laptop, a shared GPU box, or a GPU node in Kubernetes.
The mainspring is the one tightly-wound component that drives the entire movement. This is the component that drives your local AI: small, self-contained, and the thing everything else runs on.
Local inference engines are commoditized and excellent — llama.cpp and Apple MLX are MIT-licensed and win on tokens/sec. What's missing is the control plane around them. The most-cited problems with Ollama and friends are not kernel problems, they are control-plane problems:
- Silent context truncation — prompts get dropped with no error, and the same prompt truncates differently per machine ("great locally, collapses in prod").
- Silent CPU fallback — you think you're on the GPU; you aren't, and nothing tells you.
- Zero authentication — tens of thousands of instances sit exposed on the public internet.
- Model thrashing / VRAM mismanagement — evict-and-reload every call; VRAM not reclaimed on switch.
- OpenAI-compat gaps — tool-call
argumentstype flips, streaming-with-tools breaks, andnis accepted and then ignored: ask for three candidates to rank, get one, and nothing tells you. (logprobsused to be on this list. Verified against Ollama + Qwen2.5-Coder 7B it works, and Mainspring passeslogprobs/top_logprobsthrough intact, so it no longer belongs here.)
Mainspring's thesis: don't build inference kernels — build the local-inference control plane. Wrap the permissively-licensed engines behind one versioned, conformance-tested API and make the layer around them fail loud, VRAM-aware, and governed.
- One stable, versioned OpenAI and Anthropic API —
/v1/chat/completions,/completions,/embeddings,/models, and Anthropic/v1/messages(text and tool use, streaming translated) plus/v1/messages/count_tokensfor pre-flighting a context budget — both dialects validated against their real Python and JS SDKs in CI, failure paths included (an upstream 5xx, a stream cut mid-flight). Both dialects also run the same pipeline — auth, per-tenant budgets, context guardrail, clamp, concurrency gate, timeouts, circuit breaker, cost accounting, response cache, coalescing, retry and model fallback. They differ in what they put on the wire, not in how a request is governed. Anthropicstop_sequencesare matched by Mainspring rather than forwarded to the engine, because an engine that stops also erases the match — sostop_reason: "stop_sequence"actually names the sequence that hit. - Pluggable backends behind a
Backend/Runnerinterface:llamacppandmlx(managed subprocess) ·ollama/lmstudio/llamafile/gpt4all(detect-and-adopt-only, never installed). For an adopted daemon a model'sidis the name that daemon knows it by andpathis unused — there is no file to open — and settingpathanyway is flagged at startup rather than surfacing later as a failed request. Per-model routing, aliases, and ordered fallback chains — one server can serve several models on different engines and fail over when one is down. - Fail loud, never silently degrade —
/capabilitiesandX-Mainspring-{Backend,Device,Warning}headers state the actual backend, whether it fell back to CPU, and the effective context window. nis checked against the answer — ask forn: 3candidates from an engine that never implementednand you get one choice and, everywhere else, no signal. Mainspring counts what came back and reports the shortfall onX-Mainspring-Warning(in-band as an inert SSE comment on a stream, whose headers left long before the last frame). It is observed, never read off a per-backend support table that would go stale on the next engine release — an engine that does honournis not warned about. The engine's body is forwarded byte-for-byte, and a request withoutn, or withn: 1, takes exactly the path it always did.- VRAM-residency-aware scheduling — single-flight load, byte-budget + LRU eviction, KeepAlive idle unload, optional preload.
- Reliability — per-model concurrency limit + bounded queue (503 backpressure), a circuit breaker
with a background health probe, retry-with-backoff on transient upstream failures (safe for streams),
model-level fallback chains (a different model answers when the primary is down), and a structured
error taxonomy (stable
codeper failure class) that every response goes through — an unrouted path answersroute_not_foundand a wrong methodmethod_not_allowed, not Go's plain text. - Opt-in response cache — identical deterministic (temperature 0) non-streaming requests return from a
bounded TTL+LRU cache without re-running the model; hits carry
X-Mainspring-Cache: hit. - Request coalescing — a burst of identical in-flight deterministic requests shares one backend
computation (single-flight); followers carry
X-Mainspring-Coalesced: true. - Context guardrail — optional
enforce_contextrejects over-context requests (context_length_exceeded) instead of letting the engine silently truncate, or downgrades to a warning header per model. - Standalone governance — API-key tenants with roles (admin/inference), per-tenant rate + token budgets, and real streaming token accounting.
- Cost accounting — optional per-model USD pricing turns real usage into spend, surfaced in the
usage ledger, a
mainspring_cost_usd_totalmetric, and/v1/quality(a real cost signal for routing). - Operability — Prometheus
/metrics, JSONL usage ledger,X-Request-ID+ W3Ctraceparentpropagation, optional access log, a config file read strictly (a misspelled key is a startup error naming the nearest real field, never a silently dropped line that leaves a guardrail off), SIGHUP hot-reload (carries flag-supplied models and keys across, and refuses any reload that would leave a running server unauthenticated), an admin API (drain / reload / model load-unload), TLS, graceful drain, and a/v1/qualityrouting signal a trust control plane like Hydra can consume. - Detect-first, opt-in managed install — use whatever engine is present; install a missing one only
when you explicitly run
mainspring install, never silently. Every download is staged to a temp file and SHA-256-verified before it is put in place, so a mismatch never reaches the install path and never disturbs an engine already installed there. Mainspring does not bundle a catalogue of engine builds: you point it at the artifact you want with--url+--sha256, or at your own manifest (optionally ed25519-signed via--pubkey). Shipping a curated, pinned manifest is tracked separately. - Runs on Linux, macOS and Windows, amd64 and arm64 — a
CGO_ENABLED=0static binary with no libc to match, plus a real multi-arch container manifest sodocker pullpicks the right image itself. Every platform is exercised in CI. Two honest caveats on Windows: MLX is Apple-Silicon-only by construction and reports itself unavailable, and there is noSIGHUP— the server says so at startup andPOST /admin/reloaddoes the same job. - Deploy anywhere — a single static binary, a distroless container image, and a Helm chart (HPA / ServiceMonitor / GPU node scheduling).
- We do not build inference kernels. The compute cores (llama.cpp/GGML, MLX) already win; we wrap them.
- No GUI dependency — headless-first daemon.
- We do not vendor Ollama's tree; we reimplement the outer control-plane layer cleanly (its pattern is MIT).
v0.2.0 — released. The full control plane is shipped: OpenAI + Anthropic APIs, six backends with
per-model routing / aliases / fallback, VRAM-aware scheduling that never evicts a runner out from under
an in-flight request, governance with quota-headroom headers, circuit breaker (open vs half-open
distinguished in /metrics), response cache + request coalescing, cost accounting, latency and
queue-wait percentiles, TLS, admin API, request-ID + trace-context, and /v1/quality.
go test -race clean across the tree. See
CHANGELOG.md and the releases page.
Since v0.2.0 (on develop, unreleased) a full correctness audit of the tree closed 18 issues.
Anthropic /v1/messages reached parity with the OpenAI path — it had been skipping the clamp, the
context guardrail, cost accounting and the circuit breaker while recording a hardcoded 200 for every
request. The response cache stopped replaying one tenant's request id and quota headroom to another,
and stopped treating an omitted temperature as deterministic. A config reload can no longer wipe
flag-supplied models or silently drop the server into open mode. Truncated responses are no longer
served as successes or cached, and a client's own disconnect is no longer charged to the circuit
breaker at any point — while a real timeout still is. Rejections, admin principals and ledger failures
all now appear in the telemetry that claimed to cover them. And the last unaddressed item in the Why
list above closed: an n: 3 that an engine answers with one choice is now reported instead of passing
for a complete answer.
| Platform | amd64 | arm64 | How |
|---|---|---|---|
| Linux | ✅ | ✅ | .rpm · .deb · .apk · .tar.gz |
| macOS (Intel / Apple Silicon) | ✅ | ✅ | .tar.gz |
| Windows | ✅ | ✅ | .zip |
| Container | ✅ | ✅ | ghcr.io/ankit373/mainspring (multi-arch manifest) |
# RHEL / Fedora / Rocky / Alma
sudo dnf install ./mainspring_<version>_linux_amd64.rpm
# Debian / Ubuntu
sudo apt install ./mainspring_<version>_linux_amd64.deb
# Alpine
sudo apk add --allow-untrusted ./mainspring_<version>_linux_amd64.apk
# The packages install a systemd unit and /etc/mainspring/config.yaml
sudo systemctl enable --now mainspring
sudo systemctl reload mainspring # after editing the configEvery other platform: download an archive from
releases, or go build ./cmd/mainspring.
One Linux binary covers every distribution. The build is CGO_ENABLED=0 with no PT_INTERP and no
PT_DYNAMIC — no dynamic loader, no libc to match — so the same artifact runs on RHEL, Fedora, Rocky,
Alma, Debian, Ubuntu, SUSE and Alpine. The packages differ only in how each distribution expects software
to be installed and supervised.
Upgrades never overwrite /etc/mainspring/config.yaml: it is registered as a config file, so dpkg
prompts and rpm writes .rpmnew instead of replacing your edits.
# Build from source…
go build -o mainspring ./cmd/mainspring
# …or grab a release binary / the container image
# https://github.com/ankit373/mainspring/releases
# ghcr.io/ankit373/mainspring:latest
# See which engines are present on this host (detect-first)
./mainspring backends
# Serve a local GGUF via llama.cpp…
./mainspring serve --model qwen2.5-coder=/path/to/model.gguf --addr :11500
# …or adopt a model from a running Ollama daemon
./mainspring serve --backend ollama --model 'qwen2.5-coder:7b=' --addr :11500
# Talk to it with any OpenAI client
curl localhost:11500/v1/chat/completions -H 'content-type: application/json' -d '{
"model": "qwen2.5-coder",
"messages": [{"role":"user","content":"hello"}]
}'
# The fail-loud truth about what actually ran it
curl localhost:11500/capabilities
# The machine-readable routing signal (per-model resident/device/degraded/queue/breaker)
curl localhost:11500/v1/qualityThese are two control planes at different scopes, not a router and a server. Hydra is the Trust Control Plane: it routes across heads to a target confidence of correctness. Mainspring is the inference control plane for one head: admission control, VRAM residency, and fail-loud truth about what actually ran.
Hydra asks which head should answer this, and how confident are we that it's right. Mainspring answers what is true about this head right now. They meet over the standard OpenAI-compatible HTTP boundary, so either can evolve independently.
Hydra stays provider-neutral — it works with everything (Ollama, OpenAI, Anthropic/Claude, any OpenAI-compatible endpoint). Mainspring is not a replacement for that and Hydra never depends on it; because Mainspring speaks the same standard API, Hydra treats it like any other head. What makes Mainspring special is that it's the head Hydra works with best — it's the only one that emits fail-loud capability signals, VRAM-residency hints, and verified OpenAI-compat correctness that Hydra's trust layer can consume directly. Best-integrated, never exclusive.
Issue first, always — see CONTRIBUTING.md for the workflow, the commit
convention, and the go test -race bar. Security issues go through
SECURITY.md, never a public issue.
MIT — see LICENSE. Built on MIT/Apache-2.0 engines only (llama.cpp, GGML, MLX, go-openai).