Basic checks
What's broken?
When a conversation exceeds Anthropic's context window (200K tokens), the API returns:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "prompt is too long: 200534 tokens > 200000 maximum"
}
}
This should raise RubyLLM::ContextLengthExceededError, but it raises RubyLLM::BadRequestError instead. The CONTEXT_LENGTH_PATTERNS in ErrorMiddleware don't include a pattern for Anthropic's "prompt is too long" phrasing.
How to reproduce
require "ruby_llm"
RubyLLM.configure { |c| c.anthropic_api_key = ENV["ANTHROPIC_API_KEY"] }
chat = RubyLLM.chat(model: "claude-haiku-4-5")
# Send a message that exceeds 200K tokens
big_text = "This is filler text to exceed the context window. " * 50_000
chat.ask(big_text)
# => raises RubyLLM::BadRequestError: prompt is too long: 209025 tokens > 200000 maximum
# => expected: RubyLLM::ContextLengthExceededError
Expected behavior
RubyLLM::ContextLengthExceededError is raised, matching the behavior for other providers' context length errors.
What actually happened
RubyLLM::BadRequestError is raised. The context_length_exceeded? check in ErrorMiddleware returns false because none of the existing CONTEXT_LENGTH_PATTERNS match "prompt is too long":
CONTEXT_LENGTH_PATTERNS = [
/context length/i,
/context window/i,
/maximum context/i,
/request too large/i,
/too many tokens/i,
/token count exceeds/i,
/input[_\s-]?token/i,
/input or output tokens? must be reduced/i,
/reduce the length of messages/i
].freeze
Raw Anthropic API response confirming the error format:
HTTP Status: 400
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "prompt is too long: 209025 tokens > 200000 maximum"
},
"request_id": "req_011CaiQ47KhBBjL9i3P2f1R7"
}
Note: This error message is not documented in Anthropic's API error reference. Anthropic support confirmed it is their context window validation error and acknowledged it as a documentation gap on their end.
Environment
- Ruby version: 3.4
- RubyLLM version: 1.14.1
- Provider: Anthropic
- OS: macOS / Linux
Basic checks
What's broken?
When a conversation exceeds Anthropic's context window (200K tokens), the API returns:
{ "type": "error", "error": { "type": "invalid_request_error", "message": "prompt is too long: 200534 tokens > 200000 maximum" } }This should raise
RubyLLM::ContextLengthExceededError, but it raisesRubyLLM::BadRequestErrorinstead. TheCONTEXT_LENGTH_PATTERNSinErrorMiddlewaredon't include a pattern for Anthropic's"prompt is too long"phrasing.How to reproduce
Expected behavior
RubyLLM::ContextLengthExceededErroris raised, matching the behavior for other providers' context length errors.What actually happened
RubyLLM::BadRequestErroris raised. Thecontext_length_exceeded?check inErrorMiddlewarereturnsfalsebecause none of the existingCONTEXT_LENGTH_PATTERNSmatch"prompt is too long":Raw Anthropic API response confirming the error format:
Note: This error message is not documented in Anthropic's API error reference. Anthropic support confirmed it is their context window validation error and acknowledged it as a documentation gap on their end.
Environment