How to Build a Local Proxy for Switching Between Multiple LLM Providers in Your Coding Workflow

    This article explains how to unify access to multiple large language models through a local proxy, allowing developers to switch between remote and local models without changing their coding agent's configuration. It covers the architecture, a model registry, a management panel, step-by-step setup instructions, and practical tips for mapping tasks to the right model.

    Why Your Coding Workflow Needs Interchangeable Models

    Many developers now rely on coding agents as daily tools. These agents read project structures, modify code files, run test commands, and continue fixing issues based on the results. The overall experience depends not only on the model itself but also on how the workflow is structured.

    Many tools are tightly coupled to a single model provider. When you switch models, you often need to update authentication credentials, adjust parameters, and rewrite custom instructions—interrupting established habits and project configurations. By isolating the model-access layer, you can keep the workflow intact and swap the underlying model as needed.

    In practice, common needs fall into four broad categories. First, tasks with Chinese comments, documentation, or requirement descriptions benefit from models with strong Chinese language support. Second, analyzing large codebases or long logs requires models with large context windows. Third, high-frequency simple completions and formatting can usually be handled by low-cost models. Fourth, projects involving unreleased or proprietary code must keep data within the internal network, so only local inference services are allowed.

    Architecture for Unified Model Access

    The solution consists of three components.

    1. Local Proxy Layer

    The proxy listens on a local port and exposes an OpenAI-compatible API, mainly the POST /v1/chat/completions and GET /v1/models endpoints. Your coding agent simply points its base_url to http://127.0.0.1:8787/v1 (8787 is the example port). The proxy handles three key tasks:

    • Protocol conversion: Local inference services like Ollama provide an OpenAI-compatible endpoint, so requests can be forwarded directly. For services that are not compatible, you need to write conversion logic separately.
    • Field mapping: The model field in a request corresponds to a name in the registry, which is then mapped to the actual model name on the server side. Response fields such as usage and finish_reason are normalized to a consistent format.
    • Key isolation: API keys are only read from environment variables, never stored in configuration files.

    2. Model Registry

    The registry stores metadata for each model. Here is an example configuration:

    1. {
    2. "default": "remote-a",
    3. "providers": [
    4. {
    5. "name": "remote-a",
    6. "protocol": "openai-chat",
    7. "base_url": "https://api.example-a.com/v1",
    8. "model": "example-large-model",
    9. "api_key_env": "REMOTE_A_API_KEY",
    10. "context_window": 128000,
    11. "supports_tools": true,
    12. "supports_stream": true
    13. },
    14. {
    15. "name": "local-coder",
    16. "protocol": "openai-chat",
    17. "base_url": "http://127.0.0.1:11434/v1",
    18. "model": "qwen2.5-coder:7b",
    19. "api_key_env": null,
    20. "context_window": 32768,
    21. "supports_tools": false,
    22. "supports_stream": true
    23. }
    24. ]
    25. }

    Field explanations:

    • protocol specifies the request format; here it’s OpenAI-compatible chat completions.
    • api_key_env stores the environment variable name, not the key itself.
    • context_window records the model’s context limit; the proxy uses this to decide whether to truncate requests.
    • supports_tools and supports_stream indicate model capabilities. For local models, supports_tools is set to false, meaning tool calls have not been verified and are treated as unsupported.

    Ollama’s default context length is relatively short. If the registry specifies 32768, you also need to set the corresponding num_ctx in the model parameters—both values must match.

    3. Visual Management Panel

    The panel displays each model’s status, latency, and usage, and lets you switch the default model. It reduces the overhead of managing multiple models and gives team members visibility into usage distribution. The panel reads proxy logs, so it must follow the privacy conventions mentioned later.

    Step-by-Step Implementation

    Step 1: Prepare the Environment and Start the Proxy

    First, confirm that the runtime required by your proxy implementation is installed. If you use Ollama, verify that port 11434 is listening. The commands below are generic templates—replace them with the actual commands from your chosen proxy project’s documentation:

    1. # 1. Install the proxy (replace with the actual install command)
    2. <install-command> llm-proxy
    3. # 2. Generate the default config (path as per project docs)
    4. <proxy-cli> init --config ~/.config/llm-proxy/providers.json
    5. chmod 600 ~/.config/llm-proxy/providers.json
    6. # 3. Set the API key (in shell config or system keyring, never in a repo)
    7. export REMOTE_A_API_KEY="your-key"
    8. # 4. Start the proxy, defaults to 127.0.0.1:8787
    9. <proxy-cli> serve --config ~/.config/llm-proxy/providers.json

    Step 2: Register Models and Verify Incrementally

    Before integrating, test each endpoint directly to confirm whether a problem comes from the service itself or the proxy. For a local Ollama instance:

    1. ollama pull qwen2.5-coder:7b
    2. curl http://127.0.0.1:11434/v1/chat/completions \
    3. -H "Content-Type: application/json" \
    4. -d '{"model":"qwen2.5-coder:7b","messages":[{"role":"user","content":"Reply with OK"}]}'

    For a remote service, use the same request body but include the authentication header:

    1. curl https://api.example-a.com/v1/chat/completions \
    2. -H "Authorization: Bearer $REMOTE_A_API_KEY" \
    3. -H "Content-Type: application/json" \
    4. -d '{"model":"example-large-model","messages":[{"role":"user","content":"Reply with OK"}]}'

    Once both endpoints return results, test the proxy. First, list the models—you should see the names from the registry:

    1. curl http://127.0.0.1:8787/v1/models

    Finally, set the model name in your coding agent to the registry name (e.g., remote-a or local-coder) and send a simple request. If it returns normally, the full chain is working.

    Common errors and their solutions:

    Error Common Cause Fix
    401 Unauthorized Key is empty, env var not loaded, or key expired Run echo $REMOTE_A_API_KEY to verify; regenerate the key if necessary
    404 Not Found base_url is missing or has an extra /v1, or path doesn’t match protocol Check the full path against the provider’s documentation
    model not found Model name typo, or local model not pulled Use the names returned by /v1/models; for local check ollama list
    Connection refused Proxy or inference service is not running, or wrong port Run lsof -i :11434 or lsof -i :8787 to inspect listening status
    429 Too Many Requests Rate limiting triggered Reduce concurrency, or configure a fallback model in the registry

    Step 3: Switch Models Based on Task Type

    It’s recommended to define task-to-model mappings first, then let the proxy route automatically or have the developer switch manually.

    Task Type Recommended Model Category Key Metric Compliance Requirement Verification Method
    Code generation & complex refactoring Remote model with strong reasoning Test pass rate Remote models allowed for non-sensitive projects Run unit tests
    Long-context analysis Model with large context window Accuracy of cited content Depends on project policy Spot-check key conclusions
    Simple completions & batch formatting Low-cost model Cost per call Remote or local model acceptable Compare diff before/after formatting
    Sensitive projects Local inference model Service availability Must use local model Confirm traffic only goes to 127.0.0.1