feat(workflow): expose prompt and completion tokens in workflow run responses#37531
Open
priyansh19 wants to merge 2 commits into
Open
feat(workflow): expose prompt and completion tokens in workflow run responses#37531priyansh19 wants to merge 2 commits into
priyansh19 wants to merge 2 commits into
Conversation
…esponses Workflow apps previously only returned total_tokens, while chatflow apps expose prompt_tokens and completion_tokens via usage. Most providers price input and output tokens differently, so workflow consumers could not break down cost. The graph runtime already aggregates the split in graph_runtime_state.llm_usage; this surfaces it in the workflow_finished stream event and the blocking workflow response (plus the OpenAPI schema), defaulting to 0 when no LLM usage is recorded. Closes langgenius#34315.
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-06-16 14:18:18.592313562 +0000
+++ /tmp/pyrefly_pr.txt 2026-06-16 14:18:09.689232697 +0000
@@ -3023,6 +3023,10 @@
--> tests/unit_tests/core/app/apps/common/test_workflow_response_converter_resumption.py:31:37
ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account | EndUser` in function `core.app.apps.common.workflow_response_converter.WorkflowResponseConverter.__init__` [bad-argument-type]
--> tests/unit_tests/core/app/apps/common/test_workflow_response_converter_resumption.py:32:14
+ERROR Argument `SimpleNamespace` is not assignable to parameter `application_generate_entity` with type `AdvancedChatAppGenerateEntity | WorkflowAppGenerateEntity` in function `core.app.apps.common.workflow_response_converter.WorkflowResponseConverter.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/common/test_workflow_response_converter_token_usage.py:34:37
+ERROR Argument `SimpleNamespace` is not assignable to parameter `user` with type `Account | EndUser` in function `core.app.apps.common.workflow_response_converter.WorkflowResponseConverter.__init__` [bad-argument-type]
+ --> tests/unit_tests/core/app/apps/common/test_workflow_response_converter_token_usage.py:35:14
ERROR Object of class `NoneType` has no attribute `data` [missing-attribute]
--> tests/unit_tests/core/app/apps/common/test_workflow_response_converter_truncation.py:588:16
ERROR Argument `dict[str, dict[str, str]]` is not assignable to parameter `override_config_dict` with type `AppModelConfigDict | None` in function `core.app.apps.completion.app_config_manager.CompletionAppConfigManager.get_app_config` [bad-argument-type]
|
Contributor
Pyrefly Type Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #34315.
Workflow apps previously returned only
total_tokensin their run results, whilechatflow apps already expose
prompt_tokens(input) andcompletion_tokens(output)through the
usagemetadata. Because most model providers price input and outputtokens differently, workflow consumers had no way to break down or estimate cost from
the run result.
The graph runtime already aggregates the input/output split across all LLM-backed nodes
in
graph_runtime_state.llm_usage(maintained alongside the scalartotal_tokens).This PR surfaces that existing data without changing how it is computed:
WorkflowFinishStreamResponse.Data(the streamingworkflow_finishedevent) nowincludes
prompt_tokensandcompletion_tokens, populated fromgraph_runtime_state.llm_usage.WorkflowAppBlockingResponse.Data(the blocking workflow run response) carries thesame two fields through from the finish event.
WorkflowRunDataschema documents the new fields.The new fields default to
0, so behavior is unchanged for runs without LLM usage andthe change is backward compatible for existing API consumers.
Note on persistence scope
This change exposes the split in the live run responses (streaming + blocking), which is
where the issue reports the gap. Persisting
prompt_tokens/completion_tokenson theworkflow_runstable (so historical reads via the console/service log APIs also show thesplit) would require carrying the fields on the
WorkflowExecutiondomain entity, whichcurrently lives in the external
graphonpackage and only holdstotal_tokens. That canfollow as a separate change once the upstream entity supports it; happy to do it in a
follow-up if maintainers prefer.
Checklist
make lint && make type-check(backend) andcd web && pnpm exec vp staged(frontend) to appease the lint godsFrom Cursor