Skip to content

feat: add token usage tracking and cost visibility#454

Merged
bradygaster merged 1 commit intobradygaster:devfrom
spboyer:squad/453-token-usage-tracking
Mar 20, 2026
Merged

feat: add token usage tracking and cost visibility#454
bradygaster merged 1 commit intobradygaster:devfrom
spboyer:squad/453-token-usage-tracking

Conversation

@spboyer
Copy link
Copy Markdown
Contributor

@spboyer spboyer commented Mar 18, 2026

Closes #453

Summary

Adds token usage tracking and cost visibility to Squad. Users can now see how many tokens each agent consumes and optionally set budget limits.

What's New

squad cost CLI command

  • View token usage from orchestration logs
  • Filter by agent or view all historical data
  • Formatted table output with per-agent breakdown

BudgetDefinition config

  • Optional perAgentSpawn and perSession token limits
  • Configurable warning threshold (warnAt)
  • defineBudget() builder with full validation (rejects NaN, Infinity, negatives)

Orchestration log enhancement

  • New Token usage row in log entry template
  • Scribe prompt updated to record usage data from agent responses

Tests & Docs

  • 25 unit tests (CostTracker, defineBudget validation, log parsing)
  • 5 CLI integration tests
  • Feature documentation page

Review Notes

  • FIDO review caught NaN/Infinity validation gap in defineBudget() - fixed with Number.isFinite() checks
  • Existing CostTracker class and OTel metrics were already in the SDK - this PR wires them into the user-facing flow
  • Orchestration log change is backward compatible (missing usage rows return null)

@spboyer spboyer force-pushed the squad/453-token-usage-tracking branch from e474c75 to 0f07a60 Compare March 19, 2026 01:43
@spboyer
Copy link
Copy Markdown
Contributor Author

spboyer commented Mar 19, 2026

CI Status

CI is blocked by a pre-existing build issue (#455).

The CLI package depends on @bradygaster/squad-sdk version 0.8.25 from the npm registry (a fixed version, not a workspace link). The published v0.8.25 doesn't include the roles module that was added in PR #369. This causes tsc to fail with 'has no exported member listRoles'.

This affects ALL PRs targeting main that build the CLI, not just this one. Fix tracked in #455.

This PR's changes (cost tracking) are correct and tested - 173 tests pass locally (25 cost + 148 roles). The CI failure is unrelated to cost tracking.

@spboyer
Copy link
Copy Markdown
Contributor Author

spboyer commented Mar 19, 2026

cc @bradygaster — PR is ready but CI is blocked by #455 (roles module not in published SDK). Cost tracking changes are tested and correct, just need the upstream build fix to land first.

@bradygaster bradygaster changed the base branch from main to dev March 20, 2026 03:54
@bradygaster bradygaster reopened this Mar 20, 2026
@bradygaster bradygaster reopened this Mar 20, 2026
@bradygaster bradygaster changed the base branch from dev to main March 20, 2026 04:21
@bradygaster bradygaster changed the base branch from main to dev March 20, 2026 04:21
Add cost tracking infrastructure so users can see token usage per agent,
per session, with optional budget limits.

New features:
- squad cost CLI command (--all, --agent filters)
- BudgetDefinition type + defineBudget() builder for squad.config.ts
- Token usage row in orchestration log template
- Scribe prompt updated to record token usage in logs
- 25 unit tests + 5 CLI integration tests (all passing)
- Documentation page at docs/features/cost-tracking.md

FIDO review findings fixed:
- defineBudget() now rejects NaN and Infinity (Number.isFinite checks)
- Added NaN/Infinity test cases

Closes bradygaster#453

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bradygaster bradygaster force-pushed the squad/453-token-usage-tracking branch from 055f061 to 45063cc Compare March 20, 2026 08:39
@bradygaster bradygaster merged commit 9962943 into bradygaster:dev Mar 20, 2026
2 checks passed
chrislomonico pushed a commit to clomonico/squad that referenced this pull request Mar 26, 2026
…ygaster#468)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

Add token usage tracking and cost visibility for agent sessions

2 participants