You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto-compact to avoid "input length and max_tokens exceed context limit" errors #42
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
Reacted by Andrey Marchenko, Kirill Korikov, Oleksandr Sidko, Kanishka and near
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:
Keep enough reserved tokens that this can't happen (ie. that compact can always succeed)
and/or
Auto-compact when crossing a certain threshold (eg. 70% usage). Maybe this needs some more care to preserve the last few commands so that it's really seamless. Ideally users wouldn't have to think about the context limit except for knowing vaguely that claude has probably forgotten about older context.
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.
Reacted by Andrew Tokmakoff, 3zzy, Linden, IAMtheIAM, Kirill Korikov, tennyson-mccalla, Gabriel Garrett, Tânia Lopes, jumpeiMano, Oleksandr Sidko and 8 more
changed the title [-]Context Limit Exceeded: Reduce Input Length or Max Tokens[/-][+]Auto-compact to avoid "input length and max_tokens exceed context limit" errors[/+]on Feb 28, 2025
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.
Still seems to be an issue. I have a repo that is "stuck" regardless of /compact/clear/init
I'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?
Still seems to be an issue. I have a repo that is "stuck" regardless of /compact/clear/init I'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?
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_tokens exceed
context limit: 199323 + 20000 > 200000, decrease input length or max_tokens and try again"}}
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.
Activity
sid374 commentedon Feb 24, 2025
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
djrodgerspryor commentedon Feb 25, 2025
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.
[-]Context Limit Exceeded: Reduce Input Length or Max Tokens[/-][+]Auto-compact to avoid "input length and max_tokens exceed context limit" errors[/+]IAMtheIAM commentedon Mar 1, 2025
I can't run
/compactstanleyluong commentedon Mar 1, 2025
same problem. need to ctrl+C out of claude to start over whch is annoying.
Tom910 commentedon Mar 3, 2025
yeah, the same I can't run /compat because the command exceed limits
greendesertsnow commentedon Mar 3, 2025
I can't /compact in Cline or Roo...
10 remaining items
robtaylor commentedon Mar 14, 2025
Does the dev team have any proposed solutions to this?
baocin commentedon Mar 16, 2025
This is still an issue. Even with /compact on the latest version.
Nearcyan commentedon Mar 17, 2025
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.
bcherny commentedon Mar 18, 2025
This has now landed! Restart to update and let us know if it's feeling better.
robtaylor commentedon Mar 19, 2025
Seems to be working well!
wickdninja commentedon Mar 25, 2025
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?
tim-arnold commentedon Jun 7, 2025
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"}}kengonakajima commentedon Jun 12, 2025
I can't /compact, it keeps giving this error.
msitarzewski commentedon Jun 24, 2025
Ran into this today. First time. It usually auto-compacts.
⎿ Context low · Run /compact to compact & continue
autosolutionsai-didac commentedon Jun 24, 2025
edgariscoding commentedon Jul 15, 2025
I'm having this issue as well
at062084 commentedon Jul 23, 2025
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
github-actions commentedon Jul 30, 2025
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.