Submit a toolSubmit

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.

The problem: 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.

How do I shrink the input before retrying when the Claude API says the prompt is too long?

  1. 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

  2. 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

  3. 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

  4. 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.

  5. 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.

All tools for “Fix Claude API “prompt is too long” (400)” →

Related fixes

See also AI tools launched today on the daily board.