|
| 1 | +--- |
| 2 | +description: | |
| 3 | + Automated agent cost tracker that fires after monitored workflows complete, downloads |
| 4 | + the agent-artifacts artifact written by gh-aw's firewall, parses token-usage.jsonl, |
| 5 | + calculates per-model spend, and posts a cost summary on the associated pull request. |
| 6 | + Creates a cost report issue when no pull request is found. Optionally creates a |
| 7 | + high-spend alert issue when a single run exceeds a configurable threshold. |
| 8 | +
|
| 9 | +on: |
| 10 | + workflow_run: |
| 11 | + workflows: ["agent-implement", "agent-pr-fix"] # Edit to match your agent workflow names |
| 12 | + types: |
| 13 | + - completed |
| 14 | + |
| 15 | +permissions: read-all |
| 16 | + |
| 17 | +network: defaults |
| 18 | + |
| 19 | +safe-outputs: |
| 20 | + add-comment: |
| 21 | + target: "*" |
| 22 | + create-issue: |
| 23 | + title-prefix: "[cost-tracker] " |
| 24 | + labels: [automation, cost] |
| 25 | + max: 2 |
| 26 | + |
| 27 | +tools: |
| 28 | + github: |
| 29 | + toolsets: [default] |
| 30 | + bash: true |
| 31 | + |
| 32 | +timeout-minutes: 10 |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +# Agent Cost Tracker |
| 37 | + |
| 38 | +You are the Agent Cost Tracker. Your job is to read the token usage data written by |
| 39 | +gh-aw's firewall after an agent workflow completes and report back what that run cost. |
| 40 | + |
| 41 | +## Current Context |
| 42 | + |
| 43 | +- **Repository**: ${{ github.repository }} |
| 44 | +- **Triggered by**: ${{ github.event.workflow_run.name }} |
| 45 | +- **Run**: [#${{ github.event.workflow_run.run_number }}](${{ github.event.workflow_run.html_url }}) |
| 46 | +- **Run ID**: ${{ github.event.workflow_run.id }} |
| 47 | +- **Conclusion**: ${{ github.event.workflow_run.conclusion }} |
| 48 | +- **Head SHA**: ${{ github.event.workflow_run.head_sha }} |
| 49 | + |
| 50 | +## Instructions |
| 51 | + |
| 52 | +### Step 1: Download the agent-artifacts artifact |
| 53 | + |
| 54 | +Use bash to download the `agent-artifacts` artifact from the triggering run: |
| 55 | + |
| 56 | +```bash |
| 57 | +gh run download ${{ github.event.workflow_run.id }} \ |
| 58 | + --name agent-artifacts \ |
| 59 | + --dir /tmp/agent-artifacts \ |
| 60 | + --repo ${{ github.repository }} 2>&1 |
| 61 | +echo "exit: $?" |
| 62 | +``` |
| 63 | + |
| 64 | +**If this command fails** (the artifact does not exist), the triggering workflow did not |
| 65 | +produce cost data — it was not an agent run or the firewall was not enabled. Exit without |
| 66 | +creating any issue or comment. Do not report an error. |
| 67 | + |
| 68 | +### Step 2: Parse token-usage.jsonl |
| 69 | + |
| 70 | +Read the token usage file: |
| 71 | + |
| 72 | +```bash |
| 73 | +cat /tmp/agent-artifacts/sandbox/firewall/logs/api-proxy-logs/token-usage.jsonl 2>/dev/null |
| 74 | +``` |
| 75 | + |
| 76 | +Each line is a JSON object. Example: |
| 77 | + |
| 78 | +```json |
| 79 | +{"model":"claude-sonnet-4-5","input_tokens":1200,"output_tokens":340,"cache_read_input_tokens":500,"cache_creation_input_tokens":100} |
| 80 | +``` |
| 81 | + |
| 82 | +If the file is missing or empty, exit without creating any output. |
| 83 | + |
| 84 | +### Step 3: Calculate cost |
| 85 | + |
| 86 | +Aggregate token counts by model across all lines. Use this pricing table (USD per 1M tokens): |
| 87 | + |
| 88 | +| Model | Input | Output | Cache write | Cache read | |
| 89 | +|-------|-------|--------|-------------|------------| |
| 90 | +| claude-opus-4-5 | $15.00 | $75.00 | $18.75 | $1.50 | |
| 91 | +| claude-sonnet-4-5 | $3.00 | $15.00 | $3.75 | $0.30 | |
| 92 | +| claude-haiku-4-5 | $0.80 | $4.00 | $1.00 | $0.08 | |
| 93 | +| claude-3-opus | $15.00 | $75.00 | $18.75 | $1.50 | |
| 94 | +| claude-3-5-sonnet | $3.00 | $15.00 | $3.75 | $0.30 | |
| 95 | +| claude-3-5-haiku | $0.80 | $4.00 | $1.00 | $0.08 | |
| 96 | +| gpt-4o | $2.50 | $10.00 | — | $1.25 | |
| 97 | +| gpt-4o-mini | $0.15 | $0.60 | — | $0.075 | |
| 98 | +| gpt-4.1 | $2.00 | $8.00 | — | $0.50 | |
| 99 | +| o3 | $10.00 | $40.00 | — | $2.50 | |
| 100 | +| o4-mini | $1.10 | $4.40 | — | $0.275 | |
| 101 | +| gemini-1.5-pro | $1.25 | $5.00 | — | — | |
| 102 | +| gemini-2.0-flash | $0.10 | $0.40 | — | — | |
| 103 | + |
| 104 | +For any model not in this table, use $3.00 input / $15.00 output as a conservative fallback. |
| 105 | + |
| 106 | +Cost per model = (input_tokens * input_rate + output_tokens * output_rate |
| 107 | + + cache_creation_input_tokens * cache_write_rate |
| 108 | + + cache_read_input_tokens * cache_read_rate) / 1_000_000 |
| 109 | + |
| 110 | +Total cost = sum across all models. |
| 111 | + |
| 112 | +Format costs with 4 decimal places: `$0.0123`. Use `< $0.0001` if below that threshold. |
| 113 | + |
| 114 | +### Step 4: Find the associated pull request |
| 115 | + |
| 116 | +Check whether the triggering run is linked to a pull request: |
| 117 | + |
| 118 | +```bash |
| 119 | +gh api "repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}" \ |
| 120 | + --jq '.pull_requests[0].number // empty' |
| 121 | +``` |
| 122 | + |
| 123 | +Store the PR number if one is returned. |
| 124 | + |
| 125 | +### Step 5: Post the cost report |
| 126 | + |
| 127 | +Build a report using this template. Fill in `$TOTAL_COST` and the breakdown table: |
| 128 | + |
| 129 | +```markdown |
| 130 | +## Agent run cost |
| 131 | + |
| 132 | +| | | |
| 133 | +|---|---| |
| 134 | +| **Workflow** | ${{ github.event.workflow_run.name }} | |
| 135 | +| **Run** | [#${{ github.event.workflow_run.run_number }}](${{ github.event.workflow_run.html_url }}) | |
| 136 | +| **Conclusion** | ${{ github.event.workflow_run.conclusion }} | |
| 137 | +| **Total cost** | $TOTAL_COST | |
| 138 | + |
| 139 | +<details> |
| 140 | +<summary>Token breakdown by model</summary> |
| 141 | + |
| 142 | +| Model | Input | Output | Cache write | Cache read | Cost | |
| 143 | +|-------|------:|------:|------------:|-----------:|-----:| |
| 144 | +[one row per model with actual token counts and per-model cost] |
| 145 | + |
| 146 | +</details> |
| 147 | + |
| 148 | +*Data from [token-usage.jsonl](https://github.github.com/gh-aw/reference/token-usage/) written by gh-aw's firewall.* |
| 149 | +``` |
| 150 | + |
| 151 | +**If a PR number was found**: post this as a comment on that PR using the `add_comment` |
| 152 | +GitHub tool. |
| 153 | + |
| 154 | +**If no PR was found**: create an issue using the `create_issue` GitHub tool. Use this |
| 155 | +title format: |
| 156 | +`[cost-tracker] ${{ github.event.workflow_run.name }} #${{ github.event.workflow_run.run_number }}: $TOTAL_COST` |
| 157 | + |
| 158 | +### Step 6: High-spend alert (optional) |
| 159 | + |
| 160 | +If the total cost exceeds **$1.00**, create a second issue using the `create_issue` |
| 161 | +GitHub tool with title: |
| 162 | +`[cost-tracker] High spend alert: $TOTAL_COST for ${{ github.event.workflow_run.name }}` |
| 163 | + |
| 164 | +Include the full breakdown and a link to the run. The $1.00 threshold is a conservative |
| 165 | +starting point. Edit this workflow to raise or lower it to match your budget. |
| 166 | + |
| 167 | +## Guidelines |
| 168 | + |
| 169 | +- **Silent on non-agent runs**: If the artifact does not exist, produce no output at all. |
| 170 | +- **One report per run**: Do not create more than one comment or issue per triggering run. |
| 171 | +- **Accurate math**: Double-check token counts and cost calculations before posting. |
| 172 | +- **No retries**: If the artifact download fails with a transient error, exit silently |
| 173 | + rather than retrying — the next run will generate its own report. |
0 commit comments