Auto-compact to avoid "input length and max_tokens exceed context limit" errors
Resolved 💬 24 comments Opened Feb 24, 2025 by achieveramin Closed Mar 18, 2025
💡 Likely answer: A maintainer (sid374, contributor)
responded on this thread — see the highlighted reply below.
Bug Description
it raised: ⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit: 187254 + 20000 > 204798, decrease input length or max_tokens and try again"}} then it stopped continuing
Environment Info
- Platform: macos
- Terminal: iTerm.app
- Version: 0.2.9
- Feedback ID: a0322a15-98fc-4ebc-a3bf-64eb8d45da87
24 Comments
You can use
/compactto reduce the context size. Let us know if that doesn't work!https://docs.anthropic.com/en/docs/agents-and-tools/claude-code/overview#reduce-token-usage
I got into an annoying case today where /compact itself ran into the max_tokens error and I had to start again. It would be awesome to:
and/or
Another interesting approach for continuous-compaction would be to chunk up the whole transcript into well named/structured files as the session goes on and have claude search through them and read relevant ones as it does with code files. That way, if it knows it needs to refer back to something from an hour ago, it could look back through the log and find the right file to read to re-absorb the content.
I can't run
/compactsame problem. need to ctrl+C out of claude to start over whch is annoying.
yeah, the same I can't run /compat because the command exceed limits
I can't /compact in Cline or Roo...
Same here, can't run /compact as it run out of "image consultants for men" tokens. Error: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and
max_tokensexceed context limit: 194713 + 20000 > 204648, decrease input length ormax_tokensand try again"}}Same problem here: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and
max_tokensexceed context limit: 185863 + 20000 > 204648, decrease input length or
max_tokensand try again"}}same problem here > ./compact
⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and
max_tokensexceed context limit: 185330 +20000 > 204648, decrease input length or
max_tokensand try again"}}Is this error related to the amount of Tokens a user has left? Or related to how many prompts the user has left. For example I have used 94/150 prompts and I just got this error.
If it is Token based, how does a user know how many tokens they have used?
I noticed this in the documentation.
"Transition in pricing model: Tempo Labs is moving from a per-token system to a per-prompt system. This change is aimed at providing more transparency on usage and costs for users." so this hasn't happend yet? and if so when will this be implemented into Tempo Labs, because currently users have no idea its token based since there is no info on token usage just Prompt usage.
I hope a MOD or Dev can reply so I can understand what to do with my project, so close to the finish line and I get this
"AI Error: input length and max_tokens exceed context limit: 182659 + 24000 > 204648, decrease input length or max_tokens"
Another dupe at #476. Just ran into this myself. Claude isn't protecting my ability to compact in consideration of max token limit.
Does the dev team have any proposed solutions to this?
This is still an issue. Even with /compact on the latest version.
We've also encountered this issue. The state the user may be stuck in can be unpleasant as without the ability to use /compact or to easily revert state to a previous checkpoint the user may be forced to close their current session and lose their ~200K toks of context.
The suggestions from @djrodgerspryor are reasonable fixes - other fixes could include having the ability to go slightly beyond max_tokens for this use case or to make it easier for a user to revert state to before their most recent query so that they can then /compact pre-emptively.
This has now landed! Restart to update and let us know if it's feeling better.
Seems to be working well!
Still seems to be an issue. I have a repo that is "stuck" regardless of
/compact/clear/initI've killed the terminal and restarted it. Very much in a broken state.
Is there an internal context file I could clear out manually to workaround?
!Image
Same here. I've got the latest update and am stuck unable to even /compact.
API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and
max_tokensexceedcontext limit: 199323 + 20000 > 200000, decrease input length or
max_tokensand try again"}}I can't /compact, it keeps giving this error.
Ran into this today. First time. It usually auto-compacts.
⎿ Context low · Run /compact to compact & continue
⎿ Context low · Run /compact to compact & continue
⎿ Error: Error during compaction: Error: API Error: 400
{"type":"error","error":{"type":"invalid_request_error","message":"input length and
max_tokensexceed context limit:198897 + 20000 > 200000, decrease input length or
max_tokensand try again"}}I'm having this issue as well
First time use of claude-3-5-haiku-20241022. Same issue. Stuck at critical point after Claude running in circles to fix its own scripts to update my code
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.