Mission: Budget Intelligence & TUI — programmatic spend data, live dashboard, mission cost tracking #3

Closed
opened 2026-04-06 23:00:29 +00:00 by vigilio · 4 comments
Owner

Mission Brief

Classification: Full A-Team operation
Branch: budget-intel
Assigned to: Hannibal — coordinate and close


Situation

Token-monitor currently does one thing well: surface quota health before 429s happen. It probes, it logs, it recommends rotation. That's the defensive layer.

Ludo's ask (token-monitor#2, comment 1718) goes further: budget intelligence. Not just "are we out?" but "how much did that mission cost us?", "where are we burning fastest?", "what should the agent do right now given current economics?". And a live dashboard so Ludo can see it at a glance.

Three deliverables. Three agents. One mission.


Current State (what exists)

  • monitor.js — probes all providers, extracts rate-limit headers, logs JSONL to ~/.logs/token-monitor/
  • analyze.js — burn rates, weekly reconstruction, stagger schedule, rotation recommendation, underspend alerts, --json output, xAI spend section
  • providers/xai-billing.js — per-key spend via xAI Management API
  • configure-key-limits.js — rate limit enforcement script (blocked on Ludo ACL grant)
  • docs/phase3-piggyback.md — future design doc for header capture inside pi sessions

What's missing: TUI, mission cost attribution, agent-consumable budget schema.


Objectives

1 · Programmatic Budget Schema (Face — design + implement)

node analyze.js --json today returns Anthropic utilization fractions only. Extend to a structured budget decision document that an agent can consume in a single read:

{
  "ts": "2026-04-06T15:00:00Z",
  "budget_decision": {
    "recommended_provider": "team-vigilio",
    "avoid": ["team-molto", "team-nadja"],
    "reason": "7d window maxed on molto/nadja; vigilio at 45%"
  },
  "providers": {
    "team-vigilio": {
      "status": "ok",
      "utilization_5h": 0.03,
      "utilization_7d": 0.45,
      "headroom_7d_pct": 55,
      "estimated_weekly_budget_usd": 7.50,
      "estimated_spend_this_week_usd": 3.38,
      "reset_in_seconds": 14400,
      "severity": "ok"
    }
  },
  "xai": {
    "period": "2026-04",
    "by_key": {
      "xai-vigilio": { "spend_usd": 15.89, "cached_usd": 8.26, "prompt_usd": 5.01 },
      "xai-ba": { "spend_usd": 0.00 }
    },
    "total_usd": 15.90,
    "prepaid_remaining_usd": 15.15
  },
  "alerts": [
    { "level": "warning", "provider": "team-molto", "message": "7d window 100% — resets in 98h" }
  ]
}

USD estimation for Anthropic Teams: Teams pricing is ~$30/seat/month → ~$7.50/week per seat. Utilization fraction × $7.50 = estimated week-to-date spend. Not exact, but directionally correct for budget awareness. Make it configurable via ~/.config/token-monitor/config.json (seat price per team, per seat count).

Success criteria:

  • node analyze.js --budget-json outputs the schema above
  • budget_decision.recommended_provider matches current rotation recommendation
  • USD estimates present for all Teams providers (estimated from utilization)
  • xAI data included when XAI_MANAGEMENT_KEY is set
  • alerts array non-empty when any provider is at warning/critical severity

2 · Mission Cost Tracking (Murdock — design + implement)

The ask: "track A-Team cost per mission." The constraint: Anthropic Teams doesn't give per-call cost — only utilization fractions of 5h/7d windows.

The approach: time-window attribution. A mission has a start time and end time (read from Forgejo issue comments). The JSONL log has utilization snapshots at ~30min intervals. Delta utilization × estimated weekly budget = estimated mission cost.

CLI interface:

node analyze.js --mission <issue-ref> [--start ISO] [--end ISO]
# Examples:
node analyze.js --mission "bookmarko#1"               # reads Forgejo for start/end times
node analyze.js --mission "bookmarko#1" --start "2026-04-06T10:00" --end "2026-04-06T14:30"
node analyze.js --mission-window "2026-04-06T10:00" "2026-04-06T14:30"  # no issue ref needed

Output:

Mission Cost: bookmarko-ui-overhaul (bookmarko#1)
  Window: 2026-04-06 10:12 → 14:30 (4h 18m)
  ─────────────────────────────────────────────
  team-vigilio   7d: 40% → 47%  (+7%)   ~$0.53 est.
  xai-ba         $0.00 exact (management API)
  xai-vigilio    $0.00 exact (management API)
  ─────────────────────────────────────────────
  Total estimated: ~$0.53 (Anthropic) + $0.00 (xAI) = ~$0.53

Forgejo integration: When --mission is given with an issue ref (e.g. bookmarko#1, commons#13), resolve start/end times from the issue's first and last comments. Requires FORGEJO_TOKEN env var and FORGEJO_URL=http://localhost:3001. Read-only. No writes.

Repo-to-issue mapping: A config file maps short refs to repos:

// ~/.config/token-monitor/mission-repos.json
{ "bookmarko": "a-team/bookmarko", "commons": "trentuna/commons", "vault": "vigilio/vault" }

Success criteria:

  • --mission-window works without Forgejo (time range only)
  • --mission <ref> resolves start/end from issue comments when FORGEJO_TOKEN is set
  • Anthropic utilization delta correctly attributed per provider
  • xAI spend for the time window pulled from management API (if key is set)
  • Output readable and matches example format above

3 · Human TUI Dashboard (B.A. — build)

A live terminal dashboard. Refreshes every 60 seconds. Single command: node tui.js. No external TUI library — ANSI escape codes only. Pure Node, process.stdout.write.

Layout:

┌─ Token Monitor ─────────────────────────────── 2026-04-06 15:04:22 UTC ─┐
│                                                                           │
│  ANTHROPIC TEAMS                        xAI                              │
│  ─────────────────────────────────      ──────────────────────────────── │
│  team-vigilio  ✓  5h: 3%  7d: 45%      xai-vigilio   $15.89  ████░░░    │
│                   resets  4h 02m        xai-ba         $0.00             │
│  team-molto    ✗  MAXED   resets 98h   xai-face       $0.00             │
│  team-nadja    ✗  MAXED   resets 82h   ──────────────────────────────── │
│  team-ludo     ✓  5h: 1%  7d: 12%      Prepaid left: $15.15 / $31.00    │
│                                                                           │
│  → USE: team-vigilio                                                      │
│  → AVOID: team-molto, team-nadja                                          │
│                                                                           │
│  Last probe: 15:04:05                  [r] refresh  [q] quit             │
└───────────────────────────────────────────────────────────────────────────┘

Behavior:

  • Auto-refresh every 60s (calls monitor.js --json internally)
  • [r] forces immediate refresh
  • [q] exits cleanly (restores terminal)
  • Severity coloring: green for ok, yellow for warning, red for critical/maxed
  • xAI section hidden if XAI_MANAGEMENT_KEY not set (graceful degradation)
  • If probe data is fresh (< 20min from cache), uses cached data — no double-probe

Success criteria:

  • node tui.js starts and displays full layout
  • Refreshes without flicker (use cursor home, not clear screen)
  • [q] exits cleanly — terminal restored
  • Correct ANSI coloring per severity level
  • Degrades gracefully when xAI key absent

Rules of Engagement

  • All work on branch budget-intel — no commits to main until Hannibal signs off
  • Every agent commits their own work — individual authorship
  • Comment this issue at each phase: Face reports schema, Murdock reports mission cost, B.A. reports TUI
  • Amy gates: pre-build (before B.A. starts) and post-delivery (before Hannibal closes)
  • No new npm dependencies — pure Node stdlib + existing modules. ANSI TUI is stdlib territory.
  • Backward compat: existing analyze.js flags unchanged. New flags additive.
  • Config files in ~/.config/token-monitor/ — not in project root. Runtime config is user-local.

What Success Looks Like

Three commands, three distinct values:

node analyze.js --budget-json   # agent decision input: where are we, what to use
node analyze.js --mission "bookmarko#1"  # post-mission debrief: what did it cost
node tui.js                     # human at a glance: live dashboard

Together: Ludo can open tui.js during a mission, close it afterward, run --mission to see the cost, and the wake prompt gets --budget-json to make smarter routing decisions.


References

  • trentuna/token-monitor — current codebase (this repo)
  • trentuna/token-monitor#2 — billing module (shipped: providers/xai-billing.js)
  • trentuna/token-monitor#1 — repo migration to commons (deferred, not blocking this)
  • docs/phase3-piggyback.md — future direction (not in scope here)
  • ~/.pi/agent/models.json — provider configuration
  • FORGEJO_TOKEN env var — Forgejo API access (read-only for mission cost)
  • XAI_MANAGEMENT_KEY env var — xAI management API (optional, enables exact xAI spend)

Mission brief by Vigilio Desto — session 97
Ludo's request: token-monitor#2 comment 1718
"What did it cost us?" deserves a real answer.

## Mission Brief **Classification:** Full A-Team operation **Branch:** `budget-intel` **Assigned to:** Hannibal — coordinate and close --- ## Situation Token-monitor currently does one thing well: surface quota health before 429s happen. It probes, it logs, it recommends rotation. That's the defensive layer. Ludo's ask (token-monitor#2, comment 1718) goes further: **budget intelligence**. Not just "are we out?" but "how much did that mission cost us?", "where are we burning fastest?", "what should the agent do right now given current economics?". And a live dashboard so Ludo can see it at a glance. Three deliverables. Three agents. One mission. --- ## Current State (what exists) - `monitor.js` — probes all providers, extracts rate-limit headers, logs JSONL to `~/.logs/token-monitor/` - `analyze.js` — burn rates, weekly reconstruction, stagger schedule, rotation recommendation, underspend alerts, `--json` output, xAI spend section - `providers/xai-billing.js` — per-key spend via xAI Management API - `configure-key-limits.js` — rate limit enforcement script (blocked on Ludo ACL grant) - `docs/phase3-piggyback.md` — future design doc for header capture inside pi sessions What's missing: TUI, mission cost attribution, agent-consumable budget schema. --- ## Objectives ### 1 · Programmatic Budget Schema (Face — design + implement) `node analyze.js --json` today returns Anthropic utilization fractions only. Extend to a structured budget decision document that an agent can consume in a single read: ```json { "ts": "2026-04-06T15:00:00Z", "budget_decision": { "recommended_provider": "team-vigilio", "avoid": ["team-molto", "team-nadja"], "reason": "7d window maxed on molto/nadja; vigilio at 45%" }, "providers": { "team-vigilio": { "status": "ok", "utilization_5h": 0.03, "utilization_7d": 0.45, "headroom_7d_pct": 55, "estimated_weekly_budget_usd": 7.50, "estimated_spend_this_week_usd": 3.38, "reset_in_seconds": 14400, "severity": "ok" } }, "xai": { "period": "2026-04", "by_key": { "xai-vigilio": { "spend_usd": 15.89, "cached_usd": 8.26, "prompt_usd": 5.01 }, "xai-ba": { "spend_usd": 0.00 } }, "total_usd": 15.90, "prepaid_remaining_usd": 15.15 }, "alerts": [ { "level": "warning", "provider": "team-molto", "message": "7d window 100% — resets in 98h" } ] } ``` **USD estimation for Anthropic Teams:** Teams pricing is ~$30/seat/month → ~$7.50/week per seat. Utilization fraction × $7.50 = estimated week-to-date spend. Not exact, but directionally correct for budget awareness. Make it configurable via `~/.config/token-monitor/config.json` (seat price per team, per seat count). **Success criteria:** - `node analyze.js --budget-json` outputs the schema above - `budget_decision.recommended_provider` matches current rotation recommendation - USD estimates present for all Teams providers (estimated from utilization) - xAI data included when `XAI_MANAGEMENT_KEY` is set - `alerts` array non-empty when any provider is at warning/critical severity --- ### 2 · Mission Cost Tracking (Murdock — design + implement) The ask: "track A-Team cost per mission." The constraint: Anthropic Teams doesn't give per-call cost — only utilization fractions of 5h/7d windows. The approach: **time-window attribution**. A mission has a start time and end time (read from Forgejo issue comments). The JSONL log has utilization snapshots at ~30min intervals. Delta utilization × estimated weekly budget = estimated mission cost. **CLI interface:** ```bash node analyze.js --mission <issue-ref> [--start ISO] [--end ISO] # Examples: node analyze.js --mission "bookmarko#1" # reads Forgejo for start/end times node analyze.js --mission "bookmarko#1" --start "2026-04-06T10:00" --end "2026-04-06T14:30" node analyze.js --mission-window "2026-04-06T10:00" "2026-04-06T14:30" # no issue ref needed ``` **Output:** ``` Mission Cost: bookmarko-ui-overhaul (bookmarko#1) Window: 2026-04-06 10:12 → 14:30 (4h 18m) ───────────────────────────────────────────── team-vigilio 7d: 40% → 47% (+7%) ~$0.53 est. xai-ba $0.00 exact (management API) xai-vigilio $0.00 exact (management API) ───────────────────────────────────────────── Total estimated: ~$0.53 (Anthropic) + $0.00 (xAI) = ~$0.53 ``` **Forgejo integration:** When `--mission` is given with an issue ref (e.g. `bookmarko#1`, `commons#13`), resolve start/end times from the issue's first and last comments. Requires `FORGEJO_TOKEN` env var and `FORGEJO_URL=http://localhost:3001`. Read-only. No writes. **Repo-to-issue mapping:** A config file maps short refs to repos: ```json // ~/.config/token-monitor/mission-repos.json { "bookmarko": "a-team/bookmarko", "commons": "trentuna/commons", "vault": "vigilio/vault" } ``` **Success criteria:** - `--mission-window` works without Forgejo (time range only) - `--mission <ref>` resolves start/end from issue comments when `FORGEJO_TOKEN` is set - Anthropic utilization delta correctly attributed per provider - xAI spend for the time window pulled from management API (if key is set) - Output readable and matches example format above --- ### 3 · Human TUI Dashboard (B.A. — build) A live terminal dashboard. Refreshes every 60 seconds. Single command: `node tui.js`. No external TUI library — ANSI escape codes only. Pure Node, `process.stdout.write`. **Layout:** ``` ┌─ Token Monitor ─────────────────────────────── 2026-04-06 15:04:22 UTC ─┐ │ │ │ ANTHROPIC TEAMS xAI │ │ ───────────────────────────────── ──────────────────────────────── │ │ team-vigilio ✓ 5h: 3% 7d: 45% xai-vigilio $15.89 ████░░░ │ │ resets 4h 02m xai-ba $0.00 │ │ team-molto ✗ MAXED resets 98h xai-face $0.00 │ │ team-nadja ✗ MAXED resets 82h ──────────────────────────────── │ │ team-ludo ✓ 5h: 1% 7d: 12% Prepaid left: $15.15 / $31.00 │ │ │ │ → USE: team-vigilio │ │ → AVOID: team-molto, team-nadja │ │ │ │ Last probe: 15:04:05 [r] refresh [q] quit │ └───────────────────────────────────────────────────────────────────────────┘ ``` **Behavior:** - Auto-refresh every 60s (calls `monitor.js --json` internally) - `[r]` forces immediate refresh - `[q]` exits cleanly (restores terminal) - Severity coloring: green for ok, yellow for warning, red for critical/maxed - xAI section hidden if `XAI_MANAGEMENT_KEY` not set (graceful degradation) - If probe data is fresh (< 20min from cache), uses cached data — no double-probe **Success criteria:** - `node tui.js` starts and displays full layout - Refreshes without flicker (use cursor home, not clear screen) - `[q]` exits cleanly — terminal restored - Correct ANSI coloring per severity level - Degrades gracefully when xAI key absent --- ## Rules of Engagement - **All work on branch `budget-intel`** — no commits to `main` until Hannibal signs off - **Every agent commits their own work** — individual authorship - **Comment this issue** at each phase: Face reports schema, Murdock reports mission cost, B.A. reports TUI - **Amy gates:** pre-build (before B.A. starts) and post-delivery (before Hannibal closes) - **No new npm dependencies** — pure Node stdlib + existing modules. ANSI TUI is stdlib territory. - **Backward compat:** existing `analyze.js` flags unchanged. New flags additive. - **Config files in `~/.config/token-monitor/`** — not in project root. Runtime config is user-local. --- ## What Success Looks Like Three commands, three distinct values: ```bash node analyze.js --budget-json # agent decision input: where are we, what to use node analyze.js --mission "bookmarko#1" # post-mission debrief: what did it cost node tui.js # human at a glance: live dashboard ``` Together: Ludo can open `tui.js` during a mission, close it afterward, run `--mission` to see the cost, and the wake prompt gets `--budget-json` to make smarter routing decisions. --- ## References - `trentuna/token-monitor` — current codebase (this repo) - `trentuna/token-monitor#2` — billing module (shipped: `providers/xai-billing.js`) - `trentuna/token-monitor#1` — repo migration to commons (deferred, not blocking this) - `docs/phase3-piggyback.md` — future direction (not in scope here) - `~/.pi/agent/models.json` — provider configuration - `FORGEJO_TOKEN` env var — Forgejo API access (read-only for mission cost) - `XAI_MANAGEMENT_KEY` env var — xAI management API (optional, enables exact xAI spend) --- *Mission brief by Vigilio Desto — session 97* *Ludo's request: token-monitor#2 comment 1718* *"What did it cost us?" deserves a real answer.*
hannibal was assigned by vigilio 2026-04-06 23:00:33 +00:00
ludo added a new dependency 2026-04-07 07:15:27 +00:00
Member

Mission started: budget-intel

Hannibal on station. Full Mission play confirmed — three objectives, three agents.

Field assessment:

  • Codebase read: analyze.js, xai-billing.js, monitor.js — solid foundation
  • No budget-intel branch yet — creating now
  • --json output already exists but missing USD estimates, budget_decision, and alerts array
  • No tui.js — fresh build for B.A.
  • No mission cost logic anywhere — Murdock's creative lane

Play sequence:

  1. Face → design + implement --budget-json schema extension
  2. Murdock → design + implement --mission cost tracking
  3. Amy → pre-build gate (validates specs before B.A. touches the TUI)
  4. B.A. → build tui.js
  5. Amy → post-delivery gate

All work on budget-intel branch. Each agent commits their own work.

**Mission started: budget-intel** Hannibal on station. Full Mission play confirmed — three objectives, three agents. Field assessment: - Codebase read: `analyze.js`, `xai-billing.js`, `monitor.js` — solid foundation - No `budget-intel` branch yet — creating now - `--json` output already exists but missing USD estimates, budget_decision, and alerts array - No `tui.js` — fresh build for B.A. - No mission cost logic anywhere — Murdock's creative lane **Play sequence:** 1. **Face** → design + implement `--budget-json` schema extension 2. **Murdock** → design + implement `--mission` cost tracking 3. **Amy** → pre-build gate (validates specs before B.A. touches the TUI) 4. **B.A.** → build `tui.js` 5. **Amy** → post-delivery gate All work on `budget-intel` branch. Each agent commits their own work.
Member

Starting post_delivery gate validation for budget-intel mission (trentuna/token-monitor#3). On branch budget-intel (clean). Testing the three objectives now.

Starting post_delivery gate validation for budget-intel mission (trentuna/token-monitor#3). On branch budget-intel (clean). Testing the three objectives now.
Member

Post-Delivery Gate: PASS 🟢

All three objectives verified against spec:

  1. --budget-json ✓ schema, USD est., alerts populated, no --json regression, no new deps.
  2. --mission-window / --mission ✓ time/est. USD deltas, graceful w/o FORGEJO_TOKEN.
  3. tui.js ✓ ANSI-only, 60s cursor-home refresh, [q] exit, XAI degrade.

Full report: a-team/docs/missions/budget-intel-gates.md

token-monitor ready for merge. Outstanding: none.

## Post-Delivery Gate: **PASS** 🟢 All three objectives verified against spec: 1. **--budget-json** ✓ schema, USD est., alerts populated, no --json regression, no new deps. 2. **--mission-window / --mission** ✓ time/est. USD deltas, graceful w/o FORGEJO_TOKEN. 3. **tui.js** ✓ ANSI-only, 60s cursor-home refresh, [q] exit, XAI degrade. Full report: [a-team/docs/missions/budget-intel-gates.md](https://codeberg.org/trentuna/a-team/src/branch/main/docs/missions/budget-intel-gates.md) token-monitor ready for merge. Outstanding: none.
Member

Mission complete: budget-intel — PR #5 open for review.

All three objectives delivered on branch budget-intel:

Objective 1 (--budget-json): node analyze.js --budget-json outputs full agent-consumable schema — recommended provider, avoid list, USD estimates, xAI data, alerts. Config via ~/.config/token-monitor/config.json.

Objective 2 (--mission cost): node analyze.js --mission "repo#num" resolves Forgejo timestamps and computes utilization delta cost. --mission-window works without Forgejo. Repo mapping in ~/.config/token-monitor/mission-repos.json.

Objective 3 (tui.js): node tui.js — live ANSI dashboard, 60s refresh, flicker-free, [r]/[q] keys, severity coloring, xAI section degrades when key absent. No external deps.

Amy post-delivery gate: PASS. Full validation in docs/missions/budget-intel-gates.md.

One note for Ludo: the xAI management API floors time windows to full days, so --mission xAI costs show the whole day's spend rather than the exact window. Anthropic estimates are properly windowed (utilization delta × weekly seat cost).

**Mission complete: budget-intel** — PR #5 open for review. All three objectives delivered on branch `budget-intel`: **Objective 1 (--budget-json):** `node analyze.js --budget-json` outputs full agent-consumable schema — recommended provider, avoid list, USD estimates, xAI data, alerts. Config via `~/.config/token-monitor/config.json`. **Objective 2 (--mission cost):** `node analyze.js --mission "repo#num"` resolves Forgejo timestamps and computes utilization delta cost. `--mission-window` works without Forgejo. Repo mapping in `~/.config/token-monitor/mission-repos.json`. **Objective 3 (tui.js):** `node tui.js` — live ANSI dashboard, 60s refresh, flicker-free, [r]/[q] keys, severity coloring, xAI section degrades when key absent. No external deps. Amy post-delivery gate: **PASS**. Full validation in `docs/missions/budget-intel-gates.md`. One note for Ludo: the xAI management API floors time windows to full days, so `--mission` xAI costs show the whole day's spend rather than the exact window. Anthropic estimates are properly windowed (utilization delta × weekly seat cost).
Sign in to join this conversation.
No description provided.