[BUG][Bedrock] countTokens code path bypasses Bedrock adapter when env vars are only in settings.json, causing 400 on fresh session startup
Description
When CLAUDE_CODE_USE_BEDROCK=1 and model env vars (ANTHROPIC_DEFAULT_SONNET_MODEL, etc.) are configured only in settings.json's env section (not exported in the shell environment), every new Claude Code session fails on the first message with:
API Error (...inference-profile/global.anthropic.claude-sonnet-4-6): 400 The provided model identifier is invalid.
The issue disappears mid-session after using /model to switch models, and does not affect sessions that already have accumulated token counts from prior API responses.
Steps to Reproduce
- Configure
~/.claude/settings.jsonwith Bedrock settings only in theenvsection (no shell exports):
{
"model": "sonnet",
"smallFastModel": "haiku",
"awsRegion": "eu-west-1",
"env": {
"CLAUDE_CODE_USE_BEDROCK": "1",
"AWS_REGION": "eu-west-1",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-opus-4-6-v1",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-sonnet-4-6",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-haiku-4-5-20251001-v1:0"
}
}
- Ensure none of these vars are exported in
~/.zshrc/~/.bashrc - Open a new terminal and start Claude Code
- Submit any message
Expected Behavior
Claude Code uses Bedrock with the configured model ARNs.
Actual Behavior
Both countTokensWithFallback and the main messages call fail with 400 The provided model identifier is invalid.
Debug log shows the failure sequence:
[ERROR] API error (attempt 1/11): 400 400 The provided model identifier is invalid. ← countTokens with main model
[DEBUG] countTokensWithFallback: API returned null, trying haiku fallback (13 tools)
[DEBUG] countTokensWithFallback: haiku fallback failed: 400 The provided model identifier is invalid.
[DEBUG] Auto tool search disabled: 27592 chars (threshold: 50000) (char fallback) ← fell back to char count
[ERROR] API error (attempt 1/11): 400 400 The provided model identifier is invalid. ← main messages call
Root Cause Analysis
The countTokensWithFallback call (attribution code cc_version=2.1.56.160) is triggered on fresh sessions when no accumulated token counts exist from prior API responses. This code path appears to not apply the Bedrock adapter, hitting the Anthropic API directly with a Bedrock ARN as the model identifier — which the Anthropic API correctly rejects.
Sessions that already have accumulated token counts (from prior API responses) skip countTokensWithFallback entirely, which is why the error disappears after a /model switch mid-session.
The settings.json env section is also applied to Claude Code's own process, but it appears the Bedrock SDK client initialisation (or the countTokens code path specifically) does not pick up these values correctly when they are not already present in the OS-level environment at process start.
Workaround
Export the relevant env vars in the shell config so they are set before Claude Code initialises:
# ~/.zshrc
export CLAUDE_CODE_USE_BEDROCK=1
export AWS_REGION=eu-west-1
export ANTHROPIC_DEFAULT_OPUS_MODEL="arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-opus-4-6-v1"
export ANTHROPIC_DEFAULT_SONNET_MODEL="arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-sonnet-4-6"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="arn:aws:bedrock:eu-west-1:ACCOUNT_ID:inference-profile/global.anthropic.claude-haiku-4-5-20251001-v1:0"
Environment
- Claude Code version: 2.1.56
- OS: macOS Darwin 24.6.0
- API Provider: AWS Bedrock
- Region: eu-west-1
- Models: Cross-region inference profiles (
global.*)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗