Claude API says “prompt is too long”. Shrink input before you retry
A 400 invalid_request_error means the input alone exceeds the context window. Count tokens, compact or clear old tool results, and never retry the same payload. Short answer: 5 steps. 1) Read the error: input overflow vs generation stop; 2) Count tokens before you resend; 3) Compact or clear history instead of blind retries; 4) Fit max_tokens and model choice to the remaining window; 5) Watch usage and treat overflow as non-retryable.
Last updated: · This page is the decision checklist; the tools list for “Fix Claude API “prompt is too long” (400)” is a separate URL.
How do I shrink the input before retrying when the Claude API says the prompt is too long?
Read the error: input overflow vs generation stop
If the input alone exceeds the context window, the Messages API returns HTTP 400 `invalid_request_error` with “prompt is too long” on every model. That is different from Claude 4.5+ stopping mid-generation with `stop_reason: "model_context_window_exceeded"` when input plus `max_tokens` fits at accept time. Docs: https://platform.claude.com/docs/en/build-with-claude/context-windows
Count tokens before you resend
Call the Token Counting API with the same `model`, `system`, `messages`, tools, and media you would send. Everything in the request counts: system prompt, full history (including tool results), tool definitions, images/PDFs, and cached prompt blocks (caching changes price, not whether tokens occupy the window). Token counting: https://platform.claude.com/docs/en/build-with-claude/token-counting
Compact or clear history instead of blind retries
Do not retry the unchanged request—it fails the same way every time. Use server-side compaction (beta) to summarize older turns, or context editing to clear old tool results / thinking blocks. Cap tool output at the source (line ranges, head of command output) so agent loops stop accumulating giant results. Compaction: https://platform.claude.com/docs/en/build-with-claude/compaction
Fit max_tokens and model choice to the remaining window
On older models, input plus `max_tokens` can fail validation; on Claude 4.5+ the request may accept and then stop at the window. Lower `max_tokens` to what you need, or move long-context steps to a model whose window fits the task (many current Claude models default to 1M; some older ones are 200k). See the model comparison table in the context-windows docs.
Watch usage and treat overflow as non-retryable
Log `usage` from each response (`input_tokens` plus any cache_* input fields) and compact before you hit the ceiling. In client retry logic, treat 400 “prompt is too long” like other `invalid_request_error` cases: fix the payload, don’t backoff-spin. Error reference: https://platform.claude.com/docs/en/api/errors
Which tools are best to fix Claude API “prompt is too long” (400)?
Context windows
What counts toward the window and when 400 “prompt is too long” is returned.
Token counting API
Estimate input tokens for the exact Messages payload before sending.
Compaction
Server-side summaries so long agent chats fit the window again.
Claude API errors
400 invalid_request_error handling vs retryable 429/5xx/529 cases.
Open: 1 spot per week
One labeled sponsor for this topic each week; this week's spot is open. See open weeks →
All tools for “Fix Claude API “prompt is too long” (400)” →
Related fixes
- Stop a Cursor agent loopHow to fix a Cursor agent loop (same edit, or work after Stop)Fix →
- Claude Code vs Codex vs Cursor ~$20 plansClaude Code vs Codex vs Cursor $20 plan limits: how to pick without burning the weekFix →
- Scan a vibe-coded app for leaked API keysCheck a vibe-coded app for leaked API keys before you share itFix →
- Add an MCP server to Claude Code, Cursor, or CodexHow to add an MCP server to Claude Code, Cursor, or CodexFix →
See also AI tools launched today on the daily board.
NewAIToolsToday Daily
Top 3 AI launches, one model or news item that matters, and one simple fix. A 5-minute read, every weekday.