Repository navigation
Description
Parent: #1739. Second of four issues. Depends on: #1815 (it adds SessionEvent.Project). Next: #1817.
Problem
When each event carries SessionEvent.Project, cost must group by it. The goal is that /v1/usage?group=project and agentop cost --by project answer "what did each project cost this month?". Today usage.ParseGroup accepts none, model, method, endpoint, session, agent, currency, status, plugin and host (core/cost/usage/snapshot.go:82-114), and the ledger row key has no project (core/cost/ledger/row.go:148-166).
Decisions (fixed, do not reopen)
- Add one axis,
usage.GroupProject = "project". Make it the same kind of axis asGroupAgent, in both the ring and the ledger. - No
project=query parameter. agentop narrows to one project on the client from agroup=projectreply, the way it narrowsOthertoday (cmd/agentop/tui/agent_scope.go). - An empty project is the series label
unknown, as an empty agent isunknowninledger.labelFor(core/cost/ledger/query.go:526). It cannot collide with a real label, because every real label starts with/or~. agent=X&group=projectmust return per-project series. The per-agent rings (Aggregator.agents,usage.go:808-829) use the samebuckettype, so abyProjectmap onbucketgives this with no extra ring. The ledger gives it throughFilterAgentand thenFold(ledger/snapshot.go:31-40).
Design
1. Usage ring (core/cost/usage)
snapshot.go: theGroupProjectconstant,ParseGroupaccepts"project", and the error text lists it.usage.go:bucket.byProject map[string]Counts- in
foldInto(~1379), beside the agent line:b.noteUnit(GroupProject, addLabel(&b.byProject, projectLabel(e), one), unit) projectLabelmaps "" tounknown- add
GroupProjecttounitAxes(571-574) so thatSeriesCurrenciesworks for this axis
series.go: abucket.series(g)case (244-276).- The caps are unchanged:
maxLabelsPerBucket= 64 with overflow to"(other)", andMaxSeriesInResponse= 16 per response. Replayreads the sameRecord. Add a test that a replayed archive rebuilds the project series.
2. Ledger (core/cost/ledger)
row.go:Row.Project string, jsonproject,omitempty. Follow the "Add, never rename" rule (row.go:41-44). Old rows decode with "".- add
projecttokey,key(),overflowKeyForandoverflow
writer.goRecord(~455):Project: rowLabel(e.Project).query.go:labelForforGroupProjectmaps "" tounknownGroupableis trueSeriesCurrenciesIncovers the axisTestGroupable_MatchesWhatLabelForCanActuallyProducemust still pass
- The joint cap of 64 rows per minute (
maxLabelsPerMinute, row.go:226) now also counts projects. Do not change it. Add a test that a minute with more tuples than the cap folds into(other)with project(other).
3. Session API
/v1/usage (core/sessionapi/usage.go:84) already parses group= through ParseGroup (line 174), so group=project works on the ring path and on the ledger path. Add one handler test for each path, and one for agent=claude-code&group=project.
4. agentop cost --by project (cmd/agentop/cmd_cost.go)
- Add
projecttocostByAxes(line 1148) and to the--byhelp text. writeCostBreakdown(1190-1275) prints labels with%-34s, so a longer path breaks the alignment. Make the LABEL column as wide as the longest label, with a minimum of 34, for every axis. Truncate nothing.--agenttogether with--bystays refused (line 122). Do not change it here.- Tests in
cmd/agentop/cmd_cost_test.gowithfakeUsageServer: a--by projectbreakdown with anunknownrow and a label over 34 runes.
5. Docs
docs/session-api.md:group=projectand theunknownlabel.cmd/agentop/README.md: theagentop costsection listsproject.
Acceptance
- Ring test: events with projects A, B and "" give series A, B and
unknownforgroup=project, and the same foragent=claude-code&group=projecton that agent's ring. - Ledger test: it writes rows with projects, reads
group=projectfor a month window, and old rows with no field read asunknown. - Replay test from item 1.
go vetand the tests ofcore,cmd/cortexandcmd/agentoppass withGOWORK=off. Rungo mod tidy -diffin every module you touch.
Out of scope
The TUI (#1817, #1818). A project= filter. A per-project ring.
Assisted-By: Claude (Anthropic AI) [email protected]
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
chore: Remove the plan document from the PR
docs: Add the plan for cost by project (#1816)
docs: Correct the ledgerServedAxes comment step in the #1816 plan
feat: Add the project axis to the usage ring
Merge pull request #1821 from huang195/feat/cost-by-project-1816-pP1