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

View original on GitHub ↗

24 Comments

sid374 contributor · 1 year ago

You can use /compact to 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 · 1 year ago

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.

IAMtheIAM · 1 year ago

I can't run /compact

 /compact 
  ⎿  Error: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input 
     length and `max_tokens` exceed context limit: 197170 + 20000 > 204648, decrease input length or 
     `max_tokens` and try again"}}
stanleyluong · 1 year ago

same problem. need to ctrl+C out of claude to start over whch is annoying.

Tom910 · 1 year ago

yeah, the same I can't run /compat because the command exceed limits

greendesertsnow · 1 year ago

I can't /compact in Cline or Roo...

LARAMediaGroup · 1 year ago

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_tokens exceed context limit: 194713 + 20000 > 204648, decrease input length or
max_tokens and try again"}}

modman87 · 1 year ago

Same problem here: API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens
exceed context limit: 185863 + 20000 > 204648, decrease input length or max_tokens and try again"}}

visu · 1 year ago

same problem here > ./compact
  ⎿  API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit: 185330 +
20000 > 204648, decrease input length or max_tokens and try again"}}

DeadToothTimmy · 1 year ago

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"

jaredscheib · 1 year ago

Another dupe at #476. Just ran into this myself. Claude isn't protecting my ability to compact in consideration of max token limit.

robtaylor · 1 year ago

Does the dev team have any proposed solutions to this?

baocin · 1 year ago

This is still an issue. Even with /compact on the latest version.

Nearcyan · 1 year ago

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 collaborator · 1 year ago

This has now landed! Restart to update and let us know if it's feeling better.

robtaylor · 1 year ago

Seems to be working well!

wickdninja · 1 year ago

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?

!Image

tim-arnold · 1 year ago
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"}}

kengonakajima · 1 year ago

I can't /compact, it keeps giving this error.

msitarzewski · 1 year ago

Ran into this today. First time. It usually auto-compacts.

⎿  Context low · Run /compact to compact & continue

autosolutionsai-didac · 1 year ago
I click in conect avatar but nothing happens

⎿ Context low · Run /compact to compact & continue

/compact

⎿ Error: Error during compaction: Error: API Error: 400
{"type":"error","error":{"type":"invalid_request_error","message":"input length and max_tokens exceed context limit:
198897 + 20000 > 200000, decrease input length or max_tokens and try again"}}

edgariscoding · 1 year ago

I'm having this issue as well

at062084 · 11 months ago

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[bot] · 11 months ago

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.