Skip to content

feat: capture agents usage and cost - #178

Merged
maxy-shpfy merged 1 commit into
mainfrom
maxy/feat--capture-agents-cost
Nov 18, 2026
Merged

maxy-shpfy merged 1 commit into
mainfrom
maxy/feat--capture-agents-cost

Conversation

@maxy-shpfy

@maxy-shpfy maxy-shpfy commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Captures per-message token usage and dollar cost for locally spawned Pi agents and exposes it as a session rollup. Every finalized assistant turn carries the token counts and USD cost Pi computed from its per-model rate table; the local connector lifts that off each message_end and appends one usage_events row, attributed to the producing agent and whatever Run it was working under.

  • MessageUsage on Pi stdout events + usageFromMessage (pi/utils.ts) lift it into the wire UsageTotals; partial/absent fields read as 0, a message with no usage records nothing.
  • New UsageStore interface with SqliteUsageStore (prod) and InMemoryUsageStore (tests), backed by the append-only usage_events table (migration 0027).
  • PiAgentManager.recordUsage writes fire-and-forget on finalize, so a usage write never blocks or fails the turn it measures.
  • GET /api/sessions/:id/usage returns SessionUsage: a session total plus per-agent and per-run (task) breakdowns, enriched with roster/run metadata.
  • Contracts: UsageTotals, AgentUsage, RunUsage, SessionUsage. Also registers the anthropic/claude-opus-4-8 model option.
  • Docs: new "Usage and cost accounting" section in sessions-and-storage.md.

Scope: only locally spawned Pi agents (Prime + local sub-agents) are metered today; a remote-hosted sub-agent would need its usage threaded through RemoteAgentEvent first.

Screenshots

N/A — backend only.

How to test

  1. Run a session that does real model work (Prime and/or sub-agents).
  2. GET /api/sessions/:id/usage; confirm total, byAgent, and byRun reflect the spend, with cost in USD.
  3. A turn that produces no usage records nothing and never fails.
  4. pnpm --filter @tangent/server test — store/sqliteUsageStore.test.ts, routes/sessions/usage.test.ts, pi/utils.test.ts.

@maxy-shpfy
maxy-shpfy added this pull request to stack #180 November 12, 2026 03:38
@maxy-shpfy
maxy-shpfy removed this pull request from stack #180 November 12, 2026 05:50
@maxy-shpfy
maxy-shpfy force-pushed the maxy/fix--dispose-session-on-archive branch from 1e671f3 to 7dd8abc Compare November 12, 2026 05:52
@maxy-shpfy
maxy-shpfy force-pushed the maxy/feat--capture-agents-cost branch from db466e1 to 6bcb6bc Compare November 12, 2026 06:03
@maxy-shpfy
maxy-shpfy changed the base branch from maxy/fix--dispose-session-on-archive to main November 12, 2026 06:03
@maxy-shpfy
maxy-shpfy marked this pull request as ready for review November 12, 2026 07:56
@maxy-shpfy
maxy-shpfy force-pushed the maxy/feat--capture-agents-cost branch 2 times, most recently from b165e66 to 7c5d89d Compare November 18, 2026 20:27
@maxy-shpfy
maxy-shpfy requested a review from a team as a code owner November 18, 2026 20:27
Co-authored-by: Cursor <cursoragent@cursor.com>
@maxy-shpfy
maxy-shpfy force-pushed the maxy/feat--capture-agents-cost branch from 7c5d89d to 15578be Compare November 18, 2026 21:26
@maxy-shpfy
maxy-shpfy merged commit 216ec5b into main Nov 18, 2026
10 checks passed
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.

2 participants