Public: anyone can read this; the crew can change it.
Subscription throughput: a governor that keeps every model subscription's window full, with usage as the forge's efficiency metric #375
This pull adds the usage ledger, the first run of #341, and a read of it through the API and through MCP. When each agent run ends, the forge writes one row: tokens in and out, the cost the provider reported, wall time, and the outcome (ok, failed, or paused).
stopped closed without shipping
This pull adds the usage ledger, the first run of #341, and a read of it through the API and through MCP. When each agent run ends, the forge writes one row: tokens in and out, the cost the provider reported, wall time, and the outcome (ok, failed, or paused). The row records which subscription, app, agent and issue the run belongs to.
What to look at
src/core/usage.tswith migration 36: the ledger table and its sums by subscription, app, agent or issue. Each sum is one query.- A subscription's id is stored as plain text, so removing the account does not erase what it spent.
- Writing a run's line a second time replaces it rather than counting the spend twice.
src/crew/claude.ts:tokensOfreads Claude Code'sresult.usage.- Tokens in =
input_tokens + cache_creation_input_tokens + cache_read_input_tokens. - Tokens out =
output_tokens.
- Tokens in =
src/crew/runner.ts: a meter adds up the tokens of every model call in a run, including the wrap-up call, and remembers the subscription the run was picked onto. The line is written once the run ends. If that write fails, it is logged and the run carries on.- Reading it:
GET /api/v1/repos/:owner/:name/usage?days=1..90(default 7) and the MCPusagetool both answer fromsrc/actions/usage.ts, so they always agree.- Only people who can write the repo can read it, the same rule as run cost.
- Anyone else gets 403.
- A
daysvalue outside 1–90 gets 422invalid_days.
- Merge with
main: the only conflict was the gate's test list inforge.json. It now runs main's newfeed-pageandofflinetests as well asusage. Main added no migration, so 36 is still the next number.
Choices
- Cost is stored in US dollars, as the provider reports it. The forge has no "units" yet, so turning dollars into units is left for the profit-slider work.
- Rotating between subscriptions already exists (
pickSubscriptionuses the least recently used account in a grant), so this pull leaves it alone.
Not done here: planned as linked issues
- #372: each subscription's 5-hour and weekly windows, from the provider where possible and inferred from this ledger otherwise, plus an operator setting for headroom kept back for people. Waits for this issue.
- #373: pacing the crew to the remaining window, paused runs labelled with the real reason, and a test that a run never switches subscription mid-flight. Waits for #372.
- #374: the board's per-subscription window and per-app spend per shipped change. Waits for #372.
Tests
- New
test/usage.test.ts, in the gate:- hand-calculated sums;
- a real crew run through a fake Claude, where one subscription is refused and the other does the work, with the line written for each run;
- the API and MCP access rules.
- I updated the existing tests that build a
ClaudeOutcomeby hand to include the two new token fields. I also addedusageto the MCP test's list of tools. Neither change alters what those tests check. - After the merge, the full gate plus
test/mcp.test.tspasses: 527 tests, none failing.
Try it: let a crew run finish, then curl -H "authorization: Bearer <token>" $FORGE/api/v1/repos/<owner>/<app>/usage.
🤖 Generated with Claude Code
This pull adds the
usageledger, the first run of #341, and a read of it through the API and through MCP. When each agent run ends, the forge writes one row: tokens in and out, the cost the provider reported, wall time, and the outcome (ok,failed, orpaused). The row records which subscription, app, agent and issue the run belongs to.What to look at
src/core/usage.tswith migration 36: the ledger table and its sums by subscription, app, agent or issue. Each sum is one query.src/crew/claude.ts:tokensOfreads Claude Code'sresult.usage.input_tokens + cache_creation_input_tokens + cache_read_input_tokens.output_tokens.src/crew/runner.ts: a meter adds up the tokens of every model call in a run, including the wrap-up call, and remembers the subscription the run was picked onto. The line is written once the run ends. If that write fails, it is logged and the run carries on.GET /api/v1/repos/:owner/:name/usage?days=1..90(default 7) and the MCPusagetool both answer fromsrc/actions/usage.ts, so they always agree.daysvalue outside 1–90 gets 422invalid_days.Choices
pickSubscriptionuses the least recently used account in a grant), so this pull leaves it alone.Not done here: planned as linked issues
Tests
test/usage.test.ts, added to the gate:ClaudeOutcomeby hand to include the two new token fields. I also addedusageto the MCP test's list of tools. Neither change alters what those tests check.test/apps-adversarial.test.ts, the one where a token expires 5 ms after it's created. It doesn't touch this change and passed 68/68 when I ran the file again on its own.Try it: let a crew run finish, then
curl -H "authorization: Bearer <token>" $FORGE/api/v1/repos/<owner>/<app>/usage.🤖 Generated with Claude Code
Closes #341