mirror of
https://github.com/langgenius/dify-official-plugins.git
synced 2026-07-22 01:55:27 -04:00
Fix token calculation metadata in CoT agent strategy #583
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:
This results in incomplete token usage tracking for agents using the function calling strategy.
@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.pyto include all the required fields. You can extract these from theLLMUsageobject if available, and add latency from the timing logic already present. For example, add: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
