message truncation gpt-4o-mini #14087

Closed
opened 2026-02-21 19:15:29 -05:00 by yindo · 2 comments
Owner

Originally created by @nicho2 on GitHub (May 21, 2025).

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

1.4.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Hello,

I use an agent

Image

with model gpt-4o-mini and the Azure OpenAI Service Model Plugin (version 0.0.16)
I use the tool capacity

when my conversation is > 128000 tokens , i have this error :

[models] Bad Request Error, Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 134525 tokens (134376 in the messages, 149 in the functions). Please reduce the length of the messages or functions.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}}

With a chatbot the truncation is not automatic?

✔️ Expected Behavior

No response

Actual Behavior

No response

Originally created by @nicho2 on GitHub (May 21, 2025). ### 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 1.4.0 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Hello, I use an agent ![Image](https://github.com/user-attachments/assets/954a197d-2ef0-49a0-81b4-d465c847ec8f) with model gpt-4o-mini and the Azure OpenAI Service Model Plugin (version 0.0.16) I use the tool capacity when my conversation is > 128000 tokens , i have this error : [models] Bad Request Error, Error code: 400 - {'error': {'message': "This model's maximum context length is 128000 tokens. However, your messages resulted in 134525 tokens (134376 in the messages, 149 in the functions). Please reduce the length of the messages or functions.", 'type': 'invalid_request_error', 'param': 'messages', 'code': 'context_length_exceeded'}} With a chatbot the truncation is not automatic? ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior _No response_
yindo added the 🐞 bug label 2026-02-21 19:15:29 -05:00
yindo closed this issue 2026-02-21 19:15:30 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (May 21, 2025):

I found a similar resolved issue regarding message truncation when using the gpt-4o-mini model. The solution involves implementing a pruning mechanism to automatically truncate messages if they exceed the maximum token limit. Here's a code snippet that demonstrates how to prune chat messages:

# prune the chat message if it exceeds the max token limit
curr_message_tokens = self.model_instance.get_llm_num_tokens(prompt_messages)

if curr_message_tokens > max_token_limit:
    pruned_memory = []
    while curr_message_tokens > max_token_limit and len(prompt_messages) > 1:
        pruned_memory.append(prompt_messages.pop(0))
        curr_message_tokens = self.model_instance.get_llm_num_tokens(prompt_messages)

return prompt_messages

This code checks the number of tokens in the current messages and removes messages from the beginning of the list until the token count is within the limit. You can adapt this logic to ensure your messages do not exceed the maximum context length of 128000 tokens [1].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (May 21, 2025): <!-- Answer --> I found a similar resolved issue regarding message truncation when using the gpt-4o-mini model. The solution involves implementing a pruning mechanism to automatically truncate messages if they exceed the maximum token limit. Here's a code snippet that demonstrates how to prune chat messages: ```python # prune the chat message if it exceeds the max token limit curr_message_tokens = self.model_instance.get_llm_num_tokens(prompt_messages) if curr_message_tokens > max_token_limit: pruned_memory = [] while curr_message_tokens > max_token_limit and len(prompt_messages) > 1: pruned_memory.append(prompt_messages.pop(0)) curr_message_tokens = self.model_instance.get_llm_num_tokens(prompt_messages) return prompt_messages ``` This code checks the number of tokens in the current messages and removes messages from the beginning of the list until the token count is within the limit. You can adapt this logic to ensure your messages do not exceed the maximum context length of 128000 tokens <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/memory/token_buffer_memory.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/12229f90-8dfd-4855-b711-d95841706eaa?feedback_type=other)</sup>
Author
Owner

@crazywoola commented on GitHub (May 21, 2025):

With a chatbot the truncation is not automatic?

No it's not. You should reduce the prompt length or max_tokens.

@crazywoola commented on GitHub (May 21, 2025): > With a chatbot the truncation is not automatic? No it's not. You should reduce the prompt length or max_tokens.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#14087