Skip to content

[v0.1.162] [Anthropic: Claude Sonnet 4]: AI_APICallError: input length and 'max_tokens' exceed context limit: 140740 + 64000 > 200000, decrease input length or 'max_tokens and try again #491

Closed

Description

@joegearlesss

Activity

  1. ezynda3 commented on Jun 28, 2025

    @ezynda3
    Contributor

    Having the same issue. The default max output used to be 50000 for claude no? Context runs out way to quickly when set to 64k I think.

  2. thdxr commented on Jun 29, 2025

    @thdxr
    Member

    so it turns out if you specify max tokens it needs to be less than (max context - input) but it's hard to know the exact input up front

    thinking about how to deal with this

  3. joegearlesss commented on Jun 29, 2025

    @joegearlesss
    Author

    @thdxr if you tell me some specific way to share debug information, I'll do it

  4. thdxr commented on Jun 29, 2025

    @thdxr
    Member

    the problem might be that auto compaction isn't happening - checking now

  5. thdxr commented on Jun 29, 2025

    @thdxr
    Member

    we have the following logic

          const tokens =
            previous.metadata.assistant.tokens.input +
            previous.metadata.assistant.tokens.cache.read +
            previous.metadata.assistant.tokens.cache.write +
            previous.metadata.assistant.tokens.output
          if (
            model.info.limit.context &&
            tokens >
              Math.max(
                (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9,
                0,
              )
          ) {
            await summarize({
              sessionID: input.sessionID,
              providerID: input.providerID,
              modelID: input.modelID,
            })
            return chat(input)
          }
    

    for anthropic it should be (200,000 - 64,000) * 0.9 = 122,400

    so i'm not sure how it got to 140,740

  6. thdxr commented on Jun 29, 2025

    @thdxr
    Member

    oh i see the issue it only does the compaction check when you send a message, if it's running in a loop for a while it can exceed it and not trigger compaction

    to fix this we need to upgrade to v5 ai sdk

  7. joegearlesss commented on Jun 29, 2025

    @joegearlesss
    Author

    @thdxr I hit it every 30 minutes )

  8. joegearlesss commented on Jun 29, 2025

    @joegearlesss
    Author

    and /compact do not work for this case. restart and reopen session do not working for this case too

    https://opencode.ai/s/WjniS1OH

  9. changed the title [-]AI_APICallError: input length and 'max_tokens' exceed context limit: 140740 + 64000 > 200000, decrease input length or 'max_tokens and try again[/-] [+][v0.1.162] [Anthropic: Claude Sonnet 4]: AI_APICallError: input length and 'max_tokens' exceed context limit: 140740 + 64000 > 200000, decrease input length or 'max_tokens and try again[/+] on Jun 29, 2025
  10. joegearlesss commented on Jun 29, 2025

    @joegearlesss
    Author

    @thdxr if you can give me some way to continue sessions like this before redesign or update to v5, it will be great

  11. self-assigned this
    on Jun 30, 2025
  12. oscaromsn commented on Jul 2, 2025

    @oscaromsn

    same situation here when asking the agent to fetch some web content. currently my main reason to not use opencode more.

  13. added and removed on Jul 3, 2025
  14. 4 remaining items

  15. flybayer commented on Jul 4, 2025

    @flybayer

    this is rendering opencode unusable for me for even the most basic tasks, Context: building a new app package within a large monorepo.

  16. joegearlesss commented on Jul 5, 2025

    @joegearlesss
    Author

    @oscaromsn I understand you pain! But even with this bug code made with opencode have ijigen-level quality

  17. joegearlesss commented on Jul 5, 2025

    @joegearlesss
    Author

    @flybayer we need help @thdxr to fix it, all other tool in code agent space no have so high code quality. I try few in parallel with same codebase/prompts/model

  18. stellarthemes commented on Jul 7, 2025

    @stellarthemes

    every time i hit this, i have to kill and restart opencode. is there a better way? it gets this error when it tries to read a large webpage or largish (600k) file . also, would be nice if i could cut and paste the error instead of a screenshot, but copy /paste doesn't seem to work in opencode

    Image
  19. hbthegreat commented on Jul 9, 2025

    @hbthegreat

    For those looking for a workaround right now. I'm making very fine grained checklists to work through and put specific instructions in AGENTS.MD to stop and ask if you are ready to continue after checking off each item. It lets me manually eyeball the current context number and if its about to blow I get it to update where its up to in the checklist and restart a new session to continue.

    I know this sounds cumbersome but its better than losing the whole chat.

    The other challenge that happens when one of these errors pops is that you lose the last prompt you wrote so if it was very long it means a full rewrite of that one.

  20. sockthedev commented on Jul 9, 2025

    @sockthedev

    another strategy is to make the agent use a markdown file to store its plan and progress. that way you can continue in new sessions or if a session fails. also use compact at each checkpoint.

  21. thdxr commented on Jul 9, 2025

    @thdxr
    Member

    we upgraded to ai sdk v5 which paves the way to fix this issue - note there is no magic fix, what i'm planning to do here is run compaction in between if the context gets too big in the middle of a loop

  22. joegearlesss commented on Jul 9, 2025

    @joegearlesss
    Author

    looks good

  23. BernieSumption commented on Jul 10, 2025

    @BernieSumption

    every time i hit this, i have to kill and restart opencode.

    When this happens to me I run /compact manually and I can continue the session.

  24. thdxr commented on Mar 25, 2026

    @thdxr
    Member

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions