How to Use Claude Code With Other Models Claude Code shipped from Anthropic, but plenty of developers now point it somewhere else entirely. Teams route it to local models for privacy, cheaper providers for cost savings, or task-specific models that outperform Claude on narrow jobs like boilerplate generation.

The friction is real, though. A few years ago this meant fragile hacks and homemade adapters. Today it's cleaner, but you still need correctly configured environment variables, a model that speaks the right API format, and a clear read on which models support tool calling before trusting them with an agentic workflow.

The shift toward alternative models is accelerating. On OpenRouter, programming-related token volume grew from roughly 11% of total usage in early 2025 to over 50% in recent weeks, and open-weight models now account for close to 30% of platform activity (OpenRouter's State of AI report). Claude Code's own user base grew 10x between May and October 2025, according to TechCrunch. This isn't a niche workaround anymore.

This guide covers how to connect Claude Code to other models correctly, the kind of setup that holds up in daily dev work and team pipelines, not just a five-minute demo.

Key Takeaways

  • Claude Code connects to any Messages API–compatible endpoint via ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN
  • Setup options span local engines (Ollama, LM Studio), provider APIs (DeepSeek, GLM, Kimi), and gateways like FastRouter
  • Local coding work needs roughly 32GB RAM and a 24B+ parameter model to feel usable
  • Scaling past solo experimentation calls for centralized cost tracking and governance, not manual key-swapping
  • The /model command supports mid-session switching once gateway model discovery is enabled

When Should You Use Claude Code With Other Models?

Certain conditions determine whether switching pays off, regardless of your specific use case.

Strong fits:

  • Cost-sensitive experimentation where you're running hundreds of edits a day and Claude's per-token pricing adds up fast
  • Privacy-restricted or offline environments, such as regulated industries or air-gapped machines
  • Task-specific model access, like routing quick boilerplate to a fast, cheap model while saving Claude for architecture decisions

Common misuses:

  • Trusting a low-quality or undersized model for production code review, where a wrong edit costs more than the tokens you saved
  • Ignoring context window size or tool-calling support, which breaks agentic edits mid-session without warning
  • Running multiple provider accounts without centralized cost tracking, which hides runaway spend until the invoice arrives

Strong fits versus common misuses for multi-model Claude Code setups

A solo developer testing options on a laptop has very different needs than a team running Claude Code inside a CI/CD pipeline. Scale changes what "correct setup" even means.

Where Multi-Model Setups Show Up in Practice

Individual developers typically reach for local models when working offline, protecting client code, or simply staying within a tight budget. A freelancer on a train with no signal still wants a working coding assistant.

Startups and small teams tend to follow a different pattern. They route routine tasks (formatting, test generation, simple refactors) to cheaper models, then reserve premium models for complex reasoning and system design.

As that pattern scales across a team, manually juggling API keys per developer stops working. This is exactly why routing and governance tools like FastRouter start entering the conversation.

What You Need Before Connecting Claude Code to Other Models

A few prerequisites determine whether your setup works on the first try or burns an afternoon on debugging.

  • Claude Code CLI installed - the base requirement regardless of which model you connect
  • Sufficient hardware for local paths - a single RTX 4090 or 32GB of Mac RAM handles Mistral's 24B Devstral Small model, a reasonable floor for local coding (Mistral's model documentation)
  • Valid API keys or a running inference server - credentials for your provider, a unified gateway like FastRouter for multi-provider access, or a local server such as Ollama, LM Studio, or llama.cpp
  • Comfort with environment variables - you'll set and unset ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, and sometimes ANTHROPIC_MODEL
  • Confirmed tool-calling support - not every model handles function calling reliably, and agentic edits depend on it

Skip the last item and you'll get a model that ignores your file-edit instructions or hallucinates a tool call format Claude Code doesn't recognize.

How to Connect Claude Code to Other Models (Step-by-Step)

Connecting correctly follows a defined sequence. Skip a step, like forgetting to clear an old API key, and you'll get authentication errors or an agent that behaves strangely for reasons that aren't obvious from the error message.

Setup and Preparation

Pick one of three paths based on your priorities:

  1. Local inference engine (Ollama, LM Studio, llama.cpp) - best for privacy and zero per-token cost, at the price of managing your own hardware
  2. Direct provider API (DeepSeek, GLM, Kimi) - best when you want a specific model's pricing or quality without extra layers
  3. Unified gateway (FastRouter, OpenRouter, LiteLLM) - best when you need multiple models behind one endpoint without juggling separate configs

For local setups, pull a coding-capable model sized to your hardware. A 24B+ parameter model is the realistic floor for agentic coding; anything smaller tends to produce shakier edits. For provider APIs, match the model to your pricing and quality needs.

Watch for this common error: forgetting flags required for tool calling. llama.cpp needs --jinja enabled for tool use, and LM Studio recommends more than roughly 25K tokens of context for Claude Code sessions. Skip either and agentic edits fail silently instead of throwing a clear error.

Initiating the Connection

Initiation happens by exporting two variables:

export ANTHROPIC_BASE_URL="https://your-endpoint-here"
export ANTHROPIC_AUTH_TOKEN="your-token-here"

Clear ANTHROPIC_API_KEY if it's already set, since Claude Code checks both and a leftover key can cause conflicting authentication behavior.

There are two ways to do this. Persistent initiation adds the exports to your shell profile (.zshrc, .bashrc) so every session uses the alternative endpoint by default. One-off initiation exports inline for a single terminal session, paired with claude --model <model-name> for that run only.

You'll know it worked when the CLI starts cleanly, shows the expected model name, and returns a normal response on your first prompt. A 401 or 404 error on that first prompt means something in the variable setup is wrong.

Operating and Switching Between Models

Once connected, you can operate across models in two ways: pass --model per session, or use the in-session /model command when your gateway supports model discovery.

Two things matter most while switching: match the model's context window to task complexity, since a long refactor on a small-context model gets truncated silently, and watch provider-specific rate limits, which vary widely and can throttle mid-task.

Model choice is directly tied to output quality here. An undersized or mismatched model runs slower and produces more wrong edits and retries, quietly erasing whatever cost savings you were chasing in the first place.

This is where a unified control plane like FastRouter changes the workflow. Instead of manually managing separate base URLs and keys per provider, FastRouter offers an OpenAI-compatible layer that gives teams access to 100+ models through a single endpoint, with routing handled centrally rather than per developer.

Monitoring During Use

While running alternative models, keep an eye on:

  • Token usage and cost accumulation, especially with providers billed per-token
  • Malformed tool calls, which show up as broken diffs or edits that don't apply cleanly
  • Unusually slow responses, often a sign the model or hardware combination is a poor fit

Repeated retries, broken diffs, or a model that ignores your system prompt almost always trace back to missing or partial tool-calling support. That's a signal to switch models rather than keep retrying the same prompt.

Reverting or Completing a Session

Cleanly revert by unsetting both variables:

unset ANTHROPIC_BASE_URL
unset ANTHROPIC_AUTH_TOKEN

A simple shell function that runs both commands saves time if you switch back and forth often. Leaving stale variables set is one of the most common causes of confusing authentication errors, and it tends to surface later, in a completely unrelated project, when you least expect it.

5-step workflow for connecting Claude Code to alternative AI models

Best Practices for Using Claude Code With Multiple Models Effectively

A few habits separate a setup that holds up from one that quietly degrades over time.

  • Match the model to the task. Cheap, fast models handle boilerplate and repetitive edits well. Save premium models for architecture decisions and complex reasoning.
  • Keep credentials organized. Store keys in a secured secrets file, not hardcoded in shell configs, and rotate them on a schedule.
  • Test tool-calling compatibility first. Don't assume a new model handles function calling reliably; verify it before relying on it for agentic edits.
  • Centralize routing as usage grows. Scattered scripts and per-developer proxies don't scale.

That last point matters more as teams grow. Once you're past solo experimentation, tracking cost, applying guardrails, and running evaluations across models becomes a real operational need, not a nice-to-have.

A platform like FastRouter centralizes routing, spend limits, and audit logging at the gateway level. Policies then apply automatically across every project, instead of living in individual developers' shell configs.

Conclusion

Connecting Claude Code to other models comes down to disciplined sequencing. Get the environment variables right, pick a model suited to your hardware or budget, and confirm tool-calling support before you trust it with real work.

As usage grows from solo experiments to team-wide workflows, treat a well-managed multi-model setup as protection for both cost and flexibility, not just a way to save a few dollars on tokens. FastRouter, for example, routes Claude Code requests across 100+ models through one API with built-in failover and cost governance.

Frequently Asked Questions

Can Claude Code use models other than Claude?

Yes. Claude Code connects to any provider or proxy that exposes an Anthropic-compatible Messages API endpoint, including local models and third-party cloud providers.

Do I need an Anthropic API key to use Claude Code with other models?

No. You set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN to point at the alternative provider instead, and ANTHROPIC_API_KEY is typically left empty.

Which local models work best with Claude Code for coding tasks?

24B+ parameter coding-focused models, such as Devstral Small or Qwen3-Coder, tend to perform best on consumer hardware with at least 32GB RAM.

Can I switch models mid-session in Claude Code?

Yes, using the /model command when connected to a gateway with model discovery enabled. No restart is needed.

How much cheaper are alternative models compared to Claude's official models?

Alternative providers can be cheaper per token, sometimes by 20x or more depending on the model. Check current pricing pages for exact comparisons, since rates change often.

What's the difference between connecting directly to a provider versus using a gateway like FastRouter or LiteLLM?

Direct connections require managing separate keys and configs per provider. A gateway like FastRouter unifies multiple models behind one endpoint and typically adds routing, cost tracking, and governance on top.