[BUG] CLI crashes with "CLI output was not valid JSON" when Bedrock /count-tokens returns 403

Resolved 💬 3 comments Opened Feb 6, 2026 by karlpauls Closed Mar 29, 2026

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

When using Claude Code with CLAUDE_CODE_USE_BEDROCK=1 (both standalone CLI and via the Agent SDK @anthropic-ai/claude-agent-sdk@0.2.32), the CLI process crashes when processing large context (e.g., large HTML pages as tool results).

### Root cause

The CLI internally calls /model/{modelId}/count-tokens on Bedrock for context window management. This endpoint is not supported on Bedrock and returns a 403. The CLI does not handle this failure gracefully — it crashes mid-stdout-write, producing a truncated JSON line.

When used via the Agent SDK, the SDK parent process reads this truncated line and throws:

Error: CLI output was not valid JSON. This may indicate an error during startup.
Output: {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_bdrk_01ANFzhQWMo5XE2V5QmKCaqH","type":"tool_result","content":" 200→ \n...

When used as a standalone CLI, the session crashes with no recovery.

### Two sub-issues

  1. Wrong model ID format for Bedrock: The CLI calls /model/anthropic.claude-opus-4-6-v1/count-tokens — using the Anthropic-native model ID (no region prefix, no version suffix) instead of the Bedrock format (e.g., us.anthropic.claude-opus-4-6-v1:0). This is inconsistent with how

/invoke and /invoke-with-response-stream paths are constructed.

  1. Unhandled 403 crashes the process: When count-tokens returns 403, the CLI process dies mid-write to stdout. There is no fallback or graceful degradation.

What Should Happen?

The CLI should either:

  • Skip count-tokens when running on Bedrock (if the endpoint isn't supported)
  • Fall back to a local token estimation when the API call fails
  • At minimum, handle the 403 without crashing the process

Error Messages/Logs

"http_response", "host": "bedrock-runtime.us-east-1.amazonaws.com", "path": "/model/anthropic.claude-opus-4-6-v1/count-tokens", "method": "POST", "status_code": "403", "duration_seconds": 0.66, "response_size_bytes": 274

Claude Code agent stopped error=Error: CLI output was not valid JSON. This may indicate an error during startup.

Steps to Reproduce

  • Claude Code CLI (tested via @anthropic-ai/claude-agent-sdk@0.2.32)
  • CLAUDE_CODE_USE_BEDROCK=1 with bearer token auth
  • Send a message that causes the agent to read a large HTML file (100KB+) as a tool result
  • The CLI calls count-tokens, gets 403, and crashes

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.32 (Claude Code)

Platform

AWS Bedrock

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

### Workaround

Intercept /model/{modelId}/count-tokens requests at the proxy layer and return a synthetic {"input_tokens": <estimate>} response before the request reaches Bedrock.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗