A Local Multi-Model Gateway for Programming Assistants

    A local protocol gateway between a coding assistant and upstream models keeps the client API stable while handling auth, routing, protocol translation, and usage observability. This article explains protocol mismatches, failover routing, task-based model selection, and acceptance checks for streaming, tools, and session affinity.

    Why put a layer between the coding assistant and the model

    Coding tools get adopted less because one model is “smarter” and more because they close a repeatable loop: repository context, file edits, command execution, and result verification. What often blocks production use is unstable model supply. Chinese-language tasks, long context, cost-sensitive work, and code that cannot leave the intranet all impose different requirements on upstreams. If every model swap means a new client, a pile of config changes, and reworked auth, integration cost wipes out workflow gains.

    A more durable approach is to split the “operator UI” from the “model that answers.” The client keeps using the request format it already knows. A protocol gateway runs locally and handles authentication, routing, protocol conversion, and usage observability. Users still see the same assistant; the model that actually produces patches and explanations can be a cloud inference service, a compatible API, or an intranet runtime.

    This is not a new coding product, and it does not claim the gateway itself writes better code. The gateway does not infer; it transports and normalizes requests. Whether a model can reliably edit files, call tools, or handle images depends on upstream capabilities, not on the fact that something is “connected.”

    Protocol differences the gateway must actually solve

    Chat APIs are not interchangeable. Common gaps include:

    • Request shape: some APIs use a multi-turn message list; others split system prompt, tool definitions, and history into separate fields; others use a dedicated responses-style interface.
    • Streaming events: incremental text, tool-call arguments, reasoning summaries, and finish reasons differ in field names and event boundaries.
    • Tool calling: function names, whether arguments arrive in one shot or in fragments, parallel calls, and how tool results are folded back after failure all follow the upstream contract.
    • Multimodality: images may be URLs, inline data, or file references. The gateway can only convert representations it understands; it cannot invent modalities the upstream does not support.

    “Connect every model” therefore means: ship a set of protocol adapters and allow declared-compatible endpoints, provided the upstream protocol is in scope. Tool calling, long context, and image results must be checked item by item, not inferred from a vendor list.

    A minimal, auditable conversion path:

    1. The client sends a completion or response request in a fixed protocol, with system prompt, history, tool definitions, and optional reasoning intensity.
    2. The gateway validates identity, applies routing policy, and selects an upstream account or endpoint.
    3. It maps messages, tools, and multimodal content to the target protocol, strips unknown fields, and keeps a replayable request id.
    4. It reads the upstream stream and maps text deltas, tool calls, and finish reasons back to the events the client expects.
    5. It records token usage, latency, error codes, and the chosen upstream for dashboards and audit.

    A schematic routing config expresses policy only; it is not bound to any vendor API:

    1. virtual_model: code-default
    2. strategy: failover # or weighted
    3. candidates:
    4. - provider: cloud-a
    5. weight: 70
    6. timeout_ms: 20000
    7. - provider: cloud-b
    8. weight: 30
    9. timeout_ms: 20000
    10. - provider: intranet-runtime
    11. weight: 0 # used only after prior failures
    12. session_affinity: true

    Local runtime and visual management

    Running the gateway on the machine or intranet, rather than introducing a control plane that must live on the public internet, has three direct benefits.

    First, keys and code context stay inside a controlled boundary. Before traffic leaves the network, policies can redact paths, environment variables, and secret fragments; intranet models need not leave the network at all. Second, start and stop follow the development session: the proxy comes up with the assistant when needed and need not occupy ports and quota while idle. Third, the management UI can be a local page instead of burying routing policy in nested JSON.

    A practical console should at least show whether the proxy is online, version, uptime, number of configured upstreams, and token usage. A sidebar is a natural place for auth, model aliases, subtask routing, logs, and local storage. These are operations facts, not model-capability marketing.

    Composite routing is closer to real use than “one alias, one upstream.” The same virtual model can use failover or weighted round-robin: if an upstream is rate-limited, times out, or returns a non-retryable error, try the next candidate. Weights send routine traffic to cheaper or lower-latency services; failover is for availability. Switching must happen at session boundaries or explicitly allowed retry points. Mid-conversation account or model changes can break tool-call context and cache prefixes, so answers stop lining up.

    Multi-account setups must also separate “pick available quota for a new session” from “keep affinity for an old session.” The panel can show quotas and recent errors so new work avoids exhausted accounts, while existing sessions stay on the original account so a half-written patch plan is not handed to another model.

    Choose models by task, not by swapping tools for a brand

    Write routing as task constraints, not vendor preference.

    • Repo-scale edits: prefer models with stable tool calling that return patch-shaped output, and limit the scope of a single change.
    • Long documents or large diffs: prefer context window and citation stability; retrieve and compress before sending if needed.
    • Cost-sensitive batch explanation, comments, and test drafts: cheaper or local runtimes are fine, with reduced permission to auto-run commands.
    • Code that cannot leave the intranet: register only intranet inference endpoints; the gateway must refuse a default fallback to the public internet.
    • Mixed Chinese business prose and code: pick upstreams validated on the target language and compare them on the same eval set, rather than switching by feel.

    Permissions should travel with routing. File writes, command execution, and network access must not stay at the same level just because the model got cheaper. Local or experimental models fit read-only analysis and drafts; auto-applying patches still needs a diff review.

    Capability limits and acceptance

    Before go-live, accept against fixed cases, not “it connects”:

    • Streaming: after a mid-stream disconnect, the client can end the turn instead of hanging.
    • Tool calling: argument fragments assemble into a valid structure; tool failures send error text back to the model.
    • Protocol fields: unknown fields are dropped or explicitly rejected so semantics do not change silently.
    • Affinity: the same session id still hits the original upstream after retry, unless that upstream is down and policy allows transfer.
    • Usage: input, output, and reasoning-related tokens are separately observable for billing checks.
    • Failures: rate limits, auth failures, and context overflow map to distinguishable errors, not a generic network error.

    Document what the gateway cannot paper over: if the upstream has no tools, it cannot write files for the assistant; if it has no vision, the gateway cannot turn a screenshot into visual understanding; if context exceeds the window, the translation layer can only truncate or reject—it cannot invent a longer window.

    Rollout steps

    1. Freeze the one outbound protocol the client understands and list required fields: messages, tools, streaming, finish reason.
    2. Write an adapter per upstream with a minimal round-trip sample: plain text, one tool call, one failed retry.
    3. Expose a virtual model name; bind endpoints, timeouts, retries, and whether egress is allowed internally.
    4. Enable session affinity; apply failover only on new requests or retryable errors.
    5. Use the local panel to check online status, quotas, and logs; by default, logs should not persist full source.
    6. Regression on a small set of real-repo tasks: explain a function, generate a patch, run tests, then edit again from the failure.

    Teams keep the familiar read–edit–run–verify loop while choosing cloud or intranet models under task constraints. The protocol gateway’s job is conversion, routing, and observability. Code quality still depends on model ability, context quality, and human review—not on the access layer itself.