Run Claude Code with Ollama or OpenRouter: Setup, Costs and Routing Checks

Published · Updated • guides
Claude CodeOllamaLLM Infrastructure

Claude Code can use a different model endpoint, but changing the endpoint does not make every model compatible or every session free. A local model uses your hardware. A hosted gateway uses remote infrastructure and may charge for usage. The model also needs the tool-calling and context capabilities required by your task.

Correction, 21 October 2026: The earlier version used obsolete gateway settings, described LiteLLM as a required bridge, and made unsupported speed, hardware and zero-egress claims. This revision follows the providers' integration documentation. It is a documentation review, not a measured comparison of model quality or performance.

Choose the inference boundary

CheckLocal Ollama modelOpenRouter gateway
Where model inference runsYour machine when you select locally stored weightsA remote provider selected through OpenRouter
What determines costHardware, electricity and operating time; a cloud model is a separate caseThe selected model, provider, usage and applicable limits
What to verifyLocal model selection, context configuration and usable tool behaviorEndpoint, authentication, supported provider and model compatibility
What routing alone cannot establishThat the CLI and every tool operate offlineThat a free model will support the complete Claude Code workflow

Local model inference stays within the machine boundary; tools and other network activity require separate checks.

A local inference route controls where model requests go. Claude Code can still invoke commands that access a network, and downloads, integrations or telemetry can have separate behavior. Review those paths before using proprietary code. Selecting an Ollama cloud model also changes the inference boundary.

Set up Ollama

Install Claude Code and Ollama using their official installation instructions. Confirm that Ollama is available in the shell where you will work:

ollama --version

Ollama's Claude Code integration provides a launcher that handles the connection and model selection:

ollama launch claude

Choose a model suited to coding and tool use. Check its download size and memory requirements against your machine. A model fitting in memory does not establish that it will handle a long repository task at an acceptable speed. Ollama recommends a context window of at least 64,000 tokens for this integration; memory use also depends on context and runtime settings.

If you need explicit environment configuration, Ollama documents this Bash setup:

export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_BASE_URL=http://localhost:11434
claude --model qwen3.5

The model must be available to Ollama. These are Bash commands; PowerShell uses different environment-variable syntax. The token value here is the local integration's placeholder, not a real Anthropic credential.

Run Claude Code's /status command to inspect its connection. Confirm that the chosen model is local rather than a cloud-tagged selection. Our longer Ollama setup walkthrough covers the local workflow in more detail.

Configure OpenRouter separately

Claude Code sends requests through a remote OpenRouter gateway to a selected provider.

Read OpenRouter's Claude Code integration instructions before choosing a model. Its documentation limits its compatibility guarantee to Anthropic first-party provider routes. A model appearing in the catalog does not establish that all Claude Code features work with that model.

Create an OpenRouter key in your own account and supply it through a local secret or environment variable named OPENROUTER_API_KEY. Do not paste it into a repository, shared terminal transcript or shell alias. In Bash, configure the gateway as follows:

export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""

The endpoint is the Anthropic-compatible gateway base, not the OpenAI-compatible /api/v1 endpoint used in the older guide. Clearing the API-key variable avoids a stale key conflicting with bearer-token authentication. Follow the provider's current model-selection instructions, then inspect /status before beginning work.

Check the actual provider, model availability, billing and data-handling settings in your account. A free catalog entry can have restrictions, and compatibility is a separate question from price. This guide does not promise free or unlimited access.

Test a small task before trusting a repository change

Use a disposable repository with a known failing test. Ask the agent to identify the failure, propose a small change, run the test and show its diff. This is a suggested evaluation procedure, not a test performed for this article.

Record whether the agent:

  • Reads the intended files and produces valid tool arguments.
  • Handles a failed command without inventing a successful result.
  • Keeps changes within the requested scope.
  • Runs the relevant test and accurately reports its output.

Measure elapsed time and usage for that task. Output tokens per second alone miss prompt processing, tool execution, retries and network delays. Comparing providers requires the same task, permissions and acceptance criteria.

Diagnose connection problems

For an authentication failure, check the environment in the current terminal and look for conflicting credentials. Do not print real tokens into a public issue. For a model error, verify the provider's exact model identifier and whether that route supports the integration.

For local slowness or context failures, inspect the model, context settings and available memory. Reducing model size may change task quality; increasing context may increase memory use. Avoid assuming a fixed RAM size implies a fixed throughput.

Keep gateway settings scoped to the terminal session while testing. When returning to another provider, remove the gateway overrides and follow that provider's normal sign-in instructions. Verify the connection again rather than assuming that closing a project changed the shell environment.

A useful setup is one whose route, permissions, cost and task results you can inspect. It does not require an unsupported claim that one model replaces another for a fixed percentage of an engineer's work.

Related Guides