feat: expose token usage as step outputs#1189
Open
adamhenson wants to merge 3 commits intoanthropics:mainfrom
Open
feat: expose token usage as step outputs#1189adamhenson wants to merge 3 commits intoanthropics:mainfrom
adamhenson wants to merge 3 commits intoanthropics:mainfrom
Conversation
Accumulate input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, and turns during SDK execution and surface them as step outputs. Closes anthropics#59, addresses anthropics#136. Also adds a docs/usage.md section showing how to log, budget-gate, and forward token counts to a cost dashboard. Made-with: Cursor
Made-with: Cursor
Made-with: Cursor
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.
Closes #59, closes #136.
The action tracks token usage internally but never surfaces it as step outputs, so workflows can't gate on cost, log spend, or forward counts to external tools. This PR wires it up.
Changes
base-action/src/run-claude-sdk.ts— ExtendedClaudeRunResultwith 5 new optional fields. Added accumulators in the SDK message loop summingmessage.usagefrom each assistant message, and set them on the result after the loop alongsidenum_turnsfromresultMessage.src/entrypoints/run.ts— 5 newcore.setOutput()calls after conclusion, using?? ""as fallback (same pattern as the rest of the codebase).action.yml— 5 new entries in theoutputs:section.docs/usage.md— New## Tracking Token Usage and Costsection inserted between Structured Outputs and Ways to Tag @claude, with sub-sections covering logging, budget enforcement, and forwarding to a cost dashboard.Usage after this PR
Budget enforcement is now possible via a simple workflow step checking the outputs — partially addressing #136.