Hijacking Claude Code
Point Claude Code at Ollama, LM Studio, or a multi-backend LiteLLM gateway.
"Hijacking" here means: keep using Claude Code's normal UX, but send model traffic to your machine (or team gateway) instead of Anthropic's first-party API.
- Ollama: prefer the official shortcut
ollama launch claude(and--model,--config,--yes) so env and Anthropic-compatible routing are set for you, see Claude Code · Ollama and Anthropic compatibility. - LM Studio: connect Claude Code directly to its Anthropic-compatible
/v1/messagesendpoint. - LiteLLM: use a gateway when you need multiple providers, fallbacks, centralized authentication, or metering.
0) Ollama: ollama launch claude (try this first)
If Ollama is your local runtime, use the integrated launcher instead of maintaining LiteLLM only for Claude Code:
ollama launch claude --model gemma4:e2bConfig without starting the app:
ollama launch claude --configManual env (same idea as the launcher), from Ollama's docs:
ANTHROPIC_AUTH_TOKEN=ollama ANTHROPIC_API_KEY="" ANTHROPIC_BASE_URL=http://localhost:11434 claude --model gemma4:e2bWhen this path works, you can skip the LiteLLM section below for Ollama-only setups. See Ollama for Gemma 4 E2B pull/run.
1) LM Studio direct
Start LM Studio’s server:
lms server start --port 1234Then point Claude Code at its Anthropic-compatible endpoint:
export ANTHROPIC_BASE_URL=http://localhost:1234
export ANTHROPIC_AUTH_TOKEN=lmstudio
export CLAUDE_CODE_ATTRIBUTION_HEADER=0
claude --model <exact-model-id-from-lm-studio>If Require Authentication is enabled, set ANTHROPIC_AUTH_TOKEN to your LM Studio API token. See LM Studio’s Claude Code integration.
What Claude Code actually needs
From Environment variables:
ANTHROPIC_BASE_URL: override the API host (proxies, LLM gateways, corporate forwarders).- When the host is not a first-party Anthropic endpoint, MCP tool search may be off unless you set
ENABLE_TOOL_SEARCH=true(only if your proxy forwardstool_referencecorrectly, read the same doc before enabling).
From LLM gateway:
- Gateways must support Anthropic Messages (or other documented provider shapes such as Bedrock or Google Cloud’s Agent Platform, not covered here).
- Headers and body fields must be forwarded faithfully or tools, streaming, and caching break in subtle ways.
From Third-party integrations:
ANTHROPIC_BASE_URL(and siblings like Bedrock/Vertex URLs) is how you centralize routing and auth for teams.
Authentication:
ANTHROPIC_API_KEY: direct Anthropicx-api-keystyle access.ANTHROPIC_AUTH_TOKEN: Bearer style, common when the gateway issues virtual keys, see Authentication for precedence.
Why LiteLLM still matters (multi-backend and teams)
LiteLLM exposes an endpoint that accepts Anthropic SDK traffic and routes to ollama_chat/..., openai/..., or other backends, see Configure Claude Code to Use LiteLLM Proxy. Use it when direct Ollama or LM Studio integration is not enough, for example for multiple providers, fallbacks, central auth, or metering.
Minimal env pattern from LiteLLM's tutorial:
export ANTHROPIC_BASE_URL="http://0.0.0.0:4000"
export ANTHROPIC_AUTH_TOKEN="$LITELLM_MASTER_KEY"(Use localhost if you prefer; match whatever litellm bind address you start.)
A more explicit variant from claude_code_byok-style docs includes custom headers for virtual keys, read LiteLLM Claude Code BYOK if your proxy requires x-litellm-api-key or similar.
2) Ollama + Gemma 4 E2B behind LiteLLM (optional)
Prefer ollama launch claude (section 0) when you only need Ollama + Claude Code. Use LiteLLM if you already run a proxy for other tools or need fallback models.
Start Ollama and ensure gemma4:e2b is pulled, see Ollama.
LiteLLM config.yaml (pattern from Ollama on LiteLLM; adjust model_name to whatever Claude Code will request, or add ANTHROPIC_CUSTOM_MODEL_OPTION, see below):
model_list:
- model_name: local-gemma4-e2b
litellm_params:
model: ollama_chat/gemma4:e2b
api_base: http://host.docker.internal:11434Use http://localhost:11434 when LiteLLM runs on the same host as Ollama; use host.docker.internal when LiteLLM runs inside Docker on macOS/Windows.
Start the proxy (see LiteLLM proxy quick start):
uv tool install 'litellm[proxy]'
litellm --config /path/to/config.yamlPoint Claude Code at the proxy with the ANTHROPIC_BASE_URL / ANTHROPIC_AUTH_TOKEN exports from the previous section.
3) LM Studio behind LiteLLM (optional)
Direct integration is simpler for one machine. Keep this gateway pattern only when you need LiteLLM’s routing, auth, or metering. Start LM Studio's server on http://localhost:1234/v1, see LM Studio.
LiteLLM config.yaml (pattern from proxy configs for openai/ + api_base):
model_list:
- model_name: local-lms-gemma
litellm_params:
model: openai/<exact-model-id-from-lm-studio>
api_base: http://localhost:1234/v1
api_key: lm-studioReplace <exact-model-id-from-lm-studio> with the string LM Studio shows (often includes publisher and quantization). api_key should match whatever LM Studio expects locally (dummy lm-studio is common).
Restart LiteLLM, then set ANTHROPIC_BASE_URL / auth env vars for Claude Code as above.
Model names Claude Code sends
Prefer gateway model discovery when the gateway exposes a compatible model catalog:
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1If discovery is unavailable, either configure LiteLLM model_name entries
that match the Anthropic-style IDs Claude Code emits, or add a custom picker
entry with ANTHROPIC_CUSTOM_MODEL_OPTION (and its _NAME /
_DESCRIPTION variables) per
Model configuration.
Example custom picker line from the same doc:
export ANTHROPIC_CUSTOM_MODEL_OPTION="my-gateway/local-coder"
export ANTHROPIC_CUSTOM_MODEL_OPTION_NAME="Local coder via Gateway"
export ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION="Routed through internal LiteLLM"Adapt my-gateway/... to the model_name you defined in config.yaml.
For an unknown model ID whose context window differs from Claude Code’s
assumption, also set CLAUDE_CODE_MAX_CONTEXT_TOKENS to the backend’s
actual supported limit.
Operational warnings (read before a workshop)
- Capability gap: Gemma 4 E2B is useful for latency and offline demos, not guaranteed parity with Claude on long-horizon refactors or fragile tool loops.
- Tool use & JSON: smaller models may hallucinate tool arguments; keep permissions strict.
- Secrets: never commit
.envwith real gateway keys; use throwawayLITELLM_MASTER_KEYfor local class labs. - Support: Anthropic may not support misconfigured gateways, treat this as advanced self-hosted wiring.
References (official)
- Claude Code LLM gateway
- Claude Code environment variables
- LiteLLM + Claude Code
- Ollama · Claude Code integration
- Ollama Anthropic compatibility
- Ollama OpenAI compatibility
- LM Studio Claude Code integration
- LM Studio Anthropic compatibility
- LM Studio OpenAI compatibility