Skip to content

[BUG] Anthropic "prompt is too long" error not classified as ContextLengthExceededError #755

Description

@skovy

Basic checks

  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions