Skip to content

Auto-compact to avoid "input length and max_tokens exceed context limit" errors #42

Closed

Description

@achieveramin

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

Activity

  1. sid374 commented on Feb 24, 2025

    @sid374
    Contributor

    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

  2. djrodgerspryor commented on Feb 25, 2025

    @djrodgerspryor

    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.

  3. 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
  4. self-assigned this
    on Feb 28, 2025
  5. IAMtheIAM commented on Mar 1, 2025

    @IAMtheIAM

    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"}}
    
  6. stanleyluong commented on Mar 1, 2025

    @stanleyluong

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

  7. Tom910 commented on Mar 3, 2025

    @Tom910

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

  8. greendesertsnow commented on Mar 3, 2025

    @greendesertsnow

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

  9. assigned and unassigned on Mar 3, 2025
  10. 10 remaining items

  11. robtaylor commented on Mar 14, 2025

    @robtaylor

    Does the dev team have any proposed solutions to this?

  12. baocin commented on Mar 16, 2025

    @baocin

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

  13. Nearcyan commented on Mar 17, 2025

    @Nearcyan

    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.

  14. bcherny commented on Mar 18, 2025

    @bcherny
    Collaborator

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

  15. robtaylor commented on Mar 19, 2025

    @robtaylor

    Seems to be working well!

  16. wickdninja commented on Mar 25, 2025

    @wickdninja

    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

  17. tim-arnold commented on Jun 7, 2025

    @tim-arnold

    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"}}

  18. kengonakajima commented on Jun 12, 2025

    @kengonakajima

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

  19. msitarzewski commented on Jun 24, 2025

    @msitarzewski

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

    ⎿  Context low · Run /compact to compact & continue

  20. autosolutionsai-didac commented on Jun 24, 2025

    @autosolutionsai-didac

    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"}}

  21. edgariscoding commented on Jul 15, 2025

    @edgariscoding

    I'm having this issue as well

  22. at062084 commented on Jul 23, 2025

    @at062084

    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

  23. github-actions commented on Jul 30, 2025

    @github-actions

    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.

  24. locked as resolved and limited conversation to collaborators on Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions