Fix token calculation metadata in CoT agent strategy #583

Closed
opened 2026-02-16 10:19:49 -05:00 by yindo · 1 comment
Owner

Originally created by @laipz8200 on GitHub (Aug 22, 2025).

Originally assigned to: @laipz8200 on GitHub.

Self Checks

This is only for bug report
I have searched for existing issues
I confirm that I am using English to submit this report
Please do not modify this template

Dify version

Latest

Plugin version

0.0.23

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

When using the CoT (Chain of Thought) agent strategy with function calling, the execution metadata returned to the client incorrectly formats token usage information. The current implementation only includes partial metadata fields, missing important token calculation details that are needed for proper usage tracking and billing.

✔️ Error log

No explicit error occurs, but the execution metadata lacks complete token usage information including:

  • prompt_tokens
  • prompt_unit_price
  • prompt_price_unit
  • prompt_price
  • completion_tokens
  • completion_unit_price
  • completion_price_unit
  • completion_price
  • latency

This results in incomplete token usage tracking for agents using the function calling strategy.

Originally created by @laipz8200 on GitHub (Aug 22, 2025). Originally assigned to: @laipz8200 on GitHub. ## Self Checks ✅ This is only for bug report ✅ I have searched for existing issues ✅ I confirm that I am using English to submit this report ✅ Please do not modify this template ## Dify version Latest ## Plugin version 0.0.23 ## Cloud or Self Hosted Self Hosted (Source) ## Steps to reproduce When using the CoT (Chain of Thought) agent strategy with function calling, the execution metadata returned to the client incorrectly formats token usage information. The current implementation only includes partial metadata fields, missing important token calculation details that are needed for proper usage tracking and billing. ## ✔️ Error log No explicit error occurs, but the execution metadata lacks complete token usage information including: - prompt_tokens - prompt_unit_price - prompt_price_unit - prompt_price - completion_tokens - completion_unit_price - completion_price_unit - completion_price - latency This results in incomplete token usage tracking for agents using the function calling strategy.
yindo added the bug label 2026-02-16 10:19:49 -05:00
yindo closed this issue 2026-02-16 10:19:49 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Aug 22, 2025):

You're right—the CoT agent strategy's execution metadata currently only includes total_price, currency, and total_tokens, and is missing detailed fields like prompt_tokens, completion_tokens, pricing breakdowns, and latency. Latency is tracked internally but not exposed in the final metadata. This matches your report and is confirmed in the code and recent PRs; there's no shared utility for formatting full token usage metadata yet, and other agent strategies (like ReAct) have similar limitations in their metadata output [source].

To fix this, you’ll want to expand the execution_metadata dictionary in agent-strategies/cot_agent/strategies/function_calling.py to include all the required fields. You can extract these from the LLMUsage object if available, and add latency from the timing logic already present. For example, add:

"prompt_tokens": llm_usage["usage"].prompt_tokens if llm_usage["usage"] else 0,
"prompt_unit_price": llm_usage["usage"].prompt_unit_price if llm_usage["usage"] else 0,
"prompt_price_unit": llm_usage["usage"].prompt_price_unit if llm_usage["usage"] else "",
"prompt_price": llm_usage["usage"].prompt_price if llm_usage["usage"] else 0,
"completion_tokens": llm_usage["usage"].completion_tokens if llm_usage["usage"] else 0,
"completion_unit_price": llm_usage["usage"].completion_unit_price if llm_usage["usage"] else 0,
"completion_price_unit": llm_usage["usage"].completion_price_unit if llm_usage["usage"] else "",
"completion_price": llm_usage["usage"].completion_price if llm_usage["usage"] else 0,
"latency": elapsed_time,  # Use the measured time from your log metadata

If you want consistency and maintainability, consider creating a helper function to format and extract all token usage metadata from LLMUsage, as recent PRs have started doing for token counting [source].

Let me know if you want a more detailed code snippet or guidance on integrating this into your workflow!

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 22, 2025): <!-- Answer --> You're right—the CoT agent strategy's execution metadata currently only includes total_price, currency, and total_tokens, and is missing detailed fields like prompt_tokens, completion_tokens, pricing breakdowns, and latency. Latency is tracked internally but not exposed in the final metadata. This matches your report and is confirmed in the code and recent PRs; there's no shared utility for formatting full token usage metadata yet, and other agent strategies (like ReAct) have similar limitations in their metadata output [[source](https://github.com/langgenius/dify-official-plugins/blob/166c83bfb27eb870c403915fd388fc1dff278871/agent-strategies/cot_agent/strategies/function_calling.py)]. To fix this, you’ll want to expand the execution_metadata dictionary in `agent-strategies/cot_agent/strategies/function_calling.py` to include all the required fields. You can extract these from the `LLMUsage` object if available, and add latency from the timing logic already present. For example, add: ```python "prompt_tokens": llm_usage["usage"].prompt_tokens if llm_usage["usage"] else 0, "prompt_unit_price": llm_usage["usage"].prompt_unit_price if llm_usage["usage"] else 0, "prompt_price_unit": llm_usage["usage"].prompt_price_unit if llm_usage["usage"] else "", "prompt_price": llm_usage["usage"].prompt_price if llm_usage["usage"] else 0, "completion_tokens": llm_usage["usage"].completion_tokens if llm_usage["usage"] else 0, "completion_unit_price": llm_usage["usage"].completion_unit_price if llm_usage["usage"] else 0, "completion_price_unit": llm_usage["usage"].completion_price_unit if llm_usage["usage"] else "", "completion_price": llm_usage["usage"].completion_price if llm_usage["usage"] else 0, "latency": elapsed_time, # Use the measured time from your log metadata ``` If you want consistency and maintainability, consider creating a helper function to format and extract all token usage metadata from `LLMUsage`, as recent PRs have started doing for token counting [[source](https://github.com/langgenius/dify-official-plugins/pull/1491)]. Let me know if you want a more detailed code snippet or guidance on integrating this into your workflow! <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b2ac4525-e9fb-45e2-bf7a-5c8308c80030?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify-official-plugins/issues/1564)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#583