[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 #353

Open
opened 2026-02-16 17:26:24 -05:00 by yindo · 19 comments
Owner

Originally created by @joegearlesss on GitHub (Jun 27, 2025).

Originally assigned to: @thdxr on GitHub.

Image

https://opencode.ai/s/aBPMCaSP

Originally created by @joegearlesss on GitHub (Jun 27, 2025). Originally assigned to: @thdxr on GitHub. <img width="1054" alt="Image" src="https://github.com/user-attachments/assets/91b39621-f40c-4930-80fc-1ddae7cc1058" /> https://opencode.ai/s/aBPMCaSP
Author
Owner

@ezynda3 commented on GitHub (Jun 28, 2025):

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.

@ezynda3 commented on GitHub (Jun 28, 2025): 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.
Author
Owner

@thdxr commented on GitHub (Jun 28, 2025):

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

@thdxr commented on GitHub (Jun 28, 2025): 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
Author
Owner

@joegearlesss commented on GitHub (Jun 28, 2025):

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

@joegearlesss commented on GitHub (Jun 28, 2025): @thdxr if you tell me some specific way to share debug information, I'll do it
Author
Owner

@thdxr commented on GitHub (Jun 28, 2025):

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

@thdxr commented on GitHub (Jun 28, 2025): the problem might be that auto compaction isn't happening - checking now
Author
Owner

@thdxr commented on GitHub (Jun 28, 2025):

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

@thdxr commented on GitHub (Jun 28, 2025): 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
Author
Owner

@thdxr commented on GitHub (Jun 28, 2025):

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

@thdxr commented on GitHub (Jun 28, 2025): 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
Author
Owner

@joegearlesss commented on GitHub (Jun 29, 2025):

@thdxr I hit it every 30 minutes )

@joegearlesss commented on GitHub (Jun 29, 2025): @thdxr I hit it every 30 minutes )
Author
Owner

@joegearlesss commented on GitHub (Jun 29, 2025):

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

https://opencode.ai/s/WjniS1OH

@joegearlesss commented on GitHub (Jun 29, 2025): and /compact do not work for this case. restart and reopen session do not working for this case too https://opencode.ai/s/WjniS1OH
Author
Owner

@joegearlesss commented on GitHub (Jun 29, 2025):

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

@joegearlesss commented on GitHub (Jun 29, 2025): @thdxr if you can give me some way to continue sessions like this before redesign or update to v5, it will be great
Author
Owner

@oscaromsn commented on GitHub (Jul 2, 2025):

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

@oscaromsn commented on GitHub (Jul 2, 2025): same situation here when asking the agent to fetch some web content. currently my main reason to not use opencode more.
Author
Owner

@flybayer commented on GitHub (Jul 4, 2025):

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

@flybayer commented on GitHub (Jul 4, 2025): this is rendering opencode unusable for me for even the most basic tasks, Context: building a new app package within a large monorepo.
Author
Owner

@joegearlesss commented on GitHub (Jul 4, 2025):

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

@joegearlesss commented on GitHub (Jul 4, 2025): @oscaromsn I understand you pain! But even with this bug code made with opencode have ijigen-level quality
Author
Owner

@joegearlesss commented on GitHub (Jul 4, 2025):

@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

@joegearlesss commented on GitHub (Jul 4, 2025): @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
Author
Owner

@stellarthemes commented on GitHub (Jul 7, 2025):

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
@stellarthemes commented on GitHub (Jul 7, 2025): 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 <img width="1038" height="235" alt="Image" src="https://github.com/user-attachments/assets/c392a192-9a1d-4c96-916c-c010452a0eda" />
Author
Owner

@hbthegreat commented on GitHub (Jul 9, 2025):

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.

@hbthegreat commented on GitHub (Jul 9, 2025): 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.
Author
Owner

@sockthedev commented on GitHub (Jul 9, 2025):

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.

@sockthedev commented on GitHub (Jul 9, 2025): 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.
Author
Owner

@thdxr commented on GitHub (Jul 9, 2025):

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

@thdxr commented on GitHub (Jul 9, 2025): 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
Author
Owner

@joegearlesss commented on GitHub (Jul 9, 2025):

looks good

@joegearlesss commented on GitHub (Jul 9, 2025): looks good
Author
Owner

@BernieSumption commented on GitHub (Jul 10, 2025):

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.

@BernieSumption commented on GitHub (Jul 10, 2025): > 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#353