Connecting a Coding Assistant to a Local LLM: A Complete Guide from Deployment to Connectivity

    This guide explains how to connect a coding assistant such as Claude Code to a locally running large language model via Ollama. It covers the overall architecture, environment setup, hardware and VRAM planning, model selection and download, four connection methods (Anthropic-compatible API, OpenAI-compatible API, a LiteLLM protocol-conversion proxy, and built-in assistant configuration), plus connectivity verification with curl.

    1. Understanding the Overall Pipeline

    A local setup consists of three parts. The first is the local inference service, which loads model weights, performs computation, and exposes an HTTP interface. The second is the model file, usually stored on disk in a quantized format such as GGUF and loaded into VRAM or RAM on demand. The third is the coding assistant, which runs in the terminal, reads files from the current project, sends your instructions and code snippets to the inference service, and displays the responses.

    Coding assistants connect to a cloud API by default, so pointing one at a local model requires changing three settings: the request URL, the protocol type, and the model name. If any of the three is mismatched, requests will fail.

    The protocol is where mistakes happen most often. Ollama listens on http://localhost:11434 by default and provides an OpenAI-compatible API at the path /v1/chat/completions. Anthropic-style assistants, on the other hand, send requests to /v1/messages. The two differ in both path and request body format. If you point an Anthropic-style assistant directly at the OpenAI endpoint, the requests usually cannot be parsed correctly.

    Newer versions of Ollama provide an Anthropic-compatible API. If your version supports it, you can configure the connection directly. If not, you need a protocol-conversion proxy between the assistant and the inference service. Check the official Ollama and assistant documentation for exact support details; Section 4 explains how to verify this.

    2. Preparing the Runtime Environment

    2.1 Installing the Inference Service

    On Linux or macOS, run the official Ollama installation script:

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

    Windows users can download the installer from the Ollama website. After installation, the service runs in the background. First, confirm the service is up:

    1. curl http://localhost:11434
    2. # Returns "Ollama is running" if the service is healthy

    Then check the version number, which is useful for troubleshooting later:

    1. ollama --version

    2.2 Installing the Coding Assistant

    This guide uses Claude Code as an example. It is distributed via npm:

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

    Older assistant versions may not support custom backends, so upgrade to the latest version before configuring anything.

    2.3 Evaluating Hardware and VRAM

    VRAM is the main performance bottleneck for local inference. The table below shows common estimates, assuming Q4-class quantization and a short context:

    Model size VRAM needed (quantized) Recommended hardware
    7B ~5–6 GB Entry-level GPU, or CPU inference with 16 GB RAM
    14B ~10–12 GB Mid-range GPU with 12+ GB VRAM
    32B ~20–24 GB High-end GPU or dual-GPU setup
    70B and above 40+ GB Multi-GPU workstation or a Mac with large unified memory

    These figures do not include the KV Cache. The longer the context, the more VRAM the KV Cache consumes. For example, raising the context from 4K to 32K noticeably increases memory usage. When VRAM runs out, the service offloads some layers to system RAM. This works, but at a clearly reduced speed.

    3. Selecting and Downloading a Model

    3.1 Selection Principles

    Coding scenarios demand different things from a model than casual chat. Focus on three factors when choosing.

    First, coding ability. Prefer models trained specifically on code corpora, such as the Coder variants within a model family.

    Second, context window. A coding assistant needs to fit multiple project files into context, so a context length of at least 32K is recommended.

    Third, the balance between parameter count and quantization level. Running a heavily quantized 32B model with degraded output quality is worse than running a 7B model at higher precision.

    3.2 Downloading and Managing Models

    The following commands pull a model and let you inspect its parameters:

    1. # Pull a 7B model well suited for coding
    2. ollama pull qwen2.5-coder:7b
    3. # List models available locally
    4. ollama list
    5. # Show model details: parameter count, quantization, context length
    6. ollama show qwen2.5-coder:7b
    7. # Remove a model you no longer need to free up disk space
    8. ollama rm qwen2.5-coder:7b

    After downloading, run a quick smoke test:

    1. ollama run qwen2.5-coder:7b "Write a quicksort in Python"

    If the reply is complete and the code runs correctly, the model loading and inference pipeline is working.

    4. Connecting the Coding Assistant to the Local Service

    This is the key step of the whole process. The core idea is to point the assistant’s request URL at the local service and make sure the protocol and model name match.

    Method 1: Anthropic-Compatible API

    If your Ollama version provides an Anthropic-compatible API, you can have Claude Code talk to the local service directly:

    1. export ANTHROPIC_BASE_URL=http://localhost:11434
    2. export ANTHROPIC_AUTH_TOKEN=ollama
    3. export ANTHROPIC_MODEL=qwen2.5-coder:7b
    4. claude

    Note that ANTHROPIC_BASE_URL should not include /v1. Claude Code appends /v1/messages itself. The local service usually does not validate the token, so the placeholder value ollama is fine. Do not put a real cloud API key into these variables.

    Method 2: OpenAI-Compatible API

    If the assistant supports the OpenAI protocol, you can use the OpenAI-compatible endpoint. Such assistants generally require the BASE_URL to include /v1:

    1. export OPENAI_BASE_URL=http://localhost:11434/v1
    2. export OPENAI_API_KEY=ollama
    3. export OPENAI_MODEL=qwen2.5-coder:7b

    Check the assistant’s documentation for the exact variable names. Use only one set of variables—do not mix the OpenAI and Anthropic sets at the same time.

    Method 3: Protocol-Conversion Proxy

    If the assistant can only speak the Anthropic protocol and your local Ollama version does not provide a matching endpoint, you need a protocol-conversion proxy. LiteLLM is used here as an example: it accepts Anthropic-format requests and converts them into a format Ollama can handle.

    1. pip install 'litellm[proxy]'
    2. litellm --model ollama/qwen2.5-coder:7b --port 8082

    Once started, the request chain looks like this:

    1. Claude Code → LiteLLM proxy (:8082) → Ollama service (:11434) → model

    Then point the assistant at the proxy port:

    1. export ANTHROPIC_BASE_URL=http://localhost:8082
    2. export ANTHROPIC_AUTH_TOKEN=ollama

    The proxy translates requests and forwards them to Ollama. The model name must be consistent on both sides.

    Method 4: Built-in Assistant Configuration

    Newer assistant versions let you select the local backend, service URL, and model name directly in the first-run setup wizard or in a configuration file, without setting environment variables. Refer to the assistant’s official documentation for the exact field names.

    Verifying Connectivity

    Before launching the assistant, test the inference service directly with curl. To test the OpenAI endpoint:

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

    If you are using the Anthropic-compatible API, also test /v1/messages:

    1. curl http://localhost:11434/v1/messages \
    2. -H "Content-Type: application/json" \
    3. -H "x-api-key: ollama" \
    4. -H "anthropic-version: 2023-06-01" \
    5. -d '{"model":"qwen2.5-coder:7b","max_tokens":128,"messages":[{"role":"user","content":"Hello"}]}'

    If either command returns a normal JSON response, the corresponding endpoint is working and the assistant can be pointed at it.