Skip to main content

Claude Code

Claude Code can connect to MAG through its Anthropic-compatible API.

warning

Claude Code usage can easily exceed $100 per day. Usage counts against the allocation of your project. Read this section before you start.

Anthropic reports an average cost of $6 per day for Claude Code. Heavy use, long contexts, and large models cost much more. These patterns increase token use:

  • Having too many MCP servers
  • Over-use of CLAUDE.md instead of skills
  • Not using language-aware code intelligence plugins
  • Not clearing context between tasks
  • Using an overpowered model for the task
  • Letting the context grow past the compaction points in Set the compaction point

To check your spend, see Using the MAG Dashboard. For more ways to reduce cost, see the Claude Code cost guide.

1. Set Environment Variables​

Choose one of these methods. Both set the same variables.

  • Option A: Export the variables. The MAG settings apply to every claude session in your shell.
  • Option B: Use a shell function. The MAG settings apply only when you start Claude Code with claude-amsc. Your normal shell and claude command are not changed.

Option A: Export the variables​

Add the following to ~/.zshrc or ~/.bashrc:

# Set authorization and base URL
export ANTHROPIC_AUTH_TOKEN=$AMSC_TOKEN
export ANTHROPIC_BASE_URL=https://mag.genesis.americansciencecloud.org

# Optional: select the provider that serves the Claude models (amazon or google)
export ANTHROPIC_CUSTOM_HEADERS="X-AmSC-MAG-Provider: amazon"
# export ANTHROPIC_CUSTOM_HEADERS="X-AmSC-MAG-Provider: google"

# Recommended setting
export DISABLE_NON_ESSENTIAL_MODEL_CALLS=1

# Recommended setting
export DISABLE_TELEMETRY=1

# Recommended setting for compatibility
export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1

# Recommended setting to reduce model throttling
# Higher max output tokens can be used but may cause prompts to be rejected with "too many tokens" error
export CLAUDE_CODE_MAX_OUTPUT_TOKENS=16384

# Recommended setting to suppress terminal flicker
export CLAUDE_CODE_NO_FLICKER=1

# Set the point at which Claude Code compacts the conversation (see "Set the compaction point")
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000

# Limit parallel subagents to the MAG limit of 20 simultaneous requests (see "Rate limits")
export CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS=20

Option B: Use a shell function​

Add the following to ~/.zshrc or ~/.bashrc. The function sets the same variables as Option A, but only for the claude process that it starts:

claude-amsc() {
ANTHROPIC_AUTH_TOKEN=$AMSC_TOKEN \
ANTHROPIC_BASE_URL=https://mag.genesis.americansciencecloud.org \
ANTHROPIC_CUSTOM_HEADERS="X-AmSC-MAG-Provider: amazon" \
DISABLE_NON_ESSENTIAL_MODEL_CALLS=1 \
DISABLE_TELEMETRY=1 \
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 \
CLAUDE_CODE_MAX_OUTPUT_TOKENS=16384 \
CLAUDE_CODE_NO_FLICKER=1 \
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000 \
CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS=20 \
claude "$@"
}

ANTHROPIC_CUSTOM_HEADERS is optional, as in Option A. To remove it, delete its line. To use Google, change the value to X-AmSC-MAG-Provider: google.

Choose a provider​

Claude models are available on the amazon and google providers. Set X-AmSC-MAG-Provider to one of them.

ProviderWeb search tool
amazonNot supported
googleSupported

If you need the Claude web search tool, use google. On amazon, a request that uses the web search tool does not work.

Model variables​

Do not set the model variables (ANTHROPIC_MODEL, ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, ANTHROPIC_DEFAULT_HAIKU_MODEL, CLAUDE_CODE_SUBAGENT_MODEL). Claude Code selects the current Claude models. To change the model in a session, use the /model command.

Set the compaction point​

Claude Code compacts the conversation when it reaches the compaction point. Prices are higher for long contexts, so a lower compaction point reduces cost.

  • Claude Haiku: Set the value to 100000. Prices for Haiku are 5 times higher above 100,000 tokens.
  • Claude Sonnet and Claude Opus: Set the value to 200000. This is optional. Prices are higher for long contexts.

CLAUDE_CODE_AUTO_COMPACT_WINDOW is a number from 100000 to 1000000. It overrides other compaction settings. To change the value in a session, use the command /autocompact <tokens>.

2. Verify Environment Variables​

Restart your shell, then verify the settings.

Option A: Make sure that the variables are present:

env | grep -E '^(ANTHROPIC_|CLAUDE_CODE_|DISABLE_)'

You should see each variable from Option A.

Option B: The variables are set only inside claude-amsc, so env does not show them. Make sure that the function is defined:

type claude-amsc

The output should show the function from Option B.

3. Start Claude Code​

Start Claude Code in your project directory. Use claude for Option A or claude-amsc for Option B:

cd my-project
claude # Option A
claude-amsc # Option B

Add options as usual, for example claude-amsc --model claude-haiku. If you use Haiku, set the compaction point to 100000 (see Set the compaction point).

4. Optional: Install VS Code Extension​

If you are a VS Code user, install the Claude Code for VSCode extension. When claude is run through the VS Code terminal, it will use VS Code to display highlighted diffs for approval.

Run Claude Code on a lower-cost or free model​

Claude Code does not need a Claude model. MAG translates the Anthropic API to other models, so you can pin every Claude Code model slot to one cheaper model. Two options:

ModelCostProvider headerUse for
gpt-6-lunaLow. Counts against your project allocation.openaiRoutine coding, small edits, questions about a codebase
inkling-smallFree. Hosted by Berkeley Lab (CBorg). Does not count against your allocation.cborgExploration, learning, and tasks where cost matters more than speed

Pin all model slots, including subagents, so Claude Code never calls a paid Claude model. Add these functions to ~/.zshrc or ~/.bashrc:

# Claude Code on a single MAG model; every model slot is pinned to it.
# $1 is the model, $2 the provider that serves it. Remaining arguments go to claude.
# Auto-compact is capped at 200K input tokens to bound per-request cost.
# The built-in WebSearch runs on Anthropic's servers and fails through MAG, so it is blocked.
claude-amsc-model() {
local model=$1 provider=$2
shift 2

ANTHROPIC_AUTH_TOKEN=$AMSC_TOKEN \
ANTHROPIC_BASE_URL=https://mag.genesis.americansciencecloud.org \
ANTHROPIC_CUSTOM_HEADERS="X-AmSC-MAG-Provider: $provider" \
ANTHROPIC_MODEL=$model \
ANTHROPIC_DEFAULT_OPUS_MODEL=$model \
ANTHROPIC_DEFAULT_SONNET_MODEL=$model \
ANTHROPIC_DEFAULT_HAIKU_MODEL=$model \
CLAUDE_CODE_SUBAGENT_MODEL=$model \
CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000 \
CLAUDE_CODE_MAX_OUTPUT_TOKENS=16384 \
DISABLE_NON_ESSENTIAL_MODEL_CALLS=1 \
DISABLE_TELEMETRY=1 \
CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 \
CLAUDE_CODE_NO_FLICKER=1 \
claude --disallowedTools WebSearch "$@"
}

claude-amsc-luna() { claude-amsc-model gpt-6-luna openai "$@"; }
claude-amsc-inkling() { claude-amsc-model inkling-small cborg "$@"; }

Start a session with claude-amsc-luna or claude-amsc-inkling. Arguments are passed through, so claude-amsc-luna --continue resumes your most recent conversation.

Choose a reasoning effort​

Both models accept an effort modifier, which trades speed and cost for quality. Add it to the model name, for example claude-amsc-model gpt-6-luna:high openai. For inkling-small, use inkling-small:high. See Model Names for the modifiers.

Move up when the model falls short​

If the model makes repeated failed edits or misses steps in a multi-file change, raise the effort first. If that is not enough, switch the session to a larger model with /model <name> or start a session with claude-amsc.

Troubleshooting​

The most common issue is that the environment variables are not configured correctly. You may need to log out / log-in again to ensure the variables are picked up and propagated correctly into all sub-processes.