Setting the default length to 16 causes the question and answer to be interrupted #6161

Closed
opened 2026-02-21 18:14:20 -05:00 by yindo · 1 comment
Owner

Originally created by @starweavingdream on GitHub (Oct 18, 2024).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.8.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When I was using dify, sometimes the question and answer would be interrupted. By tracking the code locally, I found that when too many questions were asked, prompt_tokens would exceed the max_tokens of the model. At this time, in the method of file api/core/app/apps/base_app_runner.py#recalc_llm_max_tokens, max_tokens would be revalued. However, no matter how the value is taken, if it exceeds the limit, it will default to 16, such as this code.

if prompt_tokens + max_tokens > model_context_tokens:
            max_tokens = max(model_context_tokens - prompt_tokens, 16)
            for parameter_rule in model_config.model_schema.parameter_rules:
                if parameter_rule.name == "max_tokens" or (
                    parameter_rule.use_template and parameter_rule.use_template == "max_tokens"
                ):
                    model_config.parameters[parameter_rule.name] = max_tokens

I have a question, why is it 16, instead of the maximum context of the model configuration?

✔️ Expected Behavior

No response

Actual Behavior

No response

Originally created by @starweavingdream on GitHub (Oct 18, 2024). ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.8.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce When I was using dify, sometimes the question and answer would be interrupted. By tracking the code locally, I found that when too many questions were asked, `prompt_tokens` would exceed the `max_tokens` of the model. At this time, in the method of file `api/core/app/apps/base_app_runner.py#recalc_llm_max_tokens`, `max_tokens` would be revalued. However, no matter how the value is taken, if it exceeds the limit, it will default to `16`, such as this code. ```PYTHON if prompt_tokens + max_tokens > model_context_tokens: max_tokens = max(model_context_tokens - prompt_tokens, 16) for parameter_rule in model_config.model_schema.parameter_rules: if parameter_rule.name == "max_tokens" or ( parameter_rule.use_template and parameter_rule.use_template == "max_tokens" ): model_config.parameters[parameter_rule.name] = max_tokens ``` I have a question, why is it `16`, instead of the maximum context of the model configuration? ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior _No response_
yindo added the 🐞 bugstale labels 2026-02-21 18:14:20 -05:00
yindo closed this issue 2026-02-21 18:14:20 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 18, 2024):

The default value of 16 for max_tokens is set to ensure that the model can still generate a minimal response even when the prompt tokens are close to the model's token limit. This is a safeguard to prevent the system from exceeding the model's token capacity, which could lead to errors or interruptions in processing. However, this choice can indeed result in very short outputs, as you've observed.

The decision to use 16 as the default minimum value might be based on a balance between allowing some response generation and avoiding token limit errors. Unfortunately, there isn't specific documentation or comments in the code explaining why 16 was chosen as the default value [1].

If this default behavior is causing issues, you might consider adjusting the configuration to better suit your needs, if possible, by customizing the max_tokens value in the configuration settings [2].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 18, 2024): <!-- Answer --> The default value of 16 for `max_tokens` is set to ensure that the model can still generate a minimal response even when the prompt tokens are close to the model's token limit. This is a safeguard to prevent the system from exceeding the model's token capacity, which could lead to errors or interruptions in processing. However, this choice can indeed result in very short outputs, as you've observed. The decision to use 16 as the default minimum value might be based on a balance between allowing some response generation and avoiding token limit errors. Unfortunately, there isn't specific documentation or comments in the code explaining why 16 was chosen as the default value <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/app/apps/base_app_runner.py)</sup>. If this default behavior is causing issues, you might consider adjusting the configuration to better suit your needs, if possible, by customizing the `max_tokens` value in the configuration settings <sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/tongyi/llm/qwen-vl-max.yaml)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6161