Skip to content

Add cost-tracker workflow for agent spend observability#319

Merged
dsyme merged 4 commits intogithubnext:mainfrom
adamhenson:feat/cost-tracker
May 1, 2026
Merged

Add cost-tracker workflow for agent spend observability#319
dsyme merged 4 commits intogithubnext:mainfrom
adamhenson:feat/cost-tracker

Conversation

@adamhenson
Copy link
Copy Markdown
Contributor

Summary

  • Add workflows/cost-tracker.md — fires on workflow_run: completed, downloads the agent-artifacts artifact written by gh-aw's firewall, parses token-usage.jsonl, calculates per-model cost, and posts a spend summary on the associated pull request or creates a cost report issue when no PR is found
  • Add docs/cost-tracker.md — installation instructions, mermaid flow diagram, and configuration guide (modelled after docs/ci-doctor.md)
  • Update README.md — add Cost Tracker to the Fault Analysis Workflows section

Closes #315. Related to #297.

Notes

  • Silent on non-agent runs: if no agent-artifacts artifact exists the workflow produces no output
  • Supports Claude, GPT, and Gemini models with a conservative fallback for unknown models
  • Optional high-spend alert issue when a single run exceeds a configurable threshold (default $1.00)
  • Token data comes from token-usage.jsonl written by gh-aw's firewall — no extra setup needed beyond enabling the firewall (the default)

@dsyme dsyme merged commit e8a4d8a into githubnext:main May 1, 2026
2 of 3 checks passed
@dsyme
Copy link
Copy Markdown
Contributor

dsyme commented May 1, 2026

Thank you @adamhenson!

Comment thread workflows/cost-tracker.md

Aggregate token counts by model across all lines. Use this pricing table (USD per 1M tokens):

| Model | Input | Output | Cache write | Cache read |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewing - hardwiring this model cost table is likely not so great. But I'll leave it as is for now

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new sample “Cost Tracker” agentic workflow to provide per-run spend observability by consuming token-usage.jsonl produced by the gh-aw firewall, then reporting costs back to the related PR (or opening an issue when no PR is associated).

Changes:

  • Add workflows/cost-tracker.md workflow that triggers on workflow_run: completed, downloads agent-artifacts, computes per-model/total cost, and posts a report.
  • Add docs/cost-tracker.md with install/config guidance and a Mermaid flow diagram.
  • Update README.md to list Cost Tracker under “Fault Analysis Workflows”.
Show a summary per file
File Description
workflows/cost-tracker.md New workflow prompt that downloads artifacts, parses token-usage.jsonl, calculates costs, and posts results via safe-outputs.
docs/cost-tracker.md Documentation for installing and configuring Cost Tracker, including data source and optional high-spend alert.
README.md Adds Cost Tracker to the workflow catalog.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 3/3 changed files
  • Comments generated: 4

Comment thread workflows/cost-tracker.md
Comment on lines +7 to +11
high-spend alert issue when a single run exceeds a configurable threshold.

on:
workflow_run:
workflows: ["agent-implement", "agent-pr-fix"] # Edit to match your agent workflow names
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this workflow is triggered via workflow_run and downloads artifacts from the triggering run, it should include a trust boundary check (or explicitly document the requirement) to avoid the known workflow_run + artifact injection risk when the monitored workflow can be triggered from untrusted contexts (e.g., pull_request from forks). Consider restricting on.workflow_run (e.g., branches: [main] like workflows/ci-doctor.md) or adding an early guard that exits unless the run’s head_repository.full_name matches ${{ github.repository }} / the PR is not from a fork.

Suggested change
high-spend alert issue when a single run exceeds a configurable threshold.
on:
workflow_run:
workflows: ["agent-implement", "agent-pr-fix"] # Edit to match your agent workflow names
high-spend alert issue when a single run exceeds a configurable threshold.
To avoid workflow_run artifact injection from untrusted contexts, this workflow only
processes artifacts from trusted upstream runs on the main branch.
on:
workflow_run:
workflows: ["agent-implement", "agent-pr-fix"] # Edit to match your agent workflow names
branches:
- main

Copilot uses AI. Check for mistakes.
Comment thread workflows/cost-tracker.md
| **Total cost** | $TOTAL_COST |

<details>
<summary>Token breakdown by model</summary>
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The report template uses <summary>Token breakdown by model</summary>, but the repo’s shared formatting guidance expects summary text to be wrapped in <b> tags for consistent rendering (see workflows/shared/formatting.md). Consider updating this to <summary><b>…</b></summary> to match the established convention.

Suggested change
<summary>Token breakdown by model</summary>
<summary><b>Token breakdown by model</b></summary>

Copilot uses AI. Check for mistakes.
Comment thread workflows/cost-tracker.md
| gemini-1.5-pro | $1.25 | $5.00 | — | — |
| gemini-2.0-flash | $0.10 | $0.40 | — | — |

For any model not in this table, use $3.00 input / $15.00 output as a conservative fallback.
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback pricing is described as a “conservative fallback”, but $3/$15 is lower than some models already listed (e.g., Opus). For unknown/unsupported models this can under-estimate cost, which is the opposite of conservative. Either change the wording to something like “default fallback”, or choose a true upper-bound fallback rate.

Suggested change
For any model not in this table, use $3.00 input / $15.00 output as a conservative fallback.
For any model not in this table, use $3.00 input / $15.00 output as the default fallback.

Copilot uses AI. Check for mistakes.
Comment thread workflows/cost-tracker.md
Comment on lines +158 to +171
### Step 6: High-spend alert (optional)

If the total cost exceeds **$1.00**, create a second issue using the `create_issue`
GitHub tool with title:
`[cost-tracker] High spend alert: $TOTAL_COST for ${{ github.event.workflow_run.name }}`

Include the full breakdown and a link to the run. The $1.00 threshold is a conservative
starting point. Edit this workflow to raise or lower it to match your budget.

## Guidelines

- **Silent on non-agent runs**: If the artifact does not exist, produce no output at all.
- **One report per run**: Do not create more than one comment or issue per triggering run.
- **Accurate math**: Double-check token counts and cost calculations before posting.
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step 6 instructs creating a second issue for high spend, but the Guidelines section later says “Do not create more than one comment or issue per triggering run.” These are contradictory; please clarify the rule (e.g., “one cost report, plus an optional high-spend alert issue” or enforce a single output type).

Copilot uses AI. Check for mistakes.
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.

Sample workflow: agent spend tracker (token cost observability)

3 participants