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:

    1. 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:

    1. curl -fsSL https://ollama.com/install.sh | sh

    Windows users can download the installer from the Ollama website. After installing, check the version:

    1. 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:

    1. ollama serve

    By default, Ollama listens on 127.0.0.1:11434 and accepts only local connections. Confirm that the service is running:

    1. 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:

    1. ollama pull qwen3-coder

    List the models installed on your machine:

    1. 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:

    1. npm install -g @anthropic-ai/claude-code

    Confirm that the tool works:

    1. 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:

    1. export ANTHROPIC_BASE_URL=http://127.0.0.1:11434
    2. export ANTHROPIC_AUTH_TOKEN=ollama
    3. export ANTHROPIC_MODEL=qwen3-coder

    Notes on each variable:

    • ANTHROPIC_BASE_URL should not include /v1. The Anthropic SDK appends /v1/messages automatically, so adding /v1 produces an incorrect path.
    • ANTHROPIC_AUTH_TOKEN can be any non-empty string. Ollama does not validate this token. It only satisfies the tool’s required-field check.
    • ANTHROPIC_MODEL must match the model name shown by ollama list.

    4.2 OpenAI-Compatible Path

    If your Ollama version is older, or the tool supports only the OpenAI format, use this configuration:

    1. export OPENAI_BASE_URL=http://127.0.0.1:11434/v1
    2. export 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:

    1. echo $ANTHROPIC_BASE_URL
    2. echo $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:

    1. curl http://127.0.0.1:11434/v1/messages \
    2. -H "x-api-key: ollama" \
    3. -H "anthropic-version: 2023-06-01" \
    4. -H "content-type: application/json" \
    5. -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:

    1. curl http://127.0.0.1:11434/v1/chat/completions \
    2. -H "Authorization: Bearer ollama" \
    3. -H "content-type: application/json" \
    4. -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:

    1. 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:

    1. Your Ollama version is too old to provide an Anthropic-compatible interface.
    2. The tool sends only Anthropic-format requests, but the inference server provides only the OpenAI format.
    3. 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:

    1. Install the proxy by following its documentation. In its configuration file, set the upstream address to http://127.0.0.1:11434/v1 and the model name to qwen3-coder.
    2. Start the proxy and note the address and port it listens on.
    3. Change ANTHROPIC_BASE_URL to the proxy address, for example http://127.0.0.1:<proxy-port>. Use the port from the proxy’s configuration.
    4. 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

    1. 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. Run ollama 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:

    1. export OLLAMA_NUM_PARALLEL=2
    2. export OLLAMA_KEEP_ALIVE=30m
    • OLLAMA_NUM_PARALLEL sets 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_ALIVE sets how long a model stays in memory after it becomes idle