How to get Agent node's token usage in the next node? #40578
Replies: 1 comment
|
@maxleton-cyber the outputs={
"text": text,
"usage": jsonable_encoder(llm_usage), # ← prompt_tokens, completion_tokens, total_tokens, prices, etc.
"files": ...,
"json": json_output,
**variables,
}So the underlying token usage data (prompt_tokens, completion_tokens, total_tokens, pricing) is already being computed and stored [1]. The gap is that the frontend doesn't declare This is exactly what your #40498 issue tracks [2]. In the meantime, since the data isn't exposed in the UI:
To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
Uh oh!
There was an error while loading. Please reload this page.
Self Checks
1. Is this request related to a challenge you're experiencing? Tell me about your story.
I'm using Dify 1.16.1's new sandboxed "Agent" node (not the classic Agent node) in a workflow. The Agent node only exposes two output variables — text and json — with no native token/usage output variable. However, as shown in the trace panel, the Agent node clearly tracks and displays its token consumption (e.g. "252.421K tokens") internally.
I need to pass this token usage value to the next node (a code node) so I can calculate credit consumption based on it. Right now I have no way to reference the Agent node's actual token usage from downstream nodes — I can only get it by asking the LLM to report it inside its own JSON output, which is unreliable (the model sometimes miscounts or omits it).
Is there a way to expose the Agent node's real token/usage stats as an output variable, similar to how LLM nodes expose usage?
2. Additional context or comments
Screenshot attached showing the trace panel where the Agent node's token usage (252.421K tokens) is visible in the UI, but this value doesn't appear to be accessible as a node output variable in the workflow.

All reactions