AI_APICallError with Qwen 3 Coder 480b: Please reduce the length of the messages or completion. Current length is 69553 while limit is 65536 #1072

Closed
opened 2026-02-16 17:29:23 -05:00 by yindo · 12 comments
Owner

Originally created by @adeperio on GitHub (Aug 2, 2025).

Originally assigned to: @thdxr on GitHub.

I am getting the following error:

Image

It goes through a number of tool calls like fetch, and glob, and task and then errors out with that message. Here's the prompt I'm using for reference if it helps recreate the issue:

_I have now downloaded opencode from https://opencode.ai/docs/. I'm trying to set it up and reading the config docs at https://opencode.ai/docs/config/#modes

Can you read through all the docs. I want to learn how to setup MCP servers for me in open code using the opencode.json file. Is there a way to also leverage my existing @CLAUDE.md files in my repo subfolder to help setup opencode?

Before you do anything can you provide me with an over view of what I can do to set things up?_

Originally created by @adeperio on GitHub (Aug 2, 2025). Originally assigned to: @thdxr on GitHub. I am getting the following error: <img width="472" height="79" alt="Image" src="https://github.com/user-attachments/assets/3033ed87-79e9-4a9c-9036-baac82853d88" /> It goes through a number of tool calls like fetch, and glob, and task and then errors out with that message. Here's the prompt I'm using for reference if it helps recreate the issue: _I have now downloaded opencode from https://opencode.ai/docs/. I'm trying to set it up and reading the config docs at https://opencode.ai/docs/config/#modes Can you read through all the docs. I want to learn how to setup MCP servers for me in open code using the opencode.json file. Is there a way to also leverage my existing @CLAUDE.md files in my repo subfolder to help setup opencode? Before you do anything can you provide me with an over view of what I can do to set things up?_
yindo closed this issue 2026-02-16 17:29:23 -05:00
Author
Owner

@adeperio commented on GitHub (Aug 2, 2025):

It looks like it's hitting max content length for the model and not compacting down the chat history when it reaches the limit.

@adeperio commented on GitHub (Aug 2, 2025): It looks like it's hitting max content length for the model and not compacting down the chat history when it reaches the limit.
Author
Owner

@adeperio commented on GitHub (Aug 2, 2025):

Interestingly I also get this error when trying to find the bug in the opencode repo, with opencode. Probably another github issue though :)

Image

Debugging with claude code the error: "AI_APICallError: Please reduce the length of the messages or completion" I get the following:

Summary

The issue is in /packages/opencode/src/session/index.ts around
lines 612-624. The context length check before summarization is
insufficient because:

  1. It only checks the previous assistant message's token count
  2. It doesn't account for the total tokens that will be sent to
    the model, including:
    - System prompts
    - All conversation messages
    - The new user message

When the total exceeds 65536 tokens (Cerebras' limit), the API
throws the error before summarization can help.

Fix

The context check needs to estimate the total tokens that will
be sent. Here's what needs to be changed:

  1. Calculate total tokens including system prompts and all
    messages
  2. Trigger summarization when approaching the limit
  3. Consider implementing a more aggressive summarization
    strategy for models with smaller context windows

The check should happen before line 822 where streamText is
called, and should account for all the messages that will be
sent in the messages array.

@adeperio commented on GitHub (Aug 2, 2025): Interestingly I also get this error when trying to find the bug in the opencode repo, with opencode. Probably another github issue though :) <img width="503" height="93" alt="Image" src="https://github.com/user-attachments/assets/a960e568-f87d-48f6-b141-ec68c058d68b" /> ---- Debugging with claude code the error: "AI_APICallError: Please reduce the length of the messages or completion" I get the following: Summary The issue is in /packages/opencode/src/session/index.ts around lines 612-624. The context length check before summarization is insufficient because: 1. It only checks the previous assistant message's token count 2. It doesn't account for the total tokens that will be sent to the model, including: - System prompts - All conversation messages - The new user message When the total exceeds 65536 tokens (Cerebras' limit), the API throws the error before summarization can help. Fix The context check needs to estimate the total tokens that will be sent. Here's what needs to be changed: 1. Calculate total tokens including system prompts and all messages 2. Trigger summarization when approaching the limit 3. Consider implementing a more aggressive summarization strategy for models with smaller context windows The check should happen before line 822 where streamText is called, and should account for all the messages that will be sent in the messages array.
Author
Owner

@thdxr commented on GitHub (Aug 3, 2025):

what provider are you using where the limit is this low?

@thdxr commented on GitHub (Aug 3, 2025): what provider are you using where the limit is this low?
Author
Owner

@adeperio commented on GitHub (Aug 3, 2025):

Hi @thdxr it's with Cerebras.

@adeperio commented on GitHub (Aug 3, 2025): Hi @thdxr it's with Cerebras.
Author
Owner

@polo871209 commented on GitHub (Aug 8, 2025):

I am facing the similiar issue using copilot/gpt-5 now 🙏

AI_APICallError: prompt token count of 128783 exceeds the limit of 128000

@polo871209 commented on GitHub (Aug 8, 2025): I am facing the similiar issue using copilot/gpt-5 now 🙏 `AI_APICallError: prompt token count of 128783 exceeds the limit of 128000`
Author
Owner

@torbjornvatn commented on GitHub (Aug 13, 2025):

I am facing the similiar issue using copilot/gpt-5 now 🙏

AI_APICallError: prompt token count of 128783 exceeds the limit of 128000

Same here. Is this a limit set by copilot somehow?

@torbjornvatn commented on GitHub (Aug 13, 2025): > I am facing the similiar issue using copilot/gpt-5 now 🙏 > > `AI_APICallError: prompt token count of 128783 exceeds the limit of 128000` Same here. Is this a limit set by copilot somehow?
Author
Owner

@jflaflamme commented on GitHub (Aug 24, 2025):

I got this error just now but what's concerning is the output after was someone's request about django and cloudflare which was totally unrelated to my context and query.

Image
@jflaflamme commented on GitHub (Aug 24, 2025): I got this error just now but what's concerning is the output after was someone's request about django and cloudflare which was totally unrelated to my context and query. <img width="1598" height="827" alt="Image" src="https://github.com/user-attachments/assets/f408113a-99c5-497e-959f-a5bde91d555d" />
Author
Owner

@rekram1-node commented on GitHub (Aug 24, 2025):

@jflaflamme thats very strange… just for clarity it is literally impossible for “someone elses request” to be sent instead of yours (on our end) because the opencode instance is only running on your machine, I guess technically anthropic could have served you the wrong response somehow but I feel like this is the llm confusing itself based on context and other things most likely

@rekram1-node commented on GitHub (Aug 24, 2025): @jflaflamme thats very strange… just for clarity it is literally impossible for “someone elses request” to be sent instead of yours (on our end) because the opencode instance is only running on your machine, I guess technically anthropic could have served you the wrong response somehow but I feel like this is the llm confusing itself based on context and other things most likely
Author
Owner

@jflaflamme commented on GitHub (Aug 24, 2025):

@jflaflamme thats very strange… just for clarity it is literally impossible for “someone elses request” to be sent instead of yours (on our end) because the opencode instance is only running on your machine, I guess technically anthropic could have served you the wrong response somehow but I feel like this is the llm confusing itself based on context and other things most likely

I appreciate the prompt response and I didn't mean to hijack the topic, just the the AI_APICallError about the length of the message error was the same even with Anthropic models selected, perhaps because of the context? Weird too.

Same goes for the possible confusing context, which I never had issues with before with Claude code (or opencode).
Could it be because I switched the model used for the agent within the session?

@jflaflamme commented on GitHub (Aug 24, 2025): > [@jflaflamme](https://github.com/jflaflamme) thats very strange… just for clarity it is literally impossible for “someone elses request” to be sent instead of yours (on our end) because the opencode instance is only running on your machine, I guess technically anthropic could have served you the wrong response somehow but I feel like this is the llm confusing itself based on context and other things most likely I appreciate the prompt response and I didn't mean to hijack the topic, just the the AI_APICallError about the length of the message error was the same even with Anthropic models selected, perhaps because of the context? Weird too. Same goes for the possible confusing context, which I never had issues with before with Claude code (or opencode). Could it be because I switched the model used for the agent within the session?
Author
Owner

@rekram1-node commented on GitHub (Aug 25, 2025):

tbh I am not really sure without seeing your session, as for the error we need to do more context optimizations like:

  • better compact
  • limit tool outputs
  • etc
@rekram1-node commented on GitHub (Aug 25, 2025): tbh I am not really sure without seeing your session, as for the error we need to do more context optimizations like: - better compact - limit tool outputs - etc
Author
Owner

@adeperio commented on GitHub (Aug 25, 2025):

Thanks all for your comments. Just tried Qwen on Cerebras again today to see if I can get it working, but I still keep running into this same issue. I don't get this issue with the anthropic or open AI models. Qwen unfortunately for me is un-useable atm. Let me know if there's other ways I can get more info to help debug this one and I'll see if I can get everyone some more detail. Thanks!

@adeperio commented on GitHub (Aug 25, 2025): Thanks all for your comments. Just tried Qwen on Cerebras again today to see if I can get it working, but I still keep running into this same issue. I don't get this issue with the anthropic or open AI models. Qwen unfortunately for me is un-useable atm. Let me know if there's other ways I can get more info to help debug this one and I'll see if I can get everyone some more detail. Thanks!
Author
Owner

@rekram1-node commented on GitHub (Dec 27, 2025):

[automated] Closing due to 90+ days of inactivity. Feel free to reopen if you still need this!

@rekram1-node commented on GitHub (Dec 27, 2025): [automated] Closing due to 90+ days of inactivity. Feel free to reopen if you still need this!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1072