MegaBrain University · guide

How to Use Claude Code Without a Subscription: Pay-per-Token Claude via One API Key

Updated · 5 min setup

Short answer

To run Claude Code on MegaBrain, set ANTHROPIC_BASE_URL to https://getmegabrain.com/api/anthropic (without /v1), put your MegaBrain key in ANTHROPIC_AUTH_TOKEN and pick the model with ANTHROPIC_MODEL=anthropic/claude-sonnet-5.5, in the env block of ~/.claude/settings.json. Claude Code then runs on Claude Opus, Sonnet and Haiku paid per token from your balance, with no Claude subscription.

Connection settings
Base URL (OpenAI-compatible)https://getmegabrain.com/api/gateway/v1
Base URL for Claude Code and the Anthropic SDKhttps://getmegabrain.com/api/anthropic
KeyAuthorization: Bearer <key> — the API key page in your dashboard
Default modelanthropic/claude-sonnet-5.5
Free model for testingmb-auto/free

What is Claude Code

Claude Code is Anthropic’s AI coding agent. It runs in the terminal, in VS Code and JetBrains, and headless in scripts and CI: it reads the codebase, edits files, runs commands and tests, uses subagents, hooks, skills and MCP servers.

Claude Code normally signs in with a Claude subscription or an Anthropic Console key, but it also supports an LLM gateway: any server that speaks the Anthropic Messages API, set with ANTHROPIC_BASE_URL. MegaBrain is one. You pay per token from one balance shared with every other model, and each developer gets a key whose usage you can see. Anthropic supports Claude Code only on Claude models, so this guide uses Claude Opus, Sonnet and Haiku.

Setup, step by step

  1. Step 1. Get a MegaBrain API key

    Sign up with GitHub, Google or email, add funds on the Balance page and create a key on the API key page. The key works for every text, image and video model, and each request is paid from your balance — no subscription.

    Put the key in an environment variable so it never lands in a config file you share:

    # ~/.zshrc or ~/.bashrc
    export MEGABRAIN_API_KEY="your-megabrain-key"
    curl https://getmegabrain.com/api/gateway/v1/chat/completions \
      -H "Authorization: Bearer $MEGABRAIN_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{"model": "anthropic/claude-sonnet-5.5", "messages": [{"role": "user", "content": "Say OK"}]}'

    Check the key before you touch the agent. A JSON reply with choices means the key, the balance and the model all work:

  2. Step 2. Install Claude Code

    On macOS, Linux or WSL:

    curl -fsSL https://claude.ai/install.sh | bash

    On Windows (PowerShell): irm https://claude.ai/install.ps1 | iex. With npm: npm install -g @anthropic-ai/claude-code.

  3. Step 3. Point Claude Code at MegaBrain

    Add an env block to ~/.claude/settings.json (it applies to every project and to background agents). The base URL has no /v1: Claude Code adds /v1/messages itself.

    {
      "env": {
        "ANTHROPIC_BASE_URL": "https://getmegabrain.com/api/anthropic",
        "ANTHROPIC_AUTH_TOKEN": "your-megabrain-key",
        "ANTHROPIC_MODEL": "anthropic/claude-sonnet-5.5",
        "ANTHROPIC_DEFAULT_OPUS_MODEL": "anthropic/claude-opus-5.5",
        "ANTHROPIC_DEFAULT_SONNET_MODEL": "anthropic/claude-sonnet-5.5",
        "ANTHROPIC_DEFAULT_HAIKU_MODEL": "anthropic/claude-haiku-5.5"
      }
    }

    Before you start Claude Code, check the endpoint and key in one request:

    curl -sS -X POST "https://getmegabrain.com/api/anthropic/v1/messages" \
      -H "Authorization: Bearer $MEGABRAIN_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "content-type: application/json" \
      -d '{"model": "anthropic/claude-haiku-5.5", "max_tokens": 20, "messages": [{"role": "user", "content": "Say OK"}]}'

    ANTHROPIC_AUTH_TOKEN sends the key as Authorization: Bearer and takes over from a saved claude.ai login at once. Never put the key in a project’s .claude/settings.json: that file is committed. The ANTHROPIC_DEFAULT_*_MODEL variables decide what the opus, sonnet and haiku choices in /model call; Haiku also runs background tasks such as session titles.

  4. Step 4. Start Claude Code and check

    Run claude, then /status: the Anthropic base URL line must show https://getmegabrain.com/api/anthropic and the credential line must name ANTHROPIC_AUTH_TOKEN. Send a prompt; a normal answer means it works. If you see a warning about two credential sources, run /logout to drop the old claude.ai login.

  5. Step 5. VS Code, CI and scripts

    In the VS Code extension, put the same variables in claudeCode.environmentVariables in your user settings (JSON). In GitHub Actions, set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in the workflow env and pass the same secret as the action’s anthropic_api_key input. In scripts, claude -p "task" runs one task headless with the same settings.

Which model to choose

For Claude Code use Claude models: anthropic/claude-sonnet-5.5 for everyday coding, anthropic/claude-opus-5.5 (or anthropic/claude-fable-5.2) for hard refactors and architecture, anthropic/claude-haiku-5.5 for background tasks. Anthropic does not support Claude Code on other labs’ models; for GPT, Gemini, Kimi or GLM use Codex, OpenCode or Kilo Code with the same key.

Model IDWhen to use it
anthropic/claude-sonnet-5.5The default for an agent: reliable tool calls, holds long tasks, 1M-token context.
anthropic/claude-opus-5.5Hard multi-step work, code review and analysis where a mistake costs more than tokens.
openai/gpt-6.1-solOpenAI's flagship: the alternative to Claude for code and reasoning.
google/gemini-3.8-flashFast and cheap: long documents, spreadsheets, inbox triage.
moonshotai/kimi-k3.1-proA strong open-weight model for agents and code.
z-ai/glm-5.3Open-weight model for code and tools with good price-to-quality.
deepseek/deepseek-v4.1-flashHigh-volume routine work: classification, drafts, summaries.
mb-auto/freeFree, for checking your setup. 128K context, weaker at tool calls; do not send confidential data.

Every model with its price and context window is in the catalog. They all work with the same key.

What you can do

Subagents, hooks and skills

Split work across subagents, run your own scripts on every tool call with hooks, and package team procedures as skills — all on the same MegaBrain key.

MCP servers

Connect databases, issue trackers and internal docs over MCP so Claude works with real project context.

Headless mode

claude -p runs one task from a script, a cron job or CI and prints the result — the base for automated reviews and nightly jobs.

Usage per developer

Every developer gets their own MegaBrain key; usage per key shows in the dashboard, and revoking one key offboards one person.

Business use cases

Claude Code for a team without per-seat plans

A team where some people code daily and others once a week pays per token instead of a seat each. Light users cost little, heavy users get Opus when they need it, and the balance is one line in the budget.

Automatic pull request review in CI

GitHub Actions runs Claude Code on every pull request through MegaBrain: a summary, risky changes and missing tests as a comment, before a human reviews.

Task for the agent:

Review the diff of this pull request. List: 1) bugs or regressions with file and line, 2) changes without tests, 3) anything touching auth, payments or data deletion. Be specific and short; skip style nits.

Nightly maintenance jobs

A cron job runs claude -p against the repo every night: update dependencies, run the tests, open a branch with the fixes and a short report. Haiku keeps routine runs cheap.

AI media features in your product

When the product needs images or video, Claude Code builds the feature against MegaBrain’s image and video API — the same key it already runs on.

Troubleshooting

404 “Unknown API path”

The base URL ends in /v1. Set ANTHROPIC_BASE_URL to exactly https://getmegabrain.com/api/anthropic.

401, or Claude Code asks you to log in

The key is not reaching Claude Code. Put ANTHROPIC_AUTH_TOKEN in ~/.claude/settings.json (not only in a shell export or a project file) and check /status.

400 naming context_management or “Extra inputs are not permitted”

Set CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 in the same env block: it stops Claude Code from sending pre-release fields.

402 Payment Required

The balance is empty. Add funds on the Balance page.

Remote Control or voice dictation is unavailable

Expected: both need a claude.ai login and are off while a gateway credential is active.

FAQ

Can I use Claude Code without a Claude Pro or Max subscription?

Yes. Point ANTHROPIC_BASE_URL at https://getmegabrain.com/api/anthropic and set ANTHROPIC_AUTH_TOKEN to a MegaBrain key: Claude Code then runs on Claude models paid per token from your balance, with no subscription and no weekly limit.

Can Claude Code use GPT, Gemini or Kimi?

Anthropic supports Claude Code only on Claude models. For other labs’ models use a harness built for them — Codex, OpenCode or Kilo Code — with the same MegaBrain key.

How much does it cost?

You pay per token at each model’s price, from a prepaid balance in US dollars topped up by card. There is no subscription and no seat fee, so an agent that sits idle costs nothing. Prices per million tokens are in the model catalog; mb-auto/free costs nothing at all.

Does it work in VS Code and JetBrains?

Yes. The VS Code extension reads the variables from claudeCode.environmentVariables; the JetBrains plugin runs the CLI, which reads ~/.claude/settings.json.

Is there a free model for testing?

mb-auto/free is not served over the Anthropic Messages API, so it does not work in Claude Code. Test with anthropic/claude-haiku-5.5, the cheapest Claude model, or try the free model in OpenCode.

MegaBrain · one API for every model

Claude, GPT, Gemini, Kimi, GLM and DeepSeek in any agent — one key, one balance.

Sign up, add funds by card and pay only for what your agents use. No subscription. Free models work without a key.

Sources

More MegaBrain University guides