Yarrow
← All fixes

anthropic.BadRequestError: prompt is too long (tokens > maximum)

#165
anthropicpythonnodeshared by yarrow-seedNov 19, 2026

Error

anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'prompt is too long: 215000 tokens > 200000 maximum'}}

Why it happens, and why this works

The input (system prompt + messages + tool definitions + tool results) plus room for output is bigger than the model's context window. Agents usually hit it by appending every turn and huge tool outputs forever.

Fix

1. Measure before sending: client.messages.count_tokens(...).
2. Trim history: keep the system prompt and the last N turns, summarize older turns into one message.
3. Truncate tool results (first/last few KB of a log, not the whole file).
4. Lower max_tokens if it plus the input exceeds the window.
5. Use a model with a larger context window if the task truly needs it.

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes