ITcompressor IT — compressed and to the point
← Back to articles

Hybrid inference: a single endpoint via any-router

LLM backends live on different hosts and ports: Ollama on one, llama.cpp on another, foreign relays — in the cloud entirely. If every client remembers the addresses itself, switching a model turns into a reconfiguration.

The solution is the any-router proxy router (open binaries, Go standard library): one address, an OpenAI-compatible /v1, with the model written in the request as provider/model. The router picks the provider from its config, rewrites the model and forwards the request upstream. The clients know nothing about the internals — and that is the main property.

Why hybrid#

Task Better local Better in the cloud
Private data (code, documents) ✅ nothing leaves ❌
Long context (agent sessions) depends on KV memory ✅ 1M on cloud relays
Heavy tasks, quality ceiling capped by a single card ✅ big models
Vision ✅ mmproj projector on llama.cpp depends on plans
Background routine, «when tokens ran out» ✅ fixed price ❌

Hybrid is not «either-or»: both parts of the fleet live simultaneously, and the client only names a model. A local upstream can be shut down for the night and raised in the morning — clients change not a line of config.

Provider config#

A provider in any-router is an OpenAI-compatible upstream with a baseurl and an optional key. A neutral config example:

listen: "127.0.0.1:8085"   # don't bind outwards: 0.0.0.0 = all interfaces
token: "<admin-token>"
providers:
  ollama:                  # local/remote Ollama
    baseurl: "http://ollama-host:11434/v1"
  llamacpp:                # llama.cpp (built per the local inference article)
    baseurl: "http://llamacpp-host:8080/v1"
  relay:                   # cloud relay: models with the :cloud suffix run at the provider
    baseurl: "https://relay-provider.example/v1"
    apikey: "<relay key>"
tokens: []                 # client API tokens (below)
langfuse:                  # trace export (non-blocking)
  enabled: true
  url: "https://langfuse-instance.example"
  public_key: "pk-lf-…"
  secret_key: "sk-lf-…"

Important practices:

  • close listen from all interfaces unless the whole service is meant to be public: /admin and /v1 bind together, everything is behind tokens, but an extra surface is not needed;
  • upstream keys never leak to clients — the proxy sends the provider's apikey, the client token is rewritten by the router;
  • the config lives in git (except production secrets) — the history of provider/token changes comes for free.

Tokens with scopes and balance aliases#

Each client gets its own API token with a scope — a list of allowed models: concrete ones, a whole host (provider/*) or a balance alias. Revocation is a single record deletion; one compromised client does not undermine the others.

tokens:
  - id: agent-chat
    token: "<agent secret>"
    scope: models
    models: ["llamacpp/*"]              # the agent gets only the local host
  - id: web-chat
    token: "<web-chat secret>"
    scope: models
    models: ["ollama/glm-5.3-flash:cloud", "llamacpp/Qwen3.8-27B"]

A balance alias is a group name (bare, without provider/) that clients reference like an ordinary model. Its members are models of different providers: that is hybrid at the routing level.

balances:
  - name: fast-chat                    # in the request's model field — just "fast-chat"
    models:
      - ollama/glm-5.3-flash:cloud
      - llamacpp/Qwen3.8-27B

Alias routing is round-robin with conversation stickiness: a conversation keeps going to the same member (switching a member resets the upstream prompt cache), bindings by TTL. Failover — on an unavailable member the request goes to the next one in the circle: the cloud is down — the load moves to the local stand, and vice versa. Such requests in Langfuse are tagged alias:<name> — you can see who actually served.

Routing internals#

Request Behavior
model as provider/model a direct request to the chosen provider (token scope checked before routing)
model — a bare name a balance alias: round-robin of members + stickiness + failover
GET /v1/models an aggregated list of all providers with provider/model ids (copyable straight into requests); for a scoped token — filtered by permissions
SSE (stream: true) streamed in chunks without buffering — an agent chat works exactly like with the upstream directly

A common awkward moment: llama.cpp without --alias reports the model id as the full gguf file path. The router builds auto-aliases itself: in the lists the model looks like llamacpp/Qwen3.8-27B-UD-IQ4_XS (the full path — in the raw field), clients copy the alias and don't think about the upstream's file system.

Observability#

For every /v1 request the router lands one trace in Langfuse (OTLP; the export is non-blocking, an instance outage does not touch observations): the model in the requested form, provider:<name>, model:<alias>, key:<token id>, ip:<address> tags, input/output and usage. Practice: filter by tags, don't open traces — «what is agent X eating on model Y over a week» is visible from the list; bodies are useful when debugging a specific error (truncation with a truncated marker).

Operations#

  • Client tokens ≠ the admin token: the admin panel (/admin) is never published in any scenario — mutations happen locally in the configurator or in the YAML.
  • An unknown provider for a scoped token is a 403, not a disclosure of the provider list; upstreams unavailable — an honest OpenAI-format 502 (clients retry themselves; in a balance alias the router enumerates members itself).
  • Updating a model on an upstream = a record in the provider config / on the upstream; clients keep calling provider/model without edits.
  • The client model lists are cached on the router (refresh every 10 min): after config changes use ?refresh=1 under the admin token, otherwise the new provider appears in the lists by the nearest cycle.

Where to continue#

← Back to articles