Skip to content

feat: expose token usage as step outputs#1189

Open
adamhenson wants to merge 3 commits intoanthropics:mainfrom
adamhenson:docs/token-cost-tracking
Open

feat: expose token usage as step outputs#1189
adamhenson wants to merge 3 commits intoanthropics:mainfrom
adamhenson:docs/token-cost-tracking

Conversation

@adamhenson
Copy link
Copy Markdown

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 — Extended ClaudeRunResult with 5 new optional fields. Added accumulators in the SDK message loop summing message.usage from each assistant message, and set them on the result after the loop alongside num_turns from resultMessage.

src/entrypoints/run.ts — 5 new core.setOutput() calls after conclusion, using ?? "" as fallback (same pattern as the rest of the codebase).

action.yml — 5 new entries in the outputs: section.

docs/usage.md — New ## Tracking Token Usage and Cost section 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

- uses: anthropics/claude-code-action@v1
  id: claude
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    prompt: "..."

- name: Log token usage
  run: |
    echo "Input tokens:  ${{ steps.claude.outputs.input_tokens }}"
    echo "Output tokens: ${{ steps.claude.outputs.output_tokens }}"
    echo "Turns:         ${{ steps.claude.outputs.turns }}"

Budget enforcement is now possible via a simple workflow step checking the outputs — partially addressing #136.

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
This was referenced Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mechanism for setting a cap on cost Cost report

1 participant