How to control chatbot(not chatflow) context length? #31835
Replies: 1 comment
-
|
Hi @mysuochao2-boop! I'm Dosu and I’m helping the dify team. To control the chatbot (not chatflow) context length in Dify, you need to adjust how much conversation history is included in each request to the LLM. Dify uses a TokenBufferMemory mechanism that prunes old messages to fit within a token limit. By default, this limit is set to 2000 tokens per request, but it is not user-configurable via the UI or environment variables—it is set in the codebase itself. If you want to change this, you need to modify the max_token_limit parameter in the TokenBufferMemory logic in the code. This will control how much historical dialogue is sent to the model and help you avoid exceeding the DeepSeek-R1 context window of 131072 tokens. For reference, see the relevant code and logic here. There is currently no user-facing setting for this in chatbot mode, and attempts to make the max dialogue history length configurable have not been merged into the project. The default for the Agent Application chatbot is 10 dialogue turns, and for workflow mode, it is 50, but these are also not user-configurable at this time source. If you are hitting the model's context limit, you can reduce the context length by modifying the code to lower the max_token_limit or message_limit in TokenBufferMemory. This will ensure that only the most recent messages are included in the prompt and prevent the error you encountered. To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Self Checks
Content
I use chatbot to build the chat agent, LLM uses deepseek-r1, and an error is reported after multiple rounds of dialogue. May I ask which parameter should I adjust to control the length of historical dialogue context?
The error is as follows:
[deepseek] Error: req_id: 26df2c6d4a PluginInvokeError: {"args":{"description":"[models] Error: API request failed with status code 400: {"error":{"message":"This model's maximum context length is 131072 tokens. However, you requested 131930 tokens (131930 in the messages, 0 in the completion). Please reduce the length of the messages or completion.","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}"},"error_type":"InvokeError","message":"[models] Error: API request failed with status code 400: {"error":{"message":"This model's maximum context length is 131072 tokens. However, you requested 131930 tokens (131930 in the messages, 0 in the completion). Please reduce the length of the messages or completion.","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}"}
Beta Was this translation helpful? Give feedback.
All reactions