Self-Hosting Claude Code: 5 Ways to Run It on Your Own Infra

TL;DR
Run Claude Code through your own gateway, Bedrock, Google Cloud or local GPUs. Five patterns with verified env vars, model IDs and what Anthropic supports.
You can run Claude Code without sending a single request to api.anthropic.com: point it at Amazon Bedrock or Google Cloud's Agent Platform with one environment variable, put your own gateway in front of it with ANTHROPIC_BASE_URL, or run Anthropic's own self-hosted Claude apps gateway with corporate SSO. Those three paths are supported by Anthropic. Routing Claude Code to non-Claude models (local Qwen, DeepSeek, anything behind Ollama) also works through community routers, but Anthropic's docs say plainly that it does not support that setup.
Last updated: October 9, 2026 - model IDs moved to the Claude 5.5 family, Vertex AI is now Google Cloud's Agent Platform, Pattern 3 now leads with Anthropic's Claude apps gateway, Claude Code Router moved to its 3.x UI-driven setup, and the air-gapped stack uses a real Qwen3-Coder checkpoint. On this date we checked the model IDs against Anthropic's models overview and AWS's Bedrock model cards, the environment variables and defaults against the Claude Code docs, the LiteLLM image tag and Claude Code Router version against their GitHub releases pages, and the retention terms against Anthropic's and AWS's retention docs. The commands come from each project's own docs; we did not run every stack end to end.
The default story is simple: install the CLI, log in with a Claude account, and your prompts go straight to Anthropic. That works for individual developers. It does not work for regulated teams, enterprises with data residency rules, or anyone who wants per-developer attribution and budgets. If you have already hit the usage limits wall, these patterns are the next step.
How Claude Code Talks to a Backend#
Claude Code picks its backend from environment variables. For any gateway that speaks the Anthropic Messages API:
# Point Claude Code at your gateway
export ANTHROPIC_BASE_URL="https://llm-gateway.example.com"
# The credential your gateway issued (sent as Authorization: Bearer)
export ANTHROPIC_AUTH_TOKEN="sk-gateway-key"
The gateway has to serve /v1/messages (and optionally /v1/messages/count_tokens) and forward the anthropic-version and anthropic-beta headers unchanged. Stripping anthropic-beta is the most common reason a homegrown proxy breaks newer Claude Code features. Cloud platforms have their own switches (CLAUDE_CODE_USE_BEDROCK=1, CLAUDE_CODE_USE_VERTEX=1) and their own base-URL variables (ANTHROPIC_BEDROCK_BASE_URL, ANTHROPIC_VERTEX_BASE_URL) if a gateway sits in front of them.
One billing detail matters: setting ANTHROPIC_BASE_URL alone does not replace a saved claude.ai login. Requests still route through your gateway, but the subscription stays the active credential until you also set a gateway credential such as ANTHROPIC_AUTH_TOKEN or an apiKeyHelper. Run /status inside Claude Code to see which base URL and credential it is actually using.
For corporate networks, HTTPS_PROXY and NODE_EXTRA_CA_CERTS handle outbound proxies and custom certificate authorities.
Pattern 1: LiteLLM Proxy for Cost Tracking#
The simplest pattern. You run LiteLLM as a proxy on port 4000, point Claude Code at it, and get per-key budgets, request logs, and fallback between Anthropic and a cloud provider. It is an afternoon of setup.
Install the proxy:
uv tool install 'litellm[proxy]'
# litellm-config.yaml
model_list:
- model_name: claude-opus-5-5
litellm_params:
model: anthropic/claude-opus-5-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: claude-sonnet-5-5
litellm_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: claude-haiku-5-5
litellm_params:
model: bedrock/invoke/us.anthropic.claude-haiku-5-5
aws_region_name: us-east-1
general_settings:
master_key: os.environ/LITELLM_MASTER_KEY
Run it and point Claude Code at it:
litellm --config litellm-config.yaml # listens on http://0.0.0.0:4000
export ANTHROPIC_BASE_URL="http://localhost:4000"
export ANTHROPIC_AUTH_TOKEN="$LITELLM_MASTER_KEY" # or a LiteLLM virtual key
export ANTHROPIC_DEFAULT_OPUS_MODEL="claude-opus-5-5"
export ANTHROPIC_DEFAULT_SONNET_MODEL="claude-sonnet-5-5"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="claude-haiku-5-5"
claude
Two settings from LiteLLM's own Claude Code guide save a debugging session. Claude Code turns MCP tool search off when the base URL is not a first-party Anthropic host, so set ENABLE_TOOL_SEARCH=true or every MCP tool schema lands in your context window. And for Bedrock-backed models, set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1, because Bedrock can reject some of the experimental beta headers Claude Code sends.
Virtual keys are the real win: one key per developer means per-person spend and budgets without handing anyone the upstream Anthropic key. The open-source proxy covers routing, keys and budgets; SSO and audit logs are LiteLLM Enterprise features. For how LiteLLM compares with managed gateways, see our self-hosted vs managed AI gateway guide.
Pattern 2: Bedrock or Google Cloud for Compliance#
If code cannot go to Anthropic directly, Amazon Bedrock and Google Cloud's Agent Platform (the new name for Vertex AI) both host the same Claude models and bill through your existing cloud account. Claude Code supports both natively, and both now ship a login wizard: run claude, pick 3rd-party platform, and it verifies which models your account can invoke and writes the pins to ~/.claude/settings.json. Re-run it later with /setup-bedrock or /setup-vertex.
For scripted rollouts, set the variables yourself. Bedrock:
export CLAUDE_CODE_USE_BEDROCK=1
export AWS_REGION=us-east-1 # optional if your AWS profile sets a region
export ANTHROPIC_DEFAULT_OPUS_MODEL='us.anthropic.claude-opus-5-5'
export ANTHROPIC_DEFAULT_SONNET_MODEL='us.anthropic.claude-sonnet-5-5'
export ANTHROPIC_DEFAULT_HAIKU_MODEL='us.anthropic.claude-haiku-5-5'
claude
Google Cloud's Agent Platform:
export CLAUDE_CODE_USE_VERTEX=1
export CLOUD_ML_REGION=global
export ANTHROPIC_VERTEX_PROJECT_ID=your-gcp-project
export ANTHROPIC_DEFAULT_OPUS_MODEL='claude-opus-5-5'
export ANTHROPIC_DEFAULT_SONNET_MODEL='claude-sonnet-5-5'
claude
Authentication uses the standard AWS credential chain (profiles, SSO, or a Bedrock API key in AWS_BEARER_TOKEN_BEDROCK) or Google Application Default Credentials. Logs flow into CloudTrail or Cloud Audit Logs. The Bedrock IDs above use the us. cross-region inference profile prefix; adjust it for your region, or set ANTHROPIC_BEDROCK_REGION_PREFIX to keep the built-in defaults with a different prefix.
Pin your models. On both platforms the unpinned primary model is Opus 5.5, but the unpinned sonnet alias still resolves to Sonnet 4.5 and background tasks use Sonnet 4.5 too, because Haiku may not be enabled in every account. If you want Sonnet 5.5 or Haiku 5.5, pin them explicitly. Opus is also billed at a higher rate than Sonnet, so an unpinned rollout costs more than many teams expect.
Two caveats. Prompt caching is on by default on both platforms, but Bedrock does not offer it in every region, so check that cache token counts are not stuck at zero. And Anthropic's covered models, the Fable line, need 30-day retention turned on before an organization running with zero data retention (ZDR) can call them on Bedrock or Google Cloud; accounts already on standard retention see no change. Anthropic's retention policy says the retained data stays in AWS or GCP. On Bedrock, that matches what AWS documents today: its Data Retention API docs say Bedrock does not share content with model providers, so both the aws_review mode and the legacy provider_data_share mode keep prompts and outputs inside the AWS boundary for up to 30 days. That is narrower than AWS's June launch post, which warned that opting into provider_data_share meant data would leave AWS's boundary. That launch-day version is what we covered in Fable 5 on AWS Bedrock; check the current AWS page before your security review, because the condition is AWS's to change.
Pattern 3: An SSO Gateway With Audit Logs#
For centralized identity, per-developer attribution and audit logs, you now have two options: Anthropic's own gateway, or one you build.
Claude apps gateway. Anthropic ships a self-hosted gateway inside the claude binary itself. The same executable that runs Claude Code on a laptop runs the server:
claude gateway --config gateway.yaml
Developers sign in with your corporate identity provider over OIDC and get short-lived bearer tokens, so nobody holds an API key and offboarding happens in your IdP (access expires within the session lifetime, one hour by default). IdP groups map to model allowlists and managed-settings policies that the gateway enforces server-side. Usage telemetry goes to your own OpenTelemetry collector with user identity, model and token counts. Upstream, it translates to Bedrock, Claude Platform on AWS, Google Cloud's Agent Platform, Microsoft Foundry or the Anthropic API, with failover between them. Auth state lives in PostgreSQL, and Anthropic's deployment guide covers Kubernetes and Cloud Run.
Know the limits before you commit: OIDC only (front SAML or LDAP with an OIDC bridge), OTLP over HTTP only, and the 1-hour prompt cache TTL is not available through it, so caching uses the 5-minute TTL.
Build your own. If you need custom logic, the classic pattern still works: a local proxy fetches an OIDC token, Identity-Aware Proxy validates it against your Google Workspace group, and a small service logs the request and forwards it upstream.
[Developer machine]
-> Local proxy (Claude Code calls this)
-> [Identity-Aware Proxy] (Google Workspace SSO)
-> [FastAPI gateway on Cloud Run]
-> Anthropic API or Bedrock
The skeleton of the gateway service, forwarding the headers Claude Code needs:
# gateway.py
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse
import httpx
import os
app = FastAPI()
ANTHROPIC_KEY = os.environ["ANTHROPIC_API_KEY"]
@app.post("/v1/messages")
async def messages(request: Request):
user = request.headers.get("X-Goog-Authenticated-User-Email")
if not user:
raise HTTPException(401, "missing identity")
body = await request.body()
# Log who, which session, which model
log_request(
user=user,
session=request.headers.get("x-claude-code-session-id"),
body=body,
)
headers = {
"x-api-key": ANTHROPIC_KEY,
"anthropic-version": request.headers.get("anthropic-version", "2023-06-01"),
"content-type": "application/json",
}
# Forward anthropic-beta verbatim; never allowlist individual values
if beta := request.headers.get("anthropic-beta"):
headers["anthropic-beta"] = beta
async def upstream():
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream(
"POST",
"https://api.anthropic.com/v1/messages",
params=request.query_params,
content=body,
headers=headers,
) as r:
async for chunk in r.aiter_raw():
yield chunk
return StreamingResponse(upstream(), media_type="text/event-stream")
The x-claude-code-session-id header groups every request from one session, and x-claude-code-agent-id attributes cost to individual subagents. To stop developers bypassing either kind of gateway, set allowedProviders to ["customEndpoint"] in managed settings with the gateway URL in the same file's env block (Claude Code v2.1.285 or later); Claude Code then refuses sessions pointed anywhere else.
Pattern 4: Other Models via Claude Code Router#
The community Claude Code Router project translates Claude Code's requests to other providers: OpenRouter, DeepSeek, Moonshot, Z.ai, Mistral, local OpenAI-compatible servers and more. Release 3.1.2 shipped on October 8, 2026, and the 3.x line works differently from older guides. Configuration lives in a desktop app or a browser UI, not a hand-edited JSON file.
# Node.js 22 or newer
npm install -g @musistudio/claude-code-router
ccr ui # management UI on http://127.0.0.1:3458
In the UI, add a provider and its models, create a CCR client key, set the default model plus routing rules and fallbacks, and start the gateway. It listens on http://127.0.0.1:3456. Then either point Claude Code's ANTHROPIC_BASE_URL at that address, or save a Claude Code profile under Agent Config and launch it by name:
ccr "Claude - Review" cli -- --model sonnet
The Logs view shows the resolved provider, model, tokens, latency and estimated cost for every request, which is the fastest way to see what a routing rule actually did.
Be clear-eyed about the trade. Anthropic does not support routing Claude Code to non-Claude models through any gateway, so tool calling, thinking and newer features can break in ways no one will fix for you. It is a good fit for cheap routine work with a strong open model, such as GLM-5.3, and a poor fit for anything you cannot afford to debug. We looked at the wider pattern in Free Claude Code is really a model gateway bet.
Pattern 5: Air-Gapped on Your Own GPUs#
The most extreme version. You serve an open-weight coding model on your own GPUs behind an Anthropic-format adapter, and nothing leaves the VPC. This is what some defense, healthcare and financial teams require, and it inherits Pattern 4's caveat: it is outside what Anthropic supports.
Stack:
- Model: Qwen3-Coder-30B-A3B-Instruct (30.5B total, 3.3B active, 262,144-token native context), served by vLLM with Qwen's
qwen3_codertool-call parser. Claude Code lives on tool calls, so the parser is not optional. - Adapter: LiteLLM in proxy mode, exposing an Anthropic-format endpoint in front of vLLM's OpenAI-compatible API.
- Network: all traffic stays inside the VPC, with no egress rule to api.anthropic.com.
Minimal docker compose:
services:
vllm:
image: vllm/vllm-openai:latest # pin a release tag in production
command:
- --model=Qwen/Qwen3-Coder-30B-A3B-Instruct
- --max-model-len=131072
- --tensor-parallel-size=2
- --enable-auto-tool-choice
- --tool-call-parser=qwen3_coder
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 2
capabilities: [gpu]
ports:
- "8000:8000"
litellm:
image: ghcr.io/berriai/litellm:v1.104.2 # latest stable release on Oct 9, 2026; pin a current stable tag, not latest
environment:
LITELLM_MASTER_KEY: sk-internal
volumes:
- ./litellm-config.yaml:/app/config.yaml
command: ["--config", "/app/config.yaml", "--port", "4000"]
ports:
- "4000:4000"
And the LiteLLM side, using its hosted_vllm/ provider prefix:
# litellm-config.yaml
model_list:
- model_name: qwen3-coder
litellm_params:
model: hosted_vllm/Qwen/Qwen3-Coder-30B-A3B-Instruct
api_base: http://vllm:8000/v1
Developers connect like this:
export ANTHROPIC_BASE_URL="https://internal-claude.corp.example.com"
export ANTHROPIC_AUTH_TOKEN="$INTERNAL_TOKEN"
export ANTHROPIC_MODEL="qwen3-coder"
claude
You give up a lot of quality against Claude Sonnet 5.5 or Opus 5.5. A 3B-active model handles refactors, test writing and routine feature work; it struggles on long multi-step agent runs. Before buying GPUs for this, run the numbers in our self-hosting break-even math. Cheap local inference does make fan-out architectures affordable, which pairs well with multi-agent workflows in Claude Code.
What to Pick#
| Need | Pattern |
|---|---|
| Cost tracking and per-developer budgets | LiteLLM proxy (Pattern 1) |
| Compliance, AWS or GCP shop, Claude models only | Bedrock or Google Cloud's Agent Platform (Pattern 2) |
| SSO, audit logs and per-group model policy for the whole org | Claude apps gateway, or your own IAP gateway (Pattern 3) |
| Route routine tasks to cheaper non-Claude models | Claude Code Router (Pattern 4, unsupported by Anthropic) |
| Air-gapped, nothing may leave the network | vLLM plus LiteLLM on your GPUs (Pattern 5, unsupported by Anthropic) |
Most teams should start with Pattern 2 if they already live in AWS or GCP, and Pattern 1 if they do not. Both are reversible and tell you whether your usage justifies the heavier options. Organizations that need SSO should look at the Claude apps gateway before writing their own: it is tested alongside each Claude Code release, so it forwards new headers without anyone maintaining an allowlist.
The Bigger Picture#
Claude Code is opinionated about how it works on your machine (the subagent system, hooks, worktrees) and flexible about where its Claude requests go. Anthropic now documents that flexibility as a product: four cloud platforms, a gateway protocol spec for third-party gateways, and its own self-hosted gateway. The line it draws is at the model, not the endpoint. Claude models through any compliant path are supported; other models through Claude Code are your own experiment.
That makes the self-hosting story one of Claude Code's stronger enterprise arguments, and the place to start a platform team conversation. Pair it with our AI coding tools comparison matrix when you make the case.
Frequently Asked Questions#
Can I run Claude Code without calling Anthropic's API directly?#
Yes. Set CLAUDE_CODE_USE_BEDROCK=1 or CLAUDE_CODE_USE_VERTEX=1 to use Amazon Bedrock or Google Cloud's Agent Platform, or set ANTHROPIC_BASE_URL plus a gateway credential such as ANTHROPIC_AUTH_TOKEN to use any gateway that speaks the Anthropic Messages API and forwards the anthropic-version and anthropic-beta headers.
What is the easiest way to self-host Claude Code for cost tracking?#
A LiteLLM proxy. Install it with uv tool install 'litellm[proxy]', run litellm --config litellm-config.yaml, then set ANTHROPIC_BASE_URL=http://localhost:4000 and ANTHROPIC_AUTH_TOKEN to a LiteLLM master or virtual key. Virtual keys give you per-developer spend tracking and budgets.
How do I use Claude Code with AWS Bedrock?#
Run claude, choose 3rd-party platform then Amazon Bedrock, and the wizard handles credentials, region and model pins. For scripted setups, set CLAUDE_CODE_USE_BEDROCK=1, optionally AWS_REGION, and pin models with ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL and ANTHROPIC_DEFAULT_HAIKU_MODEL, for example us.anthropic.claude-sonnet-5-5. Authentication uses your standard AWS credential chain.
Does Anthropic offer its own self-hosted gateway for Claude Code?#
Yes. The Claude apps gateway is built into the claude binary and runs with claude gateway --config gateway.yaml. Developers sign in through your OIDC identity provider, IdP groups map to model allowlists and managed settings, telemetry goes to your OpenTelemetry collector, and it forwards to Bedrock, Claude Platform on AWS, Google Cloud's Agent Platform, Microsoft Foundry or the Anthropic API.
Can I use Claude Code with open-source or non-Claude models?#
Technically yes, through community routers such as Claude Code Router or a LiteLLM proxy in front of vLLM or another provider. Anthropic does not support routing Claude Code to non-Claude models through any gateway, so expect gaps in tool calling and newer features, and keep a Claude path for the hard problems.
What do I need for a fully air-gapped Claude Code deployment?#
An open-weight coding model served on your own GPUs, for example Qwen3-Coder-30B-A3B-Instruct on vLLM with the qwen3_coder tool-call parser, plus LiteLLM in front to expose an Anthropic-format endpoint. All traffic stays inside the VPC. Quality is well below Claude Sonnet 5.5 or Opus 5.5, and the setup is outside what Anthropic supports.
Does self-hosting Claude Code affect prompt caching?#
Claude Code enables prompt caching automatically on Bedrock and Google Cloud's Agent Platform, and ENABLE_PROMPT_CACHING_1H=1 requests the 1-hour TTL at a higher write price. Bedrock does not offer caching in every region, so check that cache token counts are not stuck at zero. Through the Claude apps gateway, only the 5-minute TTL is available.
Which self-hosting pattern should my team start with?#
Start with Bedrock or Google Cloud if you already run on AWS or GCP, because it needs no new infrastructure. Otherwise start with a LiteLLM proxy for budgets and attribution. Move to the Claude apps gateway when you need SSO, per-group policy and audit telemetry for the whole organization.
Sources#
- Claude Code on Amazon Bedrock - setup wizard, credentials, model pinning and defaults
- Claude Code on Google Cloud's Agent Platform - setup, regions, model pinning and prompt caching
- Other LLM gateways and Connect Claude Code to an LLM gateway - base URL, credentials, subscription behaviour, support boundary
- Gateway compatibility guide - endpoints and headers a gateway must forward
- Claude apps gateway - Anthropic's self-hosted SSO gateway
- Claude models overview - current model IDs for the Claude API, Bedrock and Google Cloud
- AWS Bedrock model cards for Opus 5.5, Sonnet 5.5 and Haiku 5.5 -
us.andglobal.inference profile IDs - Data retention for covered models - 30-day retention on Bedrock and Google Cloud for ZDR organizations
- Amazon Bedrock data retention -
aws_reviewand legacyprovider_data_sharemodes, both retained inside the AWS boundary - LiteLLM Claude Code quickstart, LiteLLM vLLM provider, LiteLLM production deployment and LiteLLM releases
- Claude Code Router, its releases and its CLI reference
- Qwen3-Coder-30B-A3B-Instruct model card
Continue Reading#
Get the next deep dive like this in your inbox
One email a week on Claude Code and the rest of the AI dev stack. Free.
Read next on Claude Code
Self-Hosted vs Managed AI Gateways: A Decision Guide
Self-host LiteLLM or Kong, or use a managed gateway like Portkey, OpenRouter, or Cloudflare AI Gateway? A factual breakdown of cost, control, and ops tradeoffs.
11 min readClaude Code Usage Limits in 2026: The Practical Playbook for Pro and Max Teams
A practical operational guide to Claude Code usage limits in 2026: plan behavior, API key pitfalls, routing choices, and team controls using hooks and subagents.
9 min readSelf-Hosting Open-Weights Models: The Real Break-Even Math
Open weights are free to download, but inference is not free to run. Here is the honest break-even math on when self-hosting GLM-5.2, DeepSeek V4, or Llama beats paying per-token API prices - GPU rental and ownership costs, real throughput, utilization, the crossover in tokens per month, and the hidden ops bill nobody budgets for.
11 min readTechnical content at the intersection of AI and development. Building with AI agents, Claude Code, and modern dev tools - then showing you exactly how it works.







