Connecting a Local Model to a Coding Assistant with Ollama: Setup and Troubleshooting
This guide shows how to connect a model served by Ollama to Claude Code, a coding assistant. It covers choosing the right API format, installing Ollama and pulling a coding model, setting environment variables, testing the endpoint with curl, fixing common errors, using a proxy when direct connection fails, and tuning performance.
1. Confirm the API Format First
Local model servers and programming tools connect through an API format. If the formats do not match, requests fail even when the address and port are correct. Before you configure anything, check two things: which interface the inference server provides, and which interface the tool expects.
| Inference server | Interface format | Server address | Variables to set in the tool |
|---|---|---|---|
| Ollama (newer versions) | Anthropic Messages-compatible interface | http://127.0.0.1:11434 |
ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN |
| Ollama | OpenAI-compatible interface | http://127.0.0.1:11434/v1 |
OPENAI_BASE_URL, OPENAI_API_KEY |
| Other frameworks (vLLM, llama.cpp, etc.) | Depends on the framework’s startup parameters | Check the startup log | See the framework and tool documentation |
Ollama’s support for the Anthropic interface depends on the version. Older versions do not have a /v1/messages endpoint, so the tool returns a 404 error. The curl test in Section 5 shows whether your version can connect directly. If it cannot, use the proxy approach in Section 7.
This article uses Claude Code as the example because it reads ANTHROPIC_* environment variables. Other tools may use different variable names, so check their official documentation.
2. Prepare the Local Inference Server
2.1 Install Ollama
macOS users can run:
brew install ollama
Linux users can use the official install script. Before running it, open the script URL in a browser and review its contents:
curl -fsSL https://ollama.com/install.sh | sh
Windows users can download the installer from the Ollama website. After installing, check the version:
ollama --version
Note the version number. You will need it to check interface compatibility later.
2.2 Start the Service
The Ollama desktop app starts the service in the background automatically. If you installed from the command line, start it manually:
ollama serve
By default, Ollama listens on 127.0.0.1:11434 and accepts only local connections. Confirm that the service is running:
curl http://127.0.0.1:11434/api/version
A JSON response that includes a version number means the service is available. If you see connection refused, the service is not running, or another program is using the port.
2.3 Pull a Model
For programming tasks, use a code-focused model such as qwen3-coder. Pull the model:
ollama pull qwen3-coder
List the models installed on your machine:
ollama list
The first column shows the model name. The model name in your environment variables must match this column exactly, including the tag (for example, :latest).
Choose a model size that fits your hardware. If VRAM or RAM is insufficient, the model may fail to load or run very slowly. Prefer a version that fits entirely in VRAM.
3. Install the Coding Tool
Using Claude Code as the example, install it globally with npm:
npm install -g @anthropic-ai/claude-code
Confirm that the tool works:
claude --version
For other tools, see their official documentation. If the official source publishes a checksum or signature, verify the download before you run it.
4. Set Environment Variables
4.1 Anthropic-Compatible Path
If your Ollama version supports the Anthropic interface, use this configuration:
export ANTHROPIC_BASE_URL=http://127.0.0.1:11434export ANTHROPIC_AUTH_TOKEN=ollamaexport ANTHROPIC_MODEL=qwen3-coder
Notes on each variable:
ANTHROPIC_BASE_URLshould not include/v1. The Anthropic SDK appends/v1/messagesautomatically, so adding/v1produces an incorrect path.ANTHROPIC_AUTH_TOKENcan be any non-empty string. Ollama does not validate this token. It only satisfies the tool’s required-field check.ANTHROPIC_MODELmust match the model name shown byollama list.
4.2 OpenAI-Compatible Path
If your Ollama version is older, or the tool supports only the OpenAI format, use this configuration:
export OPENAI_BASE_URL=http://127.0.0.1:11434/v1export OPENAI_API_KEY=ollama
The address here must include /v1, because the OpenAI-compatible endpoint path is /v1/chat/completions. The tool must also accept the model name from an environment variable. Check the tool’s documentation for the variable name.
4.3 Check That the Variables Are Set
In the same terminal, run:
echo $ANTHROPIC_BASE_URLecho $ANTHROPIC_MODEL
If the output is empty, the variables are not set in the current session. To make them permanent, add the export lines to ~/.zshrc or ~/.bashrc, then open a new terminal.
5. Verify the API and Tool Connection
5.1 Test the Interface Directly
Use curl to confirm that the interface returns results before you troubleshoot the tool itself.
Test the Anthropic-compatible interface:
curl http://127.0.0.1:11434/v1/messages \-H "x-api-key: ollama" \-H "anthropic-version: 2023-06-01" \-H "content-type: application/json" \-d '{"model":"qwen3-coder","max_tokens":100,"messages":[{"role":"user","content":"Hello"}]}'
A JSON response that contains generated text means the interface works. A 404 means the current Ollama version does not support this endpoint. In that case, use the OpenAI path in Section 4 or the proxy in Section 7.
Test the OpenAI-compatible interface:
curl http://127.0.0.1:11434/v1/chat/completions \-H "Authorization: Bearer ollama" \-H "content-type: application/json" \-d '{"model":"qwen3-coder","messages":[{"role":"user","content":"Hello"}]}'
5.2 Send a Request Through the Tool
Claude Code’s non-interactive mode prints the result directly:
claude -p "Write a quicksort function in Python"
If code appears on screen, the full chain works. If the output is empty or shows an error, see the next section.
6. Troubleshooting Common Problems
| Symptom | Likely cause | Fix |
|---|---|---|
connection refused |
Ollama is not running, or the port is wrong | Run ollama serve, then check /api/version with curl |
| Interface returns 404 | The Ollama version does not support this endpoint path | Upgrade Ollama, or use the OpenAI-compatible path or a proxy |
| 401 or 403 | The credentials sent by the tool do not match what the proxy expects | Direct connections to Ollama do not validate tokens. Through a proxy, the token must match the proxy configuration |
model not found |
The model name does not match the local model | Run ollama list, then set ANTHROPIC_MODEL to the full name from the list |
| First reply is slow | The model must load from disk into memory or VRAM | This is normal. Later requests are much faster. You can adjust the keep-alive time (see Section 8) |
| Variables have no effect | The terminal was opened before the variables were set | Open a new terminal, or run source ~/.zshrc |
7. Use a Proxy When Direct Connection Fails
You need a proxy in these cases:
- Your Ollama version is too old to provide an Anthropic-compatible interface.
- The tool sends only Anthropic-format requests, but the inference server provides only the OpenAI format.
- You want a middle layer to handle logging, rate limiting, or authentication.
A common approach is to run a format-conversion proxy on your machine, such as an open-source project like claude-code-router. The proxy accepts Anthropic-format requests, converts them, and forwards them to http://127.0.0.1:11434/v1. Setup steps:
- Install the proxy by following its documentation. In its configuration file, set the upstream address to
http://127.0.0.1:11434/v1and the model name toqwen3-coder. - Start the proxy and note the address and port it listens on.
- Change
ANTHROPIC_BASE_URLto the proxy address, for examplehttp://127.0.0.1:<proxy-port>. Use the port from the proxy’s configuration. - Test again with the curl commands in Section 5, sending requests directly to the proxy address.
The proxy adds one network hop, so time to first token is usually slightly higher. If the latency is unacceptable, prefer an interface that the server supports natively.
8. Performance Tuning
Set a baseline before you tune. Send the same prompt three times before and after each change, and record the time to first token and the total response time. Without a baseline, you cannot tell whether a change helped.
8.1 Check Resource Usage
ollama ps
The PROCESSOR column shows how the model is split between GPU and CPU. A high CPU share noticeably slows inference. To fix this:
- Use a smaller model.
- Choose a quantized version. Tags containing
q4,q5, and similar markers are usually quantized and use less VRAM. Runollama show <model-name>to see the parameter count and quantization method. - Close other programs that use VRAM.
8.2 Adjust Concurrency and Keep-Alive
Set these environment variables before you start ollama serve:
export OLLAMA_NUM_PARALLEL=2export OLLAMA_KEEP_ALIVE=30m
OLLAMA_NUM_PARALLELsets how many requests one model can process at the same time. Increasing it raises VRAM use, so adjust it to fit your available VRAM.OLLAMA_KEEP_ALIVEsets how long a model stays in memory after it becomes idle
