BLACKFUEL
Cookbooks

Claude Code

Point the Claude Code CLI at Blackfuel's Anthropic-compatible endpoint and run it on an open-source model

Claude Code is Anthropic's CLI agent. It speaks the Anthropic Messages API, so it runs against any Anthropic-compatible gateway. Blackfuel exposes one at https://api.blackfuel.ai, which lets you drive Claude Code with open-source models served by Blackfuel — with transparent per-token pricing and no provider lock-in.

Prerequisites

Configure

Claude Code is configured through environment variables. Set these before launching claude:

export BLACKFUEL_API_KEY=bf_mykey_xxxxxx

export ANTHROPIC_BASE_URL=https://api.blackfuel.ai
export ANTHROPIC_AUTH_TOKEN="$BLACKFUEL_API_KEY"

# Map each Claude Code model tier to a Blackfuel model ID
export ANTHROPIC_DEFAULT_FABLE_MODEL=moonshotai/Kimi-K3
export ANTHROPIC_DEFAULT_OPUS_MODEL=zai-org/GLM-5.2
export ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731
export ANTHROPIC_DEFAULT_HAIKU_MODEL=google/gemma-4-31B-it
export CLAUDE_CODE_SUBAGENT_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731

# Leaner prompts and higher prompt-cache hit rates
export ENABLE_TOOL_SEARCH=true
export CLAUDE_CODE_MAX_CONTEXT_TOKENS=600000
export CLAUDE_CODE_ATTRIBUTION_HEADER=0

What each variable does:

VariablePurpose
ANTHROPIC_BASE_URLPoints Claude Code at Blackfuel's Anthropic-compatible endpoint. No /v1 suffix — the CLI appends the path.
ANTHROPIC_AUTH_TOKENYour Blackfuel API key. Use this variable, not ANTHROPIC_API_KEY.
ANTHROPIC_DEFAULT_FABLE_MODELThe Blackfuel model ID the fable tier resolves to.
ANTHROPIC_DEFAULT_OPUS_MODELThe Blackfuel model ID the opus tier resolves to, and the one plan mode runs on.
ANTHROPIC_DEFAULT_SONNET_MODELThe Blackfuel model ID the sonnet tier resolves to — Claude Code's default for everyday work.
ANTHROPIC_DEFAULT_HAIKU_MODELThe Blackfuel model ID for the haiku tier, which Claude Code uses for background work such as conversation summarization.
CLAUDE_CODE_SUBAGENT_MODELThe Blackfuel model ID Claude Code runs subagents and agent teams on.
ENABLE_TOOL_SEARCHSet to true to keep MCP tool definitions deferred, which keeps the prompt lean on long agent sessions. Claude Code loads them upfront by default whenever ANTHROPIC_BASE_URL points somewhere other than Anthropic.
CLAUDE_CODE_MAX_CONTEXT_TOKENSSets the context window Claude Code assumes for the routed model, which drives its context gauge and auto-compaction. Claude Code applies this itself; it is not discovered from the API. See the callout below for the value to use.
CLAUDE_CODE_ATTRIBUTION_HEADERSet to 0 to omit the attribution block from the system prompt, which raises prompt-cache hit rates — visible as cached input tokens on your requests.

Set CLAUDE_CODE_MAX_CONTEXT_TOKENS to roughly 60% of the routed model's context window, never the full window — 600000 for a 1M-token model. Claude Code's client-side token estimate counts differently from the model's tokenizer, so a window matched 1:1 lets long sessions fail with 400 context-length errors before auto-compaction fires.

Claude Code routes different jobs to different model tiers, and this setup maps each tier to a Blackfuel model ID. Per-token pricing and each model's context window are in Available Models. Swap any tier for another model ID there; to run a single model everywhere instead, set ANTHROPIC_MODEL to one model ID and drop the per-tier variables.

Persist the configuration

The export approach applies only to the current shell. To make the configuration permanent, pick one of these three approaches.

Option 1: Shell profile

Add the export lines to your shell profile so they apply to every interactive session:

# ~/.bashrc or ~/.zshrc
export BLACKFUEL_API_KEY=bf_mykey_xxxxxx
export ANTHROPIC_BASE_URL=https://api.blackfuel.ai
export ANTHROPIC_AUTH_TOKEN="$BLACKFUEL_API_KEY"
export ANTHROPIC_DEFAULT_FABLE_MODEL=moonshotai/Kimi-K3
export ANTHROPIC_DEFAULT_OPUS_MODEL=zai-org/GLM-5.2
export ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731
export ANTHROPIC_DEFAULT_HAIKU_MODEL=google/gemma-4-31B-it
export CLAUDE_CODE_SUBAGENT_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731
export ENABLE_TOOL_SEARCH=true
export CLAUDE_CODE_MAX_CONTEXT_TOKENS=600000
export CLAUDE_CODE_ATTRIBUTION_HEADER=0

Reload it (source ~/.bashrc) or open a new terminal before launching claude.

Option 2: Claude Code settings file

Claude Code reads an env block from its settings file at startup, so you can persist the variables there instead of in your shell. Edit ~/.claude/settings.json, add the keys to the existing env object, and replace bf_mykey_xxxxxx with your own key — a JSON file cannot read BLACKFUEL_API_KEY from the shell:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.blackfuel.ai",
    "ANTHROPIC_AUTH_TOKEN": "bf_mykey_xxxxxx",
    "ANTHROPIC_DEFAULT_FABLE_MODEL": "moonshotai/Kimi-K3",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "zai-org/GLM-5.2",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-ai/DeepSeek-V4-Flash-0731",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "google/gemma-4-31B-it",
    "CLAUDE_CODE_SUBAGENT_MODEL": "deepseek-ai/DeepSeek-V4-Flash-0731",
    "ENABLE_TOOL_SEARCH": "true",
    "CLAUDE_CODE_MAX_CONTEXT_TOKENS": "600000",
    "CLAUDE_CODE_ATTRIBUTION_HEADER": "0"
  }
}

Every Claude Code session on the machine then routes to Blackfuel automatically — no shell exports needed. The changes take effect on the next session start. This is the option to choose if you want all sessions to use Blackfuel regardless of which shell launched them.

Option 3: A bfclaude launcher function

If you also run Claude Code against your Anthropic account, wrap the Blackfuel configuration in a shell function instead of overriding it globally. bfclaude launches Claude Code on Blackfuel, while plain claude stays on Anthropic:

# ~/.bashrc or ~/.zshrc
export BLACKFUEL_API_KEY=bf_mykey_xxxxxx

bfclaude() {
  ANTHROPIC_BASE_URL=https://api.blackfuel.ai \
  ANTHROPIC_AUTH_TOKEN="$BLACKFUEL_API_KEY" \
  ANTHROPIC_DEFAULT_FABLE_MODEL=moonshotai/Kimi-K3 \
  ANTHROPIC_DEFAULT_OPUS_MODEL=zai-org/GLM-5.2 \
  ANTHROPIC_DEFAULT_SONNET_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731 \
  ANTHROPIC_DEFAULT_HAIKU_MODEL=google/gemma-4-31B-it \
  CLAUDE_CODE_SUBAGENT_MODEL=deepseek-ai/DeepSeek-V4-Flash-0731 \
  ENABLE_TOOL_SEARCH=true \
  CLAUDE_CODE_MAX_CONTEXT_TOKENS=600000 \
  CLAUDE_CODE_ATTRIBUTION_HEADER=0 \
  claude "$@"
}

Run bfclaude to start a Blackfuel-backed session; the variables apply only to that process, so your default claude is untouched.

Your Blackfuel key is a secret. Avoid committing ~/.claude/settings.json or your shell profile to a shared dotfiles repo, and restrict the file to your own user; loading the key from a secrets manager keeps it off disk entirely.

Run

claude

You're now in an interactive Claude Code session backed by Blackfuel, with the tier mapping above deciding which Blackfuel model ID answers each request.

Tips

  • Switch models: change the ANTHROPIC_DEFAULT_*_MODEL values to any model ID from Available Models. For EU data residency, use the :eu model ID listed under EU-Hosted Models.
  • Verify a model is live: call GET /v1/models to confirm the model ID is served before pointing Claude Code at it.

Troubleshooting

SymptomFix
401 UnauthorizedANTHROPIC_AUTH_TOKEN is missing, wrong, or revoked. Re-check the key in the console.
A Blackfuel model is missing from Claude Code's model pickerClaude Code lists a non-Anthropic model only when its anthropic_family_tier is set. Point an ANTHROPIC_DEFAULT_*_MODEL variable at the model ID to use it anyway.
404 model not foundOne of the model variables points at a model ID that isn't served. Confirm each value via GET /v1/models.
404 on /v1/messages/count_tokensClaude Code handles it by falling back to a character-based estimate of context usage.
413 Payload Too LargeRun /compact; see Request Limits.
429 with rate_limit_exceededThe request exceeded your Serverless quota; Claude Code retries after the retry-after seconds, or shows the error when the wait is over a minute; see Rate Limits.
429 with overloadedClaude Code retries automatically with backoff; see Rate Limit Errors.
Requests still hitting AnthropicANTHROPIC_BASE_URL is unset or has a trailing /v1. Set it to exactly https://api.blackfuel.ai.

Next steps

On this page