Skip to content

max_completion_tokens needs to be <= max_tokens - prompt_len #9611

Closed as not planned

Description

@lee-b

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

Activity

  1. hsn8888866666-blip commented on Jan 22, 2026

    @hsn8888866666-blip

    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.

  2. hsn8888866666-blip commented on Jan 22, 2026

    @hsn8888866666-blip
    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
  3. RNGMARTIN commented on Feb 4, 2026

    @RNGMARTIN

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

    图片

    me too. Do you solve this problem?

  4. khli-001 commented on Feb 5, 2026

    @khli-001

    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.

  5. ZeusJupiter commented on Mar 9, 2026

    @ZeusJupiter

    My config:
    {
    "$schema": "https://opencode.ai/config.json",
    "disabled_providers": [],
    "provider": {
    "myprovider": {
    "name": "MyProvider",
    "npm": "@ai-sdk/openai-compatible",
    "models": {
    "glm-4.7-flash": {
    "id": "glm-4.7-flash",
    "name": "GLM-4.7-Flash",
    "family": "glm-flash",
    "attachment": false,
    "reasoning": true,
    "tool_call": true,
    "temperature": true,
    "knowledge": "2025-04",
    "release_date": "2026-01-19",
    "last_updated": "2026-01-19",
    "modalities": { "input": ["text"], "output": ["text"] },
    "open_weights": true,
    "cost": { "input": 0, "output": 0, "cache_read": 0, "cache_write": 0 },
    "limit": { "context": 200000, "output": 131072 }
    }
    },
    "options": {
    "baseURL": "http://127.0.0.1:8000/v1"
    }
    }
    }
    }

    opencode web rebport: max_tokens=32000cannot be greater than max_model_len=max_total_tokens=28000. Please request fewer output tokens. (parameter=max_tokens, value=32000)

  6. removed
    bugSomething isn't working
    on May 3, 2026
  7. github-actions commented on Jul 6, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 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