Claude Code + MegaBrain Gateway
Claude Code is Anthropic's official CLI coding agent — it reads and writes your files, runs terminal commands, and iterates autonomously until the task is done. With MegaBrain you can route it through any of 500+ models, not just Claude. One API key, no Anthropic account required.
Any model, same CLI
Switch between Claude, GPT-4o, Gemini, Llama and more without changing your workflow — just set a flag.
Unified billing
One MegaBrain balance covers Claude Code, Claude Desktop, and your own API calls. No per-product subscriptions.
No Anthropic account required
MegaBrain acts as the provider. You never need to sign up with Anthropic or manage their API keys.
Prerequisites
- Claude Code installed via npm —
npm install -g @anthropic-ai/claude-code - A MegaBrain API key — getmegabrain.com/profile
Setup (4 steps)
Install Claude Code
Install the CLI globally with npm. Node 18+ is required.
npm install -g @anthropic-ai/claude-codeVerify the install: claude --version
Set environment variables
Tell Claude Code to use MegaBrain as its backend by setting two environment variables in your shell session or profile:
export ANTHROPIC_BASE_URL="https://getmegabrain.com/api/anthropic"
export ANTHROPIC_API_KEY="mb-your-key"Replace mb-your-key with your actual key from the profile page.
Launch Claude Code
Navigate to your project directory and start the interactive session:
claudeClaude Code will connect through MegaBrain Gateway automatically. You'll see the model name in the session header.
Switch models at runtime (optional)
You can change the model without restarting. Inside an active session type:
/model anthropic/claude-opus-4-8Or pass a model at launch time with the --model flag:
claude --model openai/gpt-4oModel IDs follow the provider/model-name format. Browse all available models on the MegaBrain leaderboard.
/v1 to the base URL. Claude Code adds /v1 automatically. Use exactly https://getmegabrain.com/api/anthropic — adding /v1 yourself will produce a double path (/v1/v1/messages) and all requests will fail with 404.Persist settings in your shell
Add the following to your ~/.zshrc or ~/.bashrc so the environment variables are set automatically in every new terminal session:
# MegaBrain Gateway for Claude Code
export ANTHROPIC_BASE_URL="https://getmegabrain.com/api/anthropic"
export ANTHROPIC_API_KEY="mb-your-key"Then reload your profile: source ~/.zshrc (or source ~/.bashrc).
How Claude Code works
Reads & writes files
Directly edits your source files. No copy-paste required — changes land on disk immediately.
Runs terminal commands
Executes shell commands, runs tests, calls build tools, and reads their output to iterate.
Git-aware
Reads git history, diffs, and blame to understand context. Can stage, commit, and create branches.
Approval loop
Shows you every file write and shell command before running it. You stay in control.
MCP tools support
Extend Claude Code with Model Context Protocol servers — databases, APIs, custom tools.
Works with any MegaBrain model
The same approval loop and file-editing engine works regardless of which underlying model you choose.
Recommended models
These model IDs work well with Claude Code via MegaBrain. Pass any of them with --model or inside the session with /model:
# Anthropic (default family — best tool use)
anthropic/claude-sonnet-4-6 # Fast, balanced — good default
anthropic/claude-opus-4-8 # Most capable Claude — complex tasks
# OpenAI
openai/gpt-4o # Strong coding, fast
openai/o3 # Reasoning-focused
# Google
google/gemini-2.5-pro # 1M token context window
# Meta / open-source
meta-llama/llama-4-maverick # Open-weight, cost-efficient
# DeepSeek
deepseek/deepseek-r1 # Excellent at reasoning & mathTroubleshooting
HTTP 405 on GET /v1/models
Expected — MegaBrain does not implement the model discovery endpoint. Claude Code calls it on startup and logs a warning, but this does not affect inference. POST /v1/messages works normally. You can safely ignore the 405.
Requests fail with 404 or double /v1/v1/ in the URL
You appended /v1 to the base URL. Set ANTHROPIC_BASE_URL to https://getmegabrain.com/api/anthropic exactly — no trailing /v1. Claude Code appends /v1 itself before every request.
Cloud sessions not working — only local sessions work
Claude Code's cloud sessions run on Anthropic infrastructure and cannot reach a custom gateway URL. Local sessions (running on your own machine) work fine. Use local mode when routing through MegaBrain.
Model not found error
Model IDs must match the MegaBrain format exactly: provider/model-name (e.g. openai/gpt-4o, not gpt-4o). Check the MegaBrain leaderboard for the exact ID. Also verify your API key has sufficient balance.