Repository navigation
feat: capture agents usage and cost - #178
Merged
Merged
Conversation
maxy-shpfy
force-pushed
the
maxy/fix--dispose-session-on-archive
branch
from
November 12, 2026 05:52
1e671f3 to
7dd8abc
Compare
maxy-shpfy
force-pushed
the
maxy/feat--capture-agents-cost
branch
from
November 12, 2026 06:03
db466e1 to
6bcb6bc
Compare
maxy-shpfy
changed the base branch from
maxy/fix--dispose-session-on-archive
to
main
November 12, 2026 06:03
maxy-shpfy
marked this pull request as ready for review
November 12, 2026 07:56
maxy-shpfy
force-pushed
the
maxy/feat--capture-agents-cost
branch
2 times, most recently
from
November 18, 2026 20:27
b165e66 to
7c5d89d
Compare
Mbeaulne
approved these changes
Nov 18, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
maxy-shpfy
force-pushed
the
maxy/feat--capture-agents-cost
branch
from
November 18, 2026 21:26
7c5d89d to
15578be
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_endand appends oneusage_eventsrow, attributed to the producing agent and whatever Run it was working under.MessageUsageon Pi stdout events +usageFromMessage(pi/utils.ts) lift it into the wireUsageTotals; partial/absent fields read as 0, a message with nousagerecords nothing.UsageStoreinterface withSqliteUsageStore(prod) andInMemoryUsageStore(tests), backed by the append-onlyusage_eventstable (migration0027).PiAgentManager.recordUsagewrites fire-and-forget on finalize, so a usage write never blocks or fails the turn it measures.GET /api/sessions/:id/usagereturnsSessionUsage: a session total plus per-agent and per-run (task) breakdowns, enriched with roster/run metadata.UsageTotals,AgentUsage,RunUsage,SessionUsage. Also registers theanthropic/claude-opus-4-8model option.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
usagethreaded throughRemoteAgentEventfirst.Screenshots
N/A — backend only.
How to test
GET /api/sessions/:id/usage; confirmtotal,byAgent, andbyRunreflect the spend, with cost in USD.usagerecords nothing and never fails.pnpm --filter @tangent/server test—store/sqliteUsageStore.test.ts,routes/sessions/usage.test.ts,pi/utils.test.ts.