
To run ollama claude code workflows entirely on local hardware, you connect Anthropic’s official Claude Code command-line interface to a local Ollama server using a protocol translation proxy. Because Claude Code communicates exclusively via Anthropic’s Messages API (/v1/messages) and requires native tool calling for file manipulation and command execution, a middleware adapter such as LiteLLM translates these requests into Ollama-compatible formats. This setup gives developers an autonomous, privacy-first terminal coding agent that operates with zero cloud API token costs.
Anthropic’s Claude Code has established itself as one of the most powerful agentic developer tools available. By running directly in the terminal, it reads file trees, executes ripgrep searches, runs test suites, and edits code across complex repositories. However, teams operating under strict data residency mandates or individual engineers managing thousands of routine edits often require complete offline isolation. Running Claude Code against local Ollama models provides total code privacy, offline operability, and predictable compute costs.
Prerequisites and System Requirements
Before connecting Claude Code to Ollama, verify that your workstation satisfies the minimum software and hardware specifications:
- Ollama Installed and Running: Ollama version 0.5.0 or later installed on Linux, macOS, or Windows WSL2. Ollama must be accessible at
http://localhost:11434. - Node.js 20+: Required to run the Claude Code CLI (
npm install -g @anthropic-ai/claude-code). - Python 3.10+: Required to install and run the LiteLLM proxy server with Anthropic endpoint support.
- GPU Hardware & VRAM: Running agentic coding models locally demands sufficient VRAM to hold both model weights and large context buffers. For detailed hardware breakdowns and benchmark trade-offs across consumer and workstation GPUs, see our comprehensive guide on the best local LLM for coding.
Selecting the Right Local Model for Claude Code
Claude Code is not a standard chat interface; it is an autonomous coding agent that issues structured tool calls (such as View, Edit, Bash, and GlobTool). If an LLM cannot reliably produce valid JSON function calls, the agent crashes or enters an infinite recovery loop.
Not every local model in the Ollama library is capable of driving Claude Code. The following table outlines how different model architectures handle agentic terminal workflows:
| Model Name | Parameter Size | Required VRAM | Tool Calling Reliability | Suitability for Claude Code |
|---|---|---|---|---|
| Qwen 2.5 Coder | 32B (Q4_K_M) | ~20 GB – 24 GB | Very High (9/10) | Recommended (Best all-around local agent) |
| Qwen 2.5 Coder | 14B (Q4_K_M) | ~10 GB – 12 GB | High (7.5/10) | Good (Capable for single-file and small refactors) |
| Llama 3.3 | 70B (Q4_K_M) | ~40 GB – 48 GB | Exceptional (9.5/10) | Excellent (Requires dual-GPU or Mac Studio) |
| DeepSeek-Coder-V2 | 16B (Lite) | ~12 GB – 14 GB | Moderate (6/10) | Experimental (Occasional tool formatting errors) |
| Models under 7B | 1B – 7B | < 6 GB | Poor (< 4/10) | Not Recommended (Fails structured tool calling) |
For most single-GPU workstations equipped with 24 GB of VRAM (such as an NVIDIA RTX 3090, 4090, or Apple Silicon Mac with unified memory), Qwen 2.5 Coder 32B provides the ideal balance of code comprehension, refactoring accuracy, and reliable JSON schema generation.
Step-by-Step Configuration Guide
Step 1: Download and Configure the Ollama Model
Pull the recommended coding model using the Ollama CLI:
ollama pull qwen2.5-coder:32b
By default, Ollama initializes models with a context window of 2,048 tokens (num_ctx 2048). Claude Code frequently transmits repository file paths, Git status outputs, and system prompt instructions that exceed 10,000 tokens on startup. If you leave Ollama at its default context length, requests will be silently truncated, causing tool calls to fail.
To increase the context length to 32,768 tokens, create a custom Modelfile:
# Create custom Modelfile for Claude Code
cat << 'EOF' > Modelfile
FROM qwen2.5-coder:32b
PARAMETER num_ctx 32768
PARAMETER temperature 0.2
EOF
# Build the custom model in Ollama
ollama create qwen2.5-coder-claudecode -f Modelfile
Verify that the new model exists by running ollama list. If you are comparing local inference architectures, our comparison of LM Studio vs Ollama breaks down runtime memory allocations and serving performance.
Step 2: Install and Launch the LiteLLM Proxy
Claude Code does not speak the Ollama REST protocol directly; it expects an Anthropic Messages API server at the endpoint specified by the ANTHROPIC_BASE_URL environment variable. You can deploy LiteLLM to serve as the translation bridge.
Install LiteLLM with proxy support in a dedicated Python environment:
# Create and activate a clean virtual environment
python3 -m venv ~/litellm-proxy
source ~/litellm-proxy/bin/activate
# Install LiteLLM with proxy capabilities
pip install 'litellm[proxy]'
Next, create a configuration file named config.yaml. This file instructs LiteLLM to intercept calls intended for Anthropic models (such as Claude 3.7 Sonnet or Claude 3.5 Sonnet) and route them to your local Ollama instance:
model_list:
# Map Claude 3.7 Sonnet requests to local Ollama
- model_name: claude-3-7-sonnet-20250219
litellm_params:
model: ollama_chat/qwen2.5-coder-claudecode
api_base: "http://localhost:11434"
# Map Claude 3.5 Sonnet fallback requests
- model_name: claude-3-5-sonnet-20241022
litellm_params:
model: ollama_chat/qwen2.5-coder-claudecode
api_base: "http://localhost:11434"
litellm_settings:
drop_params: true
set_verbose: false
Launch the LiteLLM proxy server on port 4000:
litellm --config config.yaml --port 4000
LiteLLM will start and output confirmation that it is listening on http://0.0.0.0:4000. Keep this terminal window open to monitor real-time request logs.
Step 3: Configure Environment Variables for Claude Code
Open a second terminal window. Configure the two environment variables required to redirect Claude Code away from Anthropic’s cloud and toward your local LiteLLM proxy:
# Point the API base to your local LiteLLM proxy
export ANTHROPIC_BASE_URL="http://localhost:4000"
# Provide a non-empty placeholder key to satisfy CLI initialization
export ANTHROPIC_API_KEY="sk-ollama-local-key"
For more details on proxy topologies and multi-model routing techniques, read our tutorial on deploying a Claude Code router for alternative backends.
Step 4: Launch and Verify the Connection
Navigate to an existing project directory and launch Claude Code:
cd ~/workspace/my-project
claude
Claude Code initializes without prompting for an Anthropic web login. Submit a simple test prompt that exercises both file reading and bash execution tools:
> Explain the structure of this repository and list the tracked git branches
Check the LiteLLM proxy terminal. You should see incoming POST /v1/messages requests translated immediately into Ollama chat completion calls. Claude Code will display the response and execute the corresponding terminal tools autonomously.
Verifying and Validating Local Execution
To confirm that your workflow is operating completely locally with zero outbound telemetry:
- Monitor GPU Activity: Run
nvidia-smi(on Linux/Windows) orpowermetrics --samplers gpu_power(on macOS) during prompt execution. You should observe continuous GPU memory allocation and compute utilization corresponding to Ollama’s inference process. - Inspect Ollama Server Logs: Run
journalctl -u ollama --no-pager -n 50or check Ollama terminal output to verify that prompt evaluations (eval count) and token generation rates match your hardware throughput. - Verify Tool Execution: Ask Claude Code to perform an explicit file creation:
> Create a file called hello.py that prints system information. Confirm that Claude Code executes the file tool, writes the script, and verifies its syntax.
Typical Errors and How to Fix Them
1. “Prompt Exceeds Maximum Context Window”
Cause: You are using the standard Ollama model without overriding num_ctx, leaving the model restricted to 2,048 tokens.
Fix: Build a custom model with a 32K or 64K context window using a Modelfile as described in Step 1. Ensure your GPU has sufficient VRAM to hold the expanded KV cache.
2. Malformed Tool Call JSON or Infinite Agent Loops
Cause: Smaller models (under 14B parameters) frequently fail to wrap tool calls in the exact JSON schema expected by the Claude Code CLI, resulting in parsing errors.
Fix: Switch to a model fine-tuned specifically for tool calling, such as qwen2.5-coder:32b. If hardware constraints limit you to smaller models, set the model temperature to 0.1 or 0.0 in your Modelfile to minimize output hallucinations.
3. “Invalid API Key” Authentication Error on Startup
Cause: The ANTHROPIC_API_KEY variable is either unset or completely empty. Claude Code performs client-side string validation before initiating network requests.
Fix: Ensure export ANTHROPIC_API_KEY="sk-ollama-local-key" is defined in your current shell session or added to ~/.bashrc.
Frequently Asked Questions (FAQ)
Can I use Claude Code with Ollama completely offline without an internet connection?
Yes. Once Ollama, the target model weights, LiteLLM, and the Claude Code CLI packages are installed locally, the entire inference and execution pipeline runs locally without accessing external cloud networks.
What is the minimum GPU VRAM required to use Claude Code with Ollama?
The minimum practical threshold is 12 GB of VRAM to run a quantized 14B coding model (such as Qwen 2.5 Coder 14B) alongside an expanded 32,768-token context window. For full repository reasoning and complex multi-file refactoring, 24 GB of VRAM running a 32B model is strongly recommended.
Does Ollama support Anthropic tool calling natively without LiteLLM?
No. Ollama implements its own native API and an OpenAI-compatible /v1/chat/completions endpoint, but it does not implement Anthropic’s proprietary Messages API (/v1/messages). A translation proxy such as LiteLLM is required to bridge the protocol differences.
Hi, I’m Mark, the author of Clever IT Solutions: Mastering Technology for Success. I am passionate about empowering individuals to navigate the ever-changing world of information technology. With years of experience in the industry, I have honed my skills and knowledge to share with you. At Clever IT Solutions, we are dedicated to teaching you how to tackle any IT challenge, helping you stay ahead in today’s digital world. From troubleshooting common issues to mastering complex technologies, I am here to guide you every step of the way. Join me on this journey as we unlock the secrets to IT success.


