Fix Claude Code API Error 400 Bad Request
A 400 error from the Claude Code API means your request is malformed — usually from context window overflow, invalid model names, or corrupted message history. This guide covers every common cause and how to fix each one.
The Problem
Claude Code returns API error 400 during a conversation or when starting a new task. Unlike auth errors, a 400 means the API received your request but could not process it. This typically happens mid-session when the conversation grows too large or when configuration values are invalid.
Quick Solution
Step 1: Check if the error mentions “context length” or “max tokens.” If so, your conversation exceeded the model’s context window. Start a fresh conversation:
claude
Step 2: If you are using a custom model string, verify it is valid. Check your configuration:
claude config list
Look for the model setting. Valid model names include claude-sonnet-4-20250514, claude-opus-4-20250514, and claude-haiku-35-20241022. A typo here causes a 400.
Step 3: If the error happens on every request, even fresh ones, check if a hook or CLAUDE.md is injecting invalid content. Temporarily rename your CLAUDE.md:
mv CLAUDE.md CLAUDE.md.bak
claude
If it works without CLAUDE.md, the file contains something the API rejects (often extremely long content or invalid characters).
Step 4: Clear any cached conversation state:
claude --no-resume
How It Works
The Anthropic API validates every incoming request against a strict schema. A 400 response means one or more fields failed validation. The most common trigger in Claude Code is context window overflow: as you work, Claude Code accumulates tool results, file contents, and conversation history. When this payload exceeds the model’s context limit (200K tokens for most Claude models), the API rejects it. Claude Code manages context automatically, but rapid file reads or very large codebases can push past the limit. The API also validates model names, message structure, and parameter ranges — any mismatch triggers a 400.
Common Issues
Massive file reads filling context. If you asked Claude Code to read a very large file (e.g., a minified bundle or a database dump), the context may overflow. Avoid reading files over 500KB. Instead, ask Claude Code to read specific line ranges or search for patterns.
Invalid system prompt from CLAUDE.md. If your CLAUDE.md file is extremely long (over 10,000 words) or contains binary characters, it can push the system prompt past API limits. Keep CLAUDE.md under 2,000 words and use only UTF-8 text.
Stale conversation with incompatible message format. If you updated Claude Code but resumed an old conversation, the message format may be incompatible. Start a fresh session with claude --no-resume.
Example CLAUDE.md Section
# Context Management
## Rules for Claude Code
- Never read files larger than 500KB in a single operation
- Use grep/search before reading entire files
- When context gets large, summarize findings before continuing
- Keep tool results focused — request specific line ranges, not whole files
## Project Size
- This is a medium codebase (~50K lines)
- Key entry points: src/index.ts, src/server.ts
- Ignore: node_modules/, dist/, .git/, *.min.js
## If You Hit a 400 Error
- Start a new conversation
- Re-read only the files needed for the current task
- Summarize previous context instead of replaying it
Best Practices
-
Keep conversations focused. One task per session. If you need to pivot to a different part of the codebase, start a fresh conversation rather than carrying forward context from an unrelated task.
-
Use .claudeignore for large directories. Create a
.claudeignorefile to exclude build artifacts, vendored dependencies, and generated files from Claude Code’s file operations. -
Monitor conversation length. If you notice Claude Code’s responses getting slower or less coherent, context is filling up. Wrap up the current task and start fresh.
-
Validate CLAUDE.md after edits. After changing your CLAUDE.md, run a simple test prompt to ensure it parses correctly before starting real work.
Related Reading
- Anthropic API Error 429 Rate Limit
- Claude Code Failed to Authenticate API Error 401
- Best Way to Use Claude Code for Debugging Sessions
Built by theluckystrike. More at zovo.one