max_completion_tokens needs to be <= max_tokens - prompt_len #6935

Open
opened 2026-02-16 18:05:38 -05:00 by yindo · 4 comments
Owner

Originally created by @lee-b on GitHub (Jan 20, 2026).

Originally assigned to: @thdxr on GitHub.

Description

With local models, opencode often reports errors like this one:

'max_tokens' or 'max_completion_tokens' is too large: 65536. This model's maximum context length is 65536 tokens and your request has 10969 input tokens (65536 > 65536 - 10969). (parameter=max_tokens, value=65536)

There SHOULDN'T be a problem here: the prompt is only 10k, and we have 64k available.

I believe this is happening because opencode is not actually respecting the model's max tokens in its requests to the llm -- in max_completion_tokens.

For example, if max_tokens is 10, and the prompt is 9, then max_completion_tokens should be no more than 1 (min(max_completion_tokens, max_tokens - prompt_len)).

Plugins

Just some skills

OpenCode version

1.1.26

Steps to reproduce

See session transcript. vllm; glm-4.7-flash, 64k context, launching with OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=65536 opencode.

session-ses_424a.md

opencode-redacted.json

Operating System

Linux mint

Terminal

konsole

Originally created by @lee-b on GitHub (Jan 20, 2026). Originally assigned to: @thdxr on GitHub. ### Description With local models, opencode often reports errors like this one: > 'max_tokens' or 'max_completion_tokens' is too large: 65536. This model's maximum context length is 65536 tokens and your request has 10969 input tokens (65536 > 65536 - 10969). (parameter=max_tokens, value=65536) There SHOULDN'T be a problem here: the prompt is only 10k, and we have 64k available. I believe this is happening because opencode is not actually respecting the model's max tokens in its requests to the llm -- in `max_completion_tokens`. For example, if max_tokens is 10, and the prompt is 9, then max_completion_tokens should be no more than 1 (`min(max_completion_tokens, max_tokens - prompt_len)`). ### Plugins Just some skills ### OpenCode version 1.1.26 ### Steps to reproduce See session transcript. vllm; glm-4.7-flash, 64k context, launching with `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=65536 opencode`. [session-ses_424a.md](https://github.com/user-attachments/files/24737298/session-ses_424a.md) [opencode-redacted.json](https://github.com/user-attachments/files/24737323/opencode-redacted.json) ### Operating System Linux mint ### Terminal konsole
yindo added the bug label 2026-02-16 18:05:38 -05:00
Author
Owner

@hsn8888866666-blip commented on GitHub (Jan 21, 2026):

Is this also related to the automatic context compression mechanism? Logically, when the condition "x > z−y" occurs, it indicates that OpenCode failed to perform compression in a timely manner. Claude Code has the same issue, which seems to be a problem with model support.

@hsn8888866666-blip commented on GitHub (Jan 21, 2026): Is this also related to the automatic context compression mechanism? Logically, when the condition "x > z−y" occurs, it indicates that OpenCode failed to perform compression in a timely manner. Claude Code has the same issue, which seems to be a problem with model support.
Author
Owner

@hsn8888866666-blip commented on GitHub (Jan 21, 2026):

Image Meanwhile, the maximum output limit of 128k that I configured did not take effect; the error message still shows "32000 > x - y".

like:

Image
@hsn8888866666-blip commented on GitHub (Jan 21, 2026): <img width="1620" height="763" alt="Image" src="https://github.com/user-attachments/assets/76d1485f-608b-4b16-a994-da9cefda2dcb" /> Meanwhile, the maximum output limit of 128k that I configured did not take effect; the error message still shows "32000 > x - y". like: <img width="1003" height="162" alt="Image" src="https://github.com/user-attachments/assets/f73222de-3034-4e41-ae86-bbabfc5c9f9e" />
Author
Owner

@RNGMARTIN commented on GitHub (Feb 4, 2026):

图片与此同时,我配置的最大输出限制128k未生效;错误信息仍然显示“32000 > x - y”。
比如:

图片

me too. Do you solve this problem?

@RNGMARTIN commented on GitHub (Feb 4, 2026): > <img alt="图片" width="1620" height="763" src="https://private-user-images.githubusercontent.com/249065556/538885721-76d1485f-608b-4b16-a994-da9cefda2dcb.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzAyMDA2NjUsIm5iZiI6MTc3MDIwMDM2NSwicGF0aCI6Ii8yNDkwNjU1NTYvNTM4ODg1NzIxLTc2ZDE0ODVmLTYwOGItNGIxNi1hOTk0LWRhOWNlZmRhMmRjYi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMjA0JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDIwNFQxMDE5MjVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yY2RmNDBjY2RkYzhlODg5MWY2ZDUzMGYyN2Q1MzVkODRkN2EyZDM2N2Q2OWYzZmYyYmVjYzhjMjgxNTFhNTYzJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.2VmSlNAzjy1AHVbnJcqbRr1i9XWpQbTuNC4F6kpcrfQ">与此同时,我配置的最大输出限制128k未生效;错误信息仍然显示“32000 > x - y”。 > 比如: > > <img alt="图片" width="1003" height="162" src="https://private-user-images.githubusercontent.com/249065556/538887627-f73222de-3034-4e41-ae86-bbabfc5c9f9e.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzAyMDA2NjUsIm5iZiI6MTc3MDIwMDM2NSwicGF0aCI6Ii8yNDkwNjU1NTYvNTM4ODg3NjI3LWY3MzIyMmRlLTMwMzQtNGU0MS1hZTg2LWJiYWJmYzVjOWY5ZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMjA0JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDIwNFQxMDE5MjVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0wM2UxN2ExNmViOTVlNzAyYjRkMTc3ZjU5ZGM0ODdlNWE5NDIzMmQyOGNjYzUxYWQ1ZTg4NzUzMWQ2ZmI4OWMwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.57gYfqnA4GLxvBF2G-hLh3Cj9HetkRradqUrfQ-eho0"> me too. Do you solve this problem?
Author
Owner

@khli-001 commented on GitHub (Feb 5, 2026):

easy,
you should set max output tokenm in linux you can export OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=32000;
But there is another bug, only "finish-step" case (opencode/packages/opencode/src/session/processor.ts), it will compact context. If read tool return too many content, it also will trigger this error. I think this is a bug, need being fixed.

@khli-001 commented on GitHub (Feb 5, 2026): easy, you should set max output tokenm in linux you can export OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX=32000; But there is another bug, only "finish-step" case (opencode/packages/opencode/src/session/processor.ts), it will compact context. If read tool return too many content, it also will trigger this error. I think this is a bug, need being fixed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6935