Unexpected Token Count Reduction in LLM Agent #6657

Closed
opened 2026-02-21 18:16:43 -05:00 by yindo · 4 comments
Owner

Originally created by @MitraSafarinejad on GitHub (Nov 10, 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.9.1-fix1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I encountered an unexpected decrease in token count when using an LLM model. After sending the first question, which resulted in a message token count of 3,085 and an answer token count of 183, I expected the token count to increase in the second question. However, the token count unexpectedly decreased to 1,994 tokens.

I am using Tavily as a tool in this agent, and I confirmed that the message and all previous history were passed to the next question, yet the message token count still decreased. This issue occurs with different models, such as Anthropic Sonnet and LLama 3.1 70B.

Steps to Reproduce:

  • Build an agent with the 4o Mini model, add the Tavily tool, and provide a prompt.
  • Send the first question, which uses the Tavily tool.
  • For the second question, ask a simple question that does not use the tool.

image

image

✔️ Expected Behavior

The token count for the second question should logically increase by the sum of the previous message and answer tokens

Actual Behavior

The token count unexpectedly decreased, making it challenging to predict token usage.

Originally created by @MitraSafarinejad on GitHub (Nov 10, 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.9.1-fix1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I encountered an unexpected decrease in token count when using an LLM model. After sending the first question, which resulted in a message token count of 3,085 and an answer token count of 183, I expected the token count to increase in the second question. However, the token count unexpectedly decreased to 1,994 tokens. I am using Tavily as a tool in this agent, and I confirmed that the message and all previous history were passed to the next question, yet the message token count still decreased. This issue occurs with different models, such as Anthropic Sonnet and LLama 3.1 70B. Steps to Reproduce: - Build an agent with the 4o Mini model, add the Tavily tool, and provide a prompt. - Send the first question, which uses the Tavily tool. - For the second question, ask a simple question that does not use the tool. ![image](https://github.com/user-attachments/assets/04654375-454d-481d-b6c5-fc8e20b3d847) ![image](https://github.com/user-attachments/assets/b4317d74-7380-4c75-a88e-a863045952f5) ### ✔️ Expected Behavior The token count for the second question should logically increase by the sum of the previous message and answer tokens ### ❌ Actual Behavior The token count unexpectedly decreased, making it challenging to predict token usage.
yindo added the 🤖 feat:agent label 2026-02-21 18:16:43 -05:00
yindo closed this issue 2026-02-21 18:16:43 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 10, 2024):

I found a similar issue that might be related to your problem. There was an issue where the tools parameter was not used when calculating tokens for messages, which could potentially affect token counts when using tools like Tavily. The issue was closed, and the solution involved ensuring that the tools parameter is included in the token calculation methods. You might want to check if the tools parameter is correctly passed in your setup.

Here are the changes that were suggested to resolve a similar issue:

  1. In base_app_runner.py:
    Update the calls to get_llm_num_tokens to include the tools parameter.

    # Line 79 ~ 81
    prompt_tokens = model_instance.get_llm_num_tokens(
        prompt_messages,
        tools  # Add this line
    )
    
    # Line 113 ~ 115
    prompt_tokens = model_instance.get_llm_num_tokens(
        prompt_messages,
        tools  # Add this line
    )
    
  2. In agent_history_prompt_transform.py:
    Update the call to get_num_tokens to include the tools parameter.

    # Line 48 ~ 50
    curr_message_tokens = model_type_instance.get_num_tokens(
        self.memory.model_instance.model,
        self.memory.model_instance.credentials,
        self.history_messages,
        tools  # Add this line
    )
    

These changes ensure that the tools parameter is correctly passed and used in the token calculation methods, which might help in resolving the unexpected token count reduction you're experiencing [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Nov 10, 2024): <!-- Answer --> I found a similar issue that might be related to your problem. There was an issue where the `tools` parameter was not used when calculating tokens for messages, which could potentially affect token counts when using tools like Tavily. The issue was closed, and the solution involved ensuring that the `tools` parameter is included in the token calculation methods. You might want to check if the `tools` parameter is correctly passed in your setup. Here are the changes that were suggested to resolve a similar issue: 1. **In `base_app_runner.py`**: Update the calls to `get_llm_num_tokens` to include the `tools` parameter. ```python # Line 79 ~ 81 prompt_tokens = model_instance.get_llm_num_tokens( prompt_messages, tools # Add this line ) # Line 113 ~ 115 prompt_tokens = model_instance.get_llm_num_tokens( prompt_messages, tools # Add this line ) ``` 2. **In `agent_history_prompt_transform.py`**: Update the call to `get_num_tokens` to include the `tools` parameter. ```python # Line 48 ~ 50 curr_message_tokens = model_type_instance.get_num_tokens( self.memory.model_instance.model, self.memory.model_instance.credentials, self.history_messages, tools # Add this line ) ``` These changes ensure that the `tools` parameter is correctly passed and used in the token calculation methods, which might help in resolving the unexpected token count reduction you're experiencing <sup>[[1]](https://github.com/langgenius/dify/issues/6887)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@MitraSafarinejad commented on GitHub (Nov 10, 2024):

these code changes are in version "0.9.1-fixed" but still, I have Token Count Reduction in my message tokens

@MitraSafarinejad commented on GitHub (Nov 10, 2024): these code changes are in version "0.9.1-fixed" but still, I have Token Count Reduction in my message tokens
Author
Owner

@crazywoola commented on GitHub (Nov 10, 2024):

I encountered an unexpected decrease in token count when using an LLM model. After sending the first question, which resulted in a message token count of 3,085 and an answer token count of 183, I expected the token count to increase in the second question. However, the token count unexpectedly decreased to 1,994 tokens.

Is the second question output token is less than the first one? If so, I think there might be a misunderstand that the token is count for every single round, I think you should sum the total tokens your self.

@crazywoola commented on GitHub (Nov 10, 2024): > I encountered an unexpected decrease in token count when using an LLM model. After sending the first question, which resulted in a message token count of 3,085 and an answer token count of 183, I expected the token count to increase in the second question. However, the token count unexpectedly decreased to 1,994 tokens. Is the second question output token is less than the first one? If so, I think there might be a misunderstand that the token is count for every single round, I think you should sum the total tokens your self.
Author
Owner

@MitraSafarinejad commented on GitHub (Nov 11, 2024):

Thank you for the response. @crazywoola
To clarify, are the "message_tokens" sent not accumulated with previous messages?

Logically, for the second question, the message token count should be the sum of question 1 + answer 1 + prompt + question 2. In 90% of my chat logs, this cumulative increase occurs as expected, with the message token count rising with each new question. However, in about 10% of cases, the token count unexpectedly decreases, which seems like a bug.

I’d appreciate it if you could reopen this issue and investigate further. Thank you!

@MitraSafarinejad commented on GitHub (Nov 11, 2024): Thank you for the response. @crazywoola To clarify, are the "message_tokens" sent not accumulated with previous messages? Logically, for the second question, the message token count should be the sum of question 1 + answer 1 + prompt + question 2. In 90% of my chat logs, this cumulative increase occurs as expected, with the message token count rising with each new question. However, in about 10% of cases, the token count unexpectedly decreases, which seems like a bug. I’d appreciate it if you could reopen this issue and investigate further. Thank you!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6657