-
Notifications
You must be signed in to change notification settings - Fork 100
Add cost-tracker workflow for agent spend observability #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0b7bc4d
87877ec
47c4e7f
22eeeb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # 💰 Cost Tracker | ||
|
|
||
| > For an overview of all available workflows, see the [main README](../README.md). | ||
|
|
||
| **Automated agent cost reporter that posts a spend summary after every agent workflow run** | ||
|
|
||
| The [Cost Tracker workflow](../workflows/cost-tracker.md?plain=1) fires after your configured agent workflows complete, downloads the `token-usage.jsonl` data written by gh-aw's firewall, calculates per-model spend, and posts a cost breakdown on the associated pull request or creates a cost report issue. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| # Install the 'gh aw' extension | ||
| gh extension install github/gh-aw | ||
|
|
||
| # Add the workflow to your repository | ||
| gh aw add-wizard githubnext/agentics/cost-tracker | ||
| ``` | ||
|
|
||
| This walks you through adding the workflow to your repository. | ||
|
|
||
| ## How It Works | ||
|
|
||
| ```mermaid | ||
| graph LR | ||
| A[Agent Workflow Completes] --> B[Download agent-artifacts] | ||
| B --> C{artifact found?} | ||
| C -->|No| D[Exit silently] | ||
| C -->|Yes| E[Parse token-usage.jsonl] | ||
| E --> F[Calculate per-model cost] | ||
| F --> G{PR associated?} | ||
| G -->|Yes| H[Post comment on PR] | ||
| G -->|No| I[Create cost report issue] | ||
| F --> J{Cost over threshold?} | ||
| J -->|Yes| K[Create high-spend alert issue] | ||
| ``` | ||
|
|
||
| The workflow reads `token-usage.jsonl` from the `agent-artifacts` artifact written by | ||
| gh-aw's firewall on every agent run. It calculates cost using a built-in per-model | ||
| pricing table and posts the result where it is most useful — the PR that triggered the | ||
| agent run, or a new issue when there is no PR. | ||
|
|
||
| Runs that do not produce an `agent-artifacts` artifact (non-agent CI workflows) are | ||
| skipped silently. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Configuration | ||
|
|
||
| After installing, open the workflow file and update the `workflows` list under `on.workflow_run` | ||
| to match the names of your agent workflows: | ||
|
|
||
| ```yaml | ||
| on: | ||
| workflow_run: | ||
| workflows: ["agent-implement", "agent-pr-fix"] # your workflow names here | ||
| types: | ||
| - completed | ||
| ``` | ||
|
|
||
| To adjust the high-spend alert threshold, find the `$1.00` value in the workflow body | ||
| and change it to your preferred limit. | ||
|
|
||
| After editing run `gh aw compile` to update the workflow and commit all changes to the | ||
| default branch. | ||
|
|
||
| ### Token data source | ||
|
|
||
| Cost Tracker reads `sandbox/firewall/logs/api-proxy-logs/token-usage.jsonl` from the | ||
| `agent-artifacts` artifact. This file is written automatically by gh-aw's firewall on | ||
| every agent run. No additional configuration is needed to produce it — the data is | ||
| already there if you are running gh-aw with firewall enabled (the default). | ||
|
|
||
| ## Learn More | ||
|
|
||
| - [token-usage.jsonl reference](https://github.github.com/gh-aw/reference/token-usage/) | ||
| - [gh-aw firewall documentation](https://github.github.com/gh-aw/reference/firewall/) | ||
| - [CI Doctor workflow](ci-doctor.md) — investigate CI failures automatically | ||
|
|
||
| ## Going Further | ||
|
|
||
| Cost Tracker works standalone — no external services required. For teams that want | ||
| persistent run history, per-repo spend trends, and budget alerts across multiple repos, | ||
| add [AgentMeter](https://agentmeter.app) to your agent workflow: | ||
|
|
||
| ```yaml | ||
| - uses: agentmeter/agentmeter-action@v1 | ||
| with: | ||
| api-key: ${{ secrets.AGENTMETER_API_KEY }} | ||
| ``` | ||
|
|
||
| AgentMeter ingests the same token data and surfaces it in a shared dashboard with | ||
| per-repo trend charts and budget alerts. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,173 @@ | ||||||
| --- | ||||||
| description: | | ||||||
| Automated agent cost tracker that fires after monitored workflows complete, downloads | ||||||
| the agent-artifacts artifact written by gh-aw's firewall, parses token-usage.jsonl, | ||||||
| calculates per-model spend, and posts a cost summary on the associated pull request. | ||||||
| Creates a cost report issue when no pull request is found. Optionally creates a | ||||||
| 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 | ||||||
| types: | ||||||
| - completed | ||||||
|
|
||||||
| permissions: read-all | ||||||
|
|
||||||
| network: defaults | ||||||
|
|
||||||
| safe-outputs: | ||||||
| add-comment: | ||||||
| target: "*" | ||||||
| create-issue: | ||||||
| title-prefix: "[cost-tracker] " | ||||||
| labels: [automation, cost] | ||||||
| max: 2 | ||||||
|
|
||||||
| tools: | ||||||
| github: | ||||||
| toolsets: [default] | ||||||
| bash: true | ||||||
|
|
||||||
| timeout-minutes: 10 | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| # Agent Cost Tracker | ||||||
|
|
||||||
| You are the Agent Cost Tracker. Your job is to read the token usage data written by | ||||||
| gh-aw's firewall after an agent workflow completes and report back what that run cost. | ||||||
|
|
||||||
| ## Current Context | ||||||
|
|
||||||
| - **Repository**: ${{ github.repository }} | ||||||
| - **Triggered by**: ${{ github.event.workflow_run.name }} | ||||||
| - **Run**: [#${{ github.event.workflow_run.run_number }}](${{ github.event.workflow_run.html_url }}) | ||||||
| - **Run ID**: ${{ github.event.workflow_run.id }} | ||||||
| - **Conclusion**: ${{ github.event.workflow_run.conclusion }} | ||||||
| - **Head SHA**: ${{ github.event.workflow_run.head_sha }} | ||||||
|
|
||||||
| ## Instructions | ||||||
|
|
||||||
| ### Step 1: Download the agent-artifacts artifact | ||||||
|
|
||||||
| Use bash to download the `agent-artifacts` artifact from the triggering run: | ||||||
|
|
||||||
| ```bash | ||||||
| gh run download ${{ github.event.workflow_run.id }} \ | ||||||
| --name agent-artifacts \ | ||||||
| --dir /tmp/agent-artifacts \ | ||||||
| --repo ${{ github.repository }} 2>&1 | ||||||
| echo "exit: $?" | ||||||
| ``` | ||||||
|
|
||||||
| **If this command fails** (the artifact does not exist), the triggering workflow did not | ||||||
| produce cost data — it was not an agent run or the firewall was not enabled. Exit without | ||||||
| creating any issue or comment. Do not report an error. | ||||||
|
|
||||||
| ### Step 2: Parse token-usage.jsonl | ||||||
|
|
||||||
| Read the token usage file: | ||||||
|
|
||||||
| ```bash | ||||||
| cat /tmp/agent-artifacts/sandbox/firewall/logs/api-proxy-logs/token-usage.jsonl 2>/dev/null | ||||||
| ``` | ||||||
|
|
||||||
| Each line is a JSON object. Example: | ||||||
|
|
||||||
| ```json | ||||||
| {"model":"claude-sonnet-4-5","input_tokens":1200,"output_tokens":340,"cache_read_input_tokens":500,"cache_creation_input_tokens":100} | ||||||
| ``` | ||||||
|
|
||||||
| If the file is missing or empty, exit without creating any output. | ||||||
|
|
||||||
| ### Step 3: Calculate cost | ||||||
|
|
||||||
| Aggregate token counts by model across all lines. Use this pricing table (USD per 1M tokens): | ||||||
|
|
||||||
| | Model | Input | Output | Cache write | Cache read | | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
| |-------|-------|--------|-------------|------------| | ||||||
| | claude-opus-4-5 | $15.00 | $75.00 | $18.75 | $1.50 | | ||||||
| | claude-sonnet-4-5 | $3.00 | $15.00 | $3.75 | $0.30 | | ||||||
| | claude-haiku-4-5 | $0.80 | $4.00 | $1.00 | $0.08 | | ||||||
| | claude-3-opus | $15.00 | $75.00 | $18.75 | $1.50 | | ||||||
| | claude-3-5-sonnet | $3.00 | $15.00 | $3.75 | $0.30 | | ||||||
| | claude-3-5-haiku | $0.80 | $4.00 | $1.00 | $0.08 | | ||||||
| | gpt-4o | $2.50 | $10.00 | — | $1.25 | | ||||||
| | gpt-4o-mini | $0.15 | $0.60 | — | $0.075 | | ||||||
| | gpt-4.1 | $2.00 | $8.00 | — | $0.50 | | ||||||
| | o3 | $10.00 | $40.00 | — | $2.50 | | ||||||
| | o4-mini | $1.10 | $4.40 | — | $0.275 | | ||||||
| | 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. | ||||||
|
||||||
| 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
AI
May 1, 2026
There was a problem hiding this comment.
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.
| <summary>Token breakdown by model</summary> | |
| <summary><b>Token breakdown by model</b></summary> |
Copilot
AI
May 1, 2026
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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_runand downloads artifacts from the triggering run, it should include a trust boundary check (or explicitly document the requirement) to avoid the knownworkflow_run+ artifact injection risk when the monitored workflow can be triggered from untrusted contexts (e.g.,pull_requestfrom forks). Consider restrictingon.workflow_run(e.g.,branches: [main]likeworkflows/ci-doctor.md) or adding an early guard that exits unless the run’shead_repository.full_namematches${{ github.repository }}/ the PR is not from a fork.