[BUG} API Error: 400 'input length and `max_tokens` exceed context limit' occurs when close to compaction

Resolved 💬 1 comment Opened Mar 14, 2025 by robtaylor Closed Mar 14, 2025

Environment

  • Platform (select one):
  • [ ] Anthropic API
  • [ ] AWS Bedrock
  • [ ] Google Vertex AI
  • [x ] Other: local
  • Claude CLI version: 0.2.40 (Claude Code)
  • Operating System: MacOS 15.3.1 arm64
  • Terminal: iTerm2

Bug Description

When i'm within about 5% of needing to /compact , I consistently get:

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit: 186433 + 20000 > 200000, decrease input

Steps to Reproduce

  1. do a long running activity
  2. continue until context available <5%
  3. ask something quite hard
  4. error

Expected Behavior

Error not thrown and handled gracefully, either with meaningful error message with user next steps, or transparently.
I think i would also expect that claude code also prompts at suitable times to compact. Unfortunatly after a compact you then need to 'get it back up to speed' with what it was doing when the error occured

Actual Behavior

error thrown

View original on GitHub ↗

This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗