How to Connect AI Coding Tools to Multiple LLMs: An API Proxy Practical Guide
This guide explains how to use an API proxy to connect AI coding tools to multiple large language models (LLMs). It covers the architecture principles, environment setup, configuration routing, proxy startup, and common troubleshooting methods for seamless integration.
When writing code in the command line, a common frustration is that tools often connect to only one vendor’s model, forcing you to switch tools when changing models. An API proxy middle layer solves this problem. Located between the coding client and the model, it forwards requests to different backends, meaning the client only needs to recognize a single entry point.
1. Architecture: One Entry Point, Multiple Backends
The basic flow is: AI Coding Client → API Proxy → Cloud or Local Models.
The proxy primarily does two things. First, it translates the request from the client into a format the target model can understand, including the endpoint, authentication method, and request body fields. Second, it determines where the request goes based on configuration. The client simply uses a model alias, such as local-coder, and the proxy maps this alias to the actual model and address.
It is important to note that the proxy primarily transforms standard chat and completion interfaces. Capabilities like tool calling, multimodal input, streaming output, and structured output vary in support across different models. When passed through a proxy, these capabilities may be downgraded or lost, which will be discussed further below.
2. Environment Setup
It is recommended to use Python 3.9 or above and install it in a virtual environment to avoid dependency conflicts:
python3 -m venv .venvsource .venv/bin/activatepip install 'litellm[proxy]'
In production projects, you should pin a verified version number, such as pip install 'litellm[proxy]==your_verified_version', and document it. Verify in a test environment before upgrading.
If you are using local models, you also need to install Ollama and pull the model:
ollama pull qwen2.5-coder:7bollama serve
Ollama listens on port 11434 by default. If the system service has already started Ollama, you do not need to run ollama serve again.
3. Writing the Configuration File to Define Routing
The proxy’s behavior is dictated by config.yaml. Here is a minimal example:
model_list:- model_name: cloud-fast # Alias used by the clientlitellm_params:model: openai/gpt-4o-mini # Actual model, format is "provider/model_name"api_key: os.environ/OPENAI_API_KEY- model_name: local-coderlitellm_params:model: ollama/qwen2.5-coder:7bapi_base: http://127.0.0.1:11434
A few explanations:
model_nameis the name the client sees. You can name it yourself, but it must be kept consistent.litellm_params.modelis the actual model identifier. The prefix determines which provider handles the request.api_keyusesos.environ/to reference environment variables. By not hardcoding the key in the config file, you avoid accidentally committing it to the code repository.
Before starting the proxy, set two environment variables:
export OPENAI_API_KEY="sk-your_cloud_key"export LITELLM_MASTER_KEY="$(openssl rand -hex 32)"
LITELLM_MASTER_KEY is the proxy’s own access key, which the client must include when making calls.
4. Starting the Proxy and Verifying Calls
Start the proxy, listening only on the local loopback address:
litellm --config config.yaml --host 127.0.0.1 --port 4000
First, use curl to verify that routing is working correctly:
curl http://127.0.0.1:4000/v1/chat/completions \-H "Authorization: Bearer $LITELLM_MASTER_KEY" \-H "Content-Type: application/json" \-d '{"model": "local-coder", "messages": [{"role": "user", "content": "Explain what a closure is in one sentence."}]}'
Once it returns normally, configure your coding client. Taking a client that supports custom OpenAI-compatible endpoints as an example, you usually need to set two variables:
export OPENAI_BASE_URL="http://127.0.0.1:4000/v1"export OPENAI_API_KEY="$LITELLM_MASTER_KEY"
When specifying the model in the client, enter the alias local-coder from your config.yaml. The environment variable names and model parameter syntax vary across different clients, so please refer to their official documentation.
5. Common Troubleshooting
| Phenomenon | Possible Cause | Solution |
|---|---|---|
| Port already in use on startup | Port 4000 is occupied by another process | Find the process using lsof -i :4000, or change the port. |
