· Piotr Gregorczyk · Engineering  · 8 min read

A local LLM gateway with a fallback that actually answers

One OpenAI-compatible endpoint on a Windows workstation, with a hosted model as the primary and a local open-weights model as the fallback. The routing was the easy part. Here is what broke on the way: catalogues that list models you cannot call, a service that cannot see your environment, and an install that would not repair itself.

One OpenAI-compatible endpoint on a Windows workstation, with a hosted model as the primary and a local open-weights model as the fallback. The routing was the easy part. Here is what broke on the way: catalogues that list models you cannot call, a service that cannot see your environment, and an install that would not repair itself.

This is a write-up of a small piece of infrastructure: a local LLM gateway on a Windows workstation. It exposes one OpenAI-compatible endpoint to every tool and script on the machine. Behind it, requests go to a hosted model by default and fail over to an open-weights model running locally in LM Studio.

The hosted provider in my case is AWS Bedrock, serving Claude models. Nothing in the pattern depends on that choice. The same gateway can front the Anthropic API, OpenAI, Azure OpenAI, Google Vertex AI or another local runtime such as Ollama or vLLM. Swapping or adding a provider is a few lines of gateway config, and that is the point of having a gateway.

It is a developer setup, not a production system. The goals were modest:

  • One endpoint. Tools stop hard-coding a provider, a region and a key each.
  • Degraded, not dead. When the hosted provider is unreachable, rate-limited or rejecting credentials, work continues on the local model.
  • No container host. It runs natively on Windows. I didn’t want Docker just for this.

The routing took an afternoon. Most of the time went into the parts around it, and those are the useful bits to write down.

The shape

The gateway is LiteLLM Proxy. I looked at a few alternatives and chose it because it had a straightforward no-Docker install path on Windows, and an OpenAI-compatible surface was all I needed.

The core of the configuration is two named models and a fallback rule:

model_list:
  - model_name: claude-primary
    litellm_params:
      model: bedrock/<versioned-claude-model-id>
      aws_region_name: <your-region>
      # other providers use the same shape, for example:
      # model: anthropic/<model-id>
      # model: azure/<deployment-name>
      # model: vertex_ai/<model-id>
  - model_name: lmstudio-fallback
    litellm_params:
      model: openai/<local-model-id>
      api_base: http://localhost:1234/v1
router_settings:
  fallbacks:
    - claude-primary: ['lmstudio-fallback']

Clients call http://localhost:4000/v1 with the name claude-primary. If that deployment errors, the router retries on lmstudio-fallback. A client can also ask for the fallback directly when it wants to stay local on purpose.

The fallback is a mid-size open-weights model. It is not a substitute for a frontier model on hard reasoning. It is good enough for drafts, small refactors and classification, which is what keeps work moving during an outage.

Test the fallback by breaking the primary

A fallback in a config file is a claim. The only proof is a completion that came from it.

So the test is deliberately hostile: break the primary provider’s credentials, leave LM Studio alone, and send a normal request through the gateway, exactly as a client would. It should come back with a local answer and no error.

It did. LiteLLM also says that it happened, in a response header:

x-litellm-attempted-fallbacks: 1

That header is worth surfacing in any client that cares, because a fallback answer that looks identical to a primary answer invites over-trust.

A bad credential is only one way to fail. Timeouts, rate limits, 5xx responses and empty bodies can take different paths through a proxy. Each one deserves its own test, and the tests need repeating after every gateway upgrade.

Catalogues list what exists, not what works

Two separate surprises came from the same root cause: a model listing is not a health check.

LM Studio lists downloaded models, not loaded ones. Its /v1/models endpoint happily returns a model that is on disk but not in memory. The listing looks healthy, and the fallback call then fails with “Model is unloaded”. Either load the model explicitly (lms load <model>) or enable just-in-time model loading in LM Studio’s local server settings. Then prove it with a real completion.

LiteLLM’s /v1/models lists its own configuration, not what your provider account can call. It reflects the aliases and patterns in config.yaml. Whether the provider will actually serve a given model, in a given region, for your account is a separate question, and only the provider can answer it. On Bedrock that means:

aws bedrock list-inference-profiles --region <your-region>
aws bedrock list-foundation-models --by-inference-type ON_DEMAND --region <your-region>

Other providers have their own equivalent: a models endpoint, a deployments list or a console page. Ask the provider, not the gateway.

The general lesson: anything that says “available” without producing an answer should be treated as a hint.

A service is a different user

Running the proxy in a terminal is easy. Running it as a Windows service that starts on boot and restarts on crash (here with NSSM) brings two lessons.

The service does not see your environment. Credentials that live in your user profile, such as an API key in a per-user environment variable, do not exist for the account the service runs as. A proxy that starts without its credential does not look like a configuration problem. It looks like a provider outage, because every upstream call fails authentication. The fix is to give the service its own environment explicitly (NSSM can set environment variables per service) and to fail loudly at startup when something is missing.

A half-finished install does not repair itself. An early test run from a shell that was not elevated created the service registration with the wrong executable path. The later elevated install didn’t fix it, because nssm install will not overwrite a service that already exists. It only patched parameters on top of the stale path. The service looked registered and still would not run properly. The fix is to edit the existing registration in place (nssm set <service> Application <path>) and then check the result with a real health call, not the service manager’s status:

curl -s http://localhost:4000/health/liveliness

After the repair, the gateway runs as an auto-start service and answers both the health check and real completions.

Key rotation is an operation, not an edit

The provider key will be rotated. When it is, three things have to happen together: the service environment gets the new key, the service restarts, and a test call proves it works. Doing that by hand is how you end up with a gateway that fails at 9 a.m. the next day.

So rotation is a script, run elevated. It:

  1. backs up the current config and service environment
  2. sets the new key on the service
  3. restarts the service
  4. runs a test call
  5. supports a rollback to the last backup

Three smaller lessons from the first rotation:

  • Retiring the old key is part of the rotation. The gateway started on a long-lived static access key and moved to a provider API key scoped to model calls. The rotation was not finished when the new key worked. It was finished when the old key was revoked and every copy of it was gone: credential files, exported key files, an install script, configuration backups. Those copies are easy to forget, because nothing breaks while they exist.
  • Clients keep their own copies. Some tools store a copy of the key in their own settings instead of reading it from the environment. Every one of them is a place a rotation can miss. The fewer clients that hold a provider key directly, rather than calling the gateway, the better.
  • Pin the gateway version too. One release of the proxy needed placeholder credential variables just to start in a configuration that didn’t use them. Upgrades of the gateway itself are changes. Test them like changes.

Convenience versus pinning

Later I added a catch-all route: any model name the gateway doesn’t recognise passes through to the hosted provider. Exact aliases still match first. For a developer workstation that is convenient, because new models work without editing the config.

It is also the opposite of version pinning. With a catch-all, the model that answers is whatever the client asked for, and the gateway no longer controls it. That is a fine trade-off on a laptop. Anywhere results need to be reproducible, routes should be explicit and versioned, and unknown names should be rejected.

Least privilege for the gateway credential

One principle I would apply to any gateway like this: its credential should be scoped to calling the model API and nothing else, ideally in an account that cannot administer anything around it. Prototypes tend to start with whatever credential is to hand, often a broad one. That is the first thing to replace. A key stolen from a gateway should not open the rest of your cloud estate.

What it is not

  • Not highly available. It is one process on one machine. If the workstation is off, so is the gateway.
  • Not a secret manager. Keys are in a local store and the service environment. Moving them to a proper secret store is on the list.
  • Not a quality gate. The fallback keeps work moving. It does not make the local model’s answers as good as the primary’s, and the client still has to know which one answered.

Lessons

  • A fallback counts only after it has answered a real request with the primary deliberately broken.
  • Model listings are hints. A completion is proof.
  • A service runs as a different user. Give it its own configuration and make it fail loudly.
  • Repair broken service registrations in place, and verify with a health call.
  • Script key rotation with backup, test and rollback, and finish it by revoking the old key and deleting its copies.
  • Pin the gateway version and treat upgrades as changes.
  • Catch-all routes trade control for convenience. Know which one you need.
  • Scope the gateway credential to the model API only.

Author

Piotr Gregorczyk

I'm a strategic technology executive with 15+ years in CTO and CIO roles, after a foundational decade at Accenture.

An engineer at heart and a successful manager for years, I hold an MSc in Computer Engineering from Warsaw University of Technology and a recent Executive MBA.

I turn technology modernization into real business value, with experience across clinical research, insurance, banking, real estate, software and consulting.

The opinions on this site are my own and do not represent any company I work or have worked for.

Piotr Gregorczyk
Back to Blog