Skip to content

feature: Cost by project in usage, ledger and agentop cost #1816

Closed

Description

@huang195

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)

  1. Add one axis, usage.GroupProject = "project". Make it the same kind of axis as GroupAgent, in both the ring and the ledger.
  2. No project= query parameter. agentop narrows to one project on the client from a group=project reply, the way it narrows Other today (cmd/agentop/tui/agent_scope.go).
  3. An empty project is the series label unknown, as an empty agent is unknown in ledger.labelFor (core/cost/ledger/query.go:526). It cannot collide with a real label, because every real label starts with / or ~.
  4. agent=X&group=project must return per-project series. The per-agent rings (Aggregator.agents, usage.go:808-829) use the same bucket type, so a byProject map on bucket gives this with no extra ring. The ledger gives it through FilterAgent and then Fold (ledger/snapshot.go:31-40).

Design

1. Usage ring (core/cost/usage)

  • snapshot.go: the GroupProject constant, ParseGroup accepts "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)
    • projectLabel maps "" to unknown
    • add GroupProject to unitAxes (571-574) so that SeriesCurrencies works for this axis
  • series.go: a bucket.series(g) case (244-276).
  • The caps are unchanged: maxLabelsPerBucket = 64 with overflow to "(other)", and MaxSeriesInResponse = 16 per response.
  • Replay reads the same Record. Add a test that a replayed archive rebuilds the project series.

2. Ledger (core/cost/ledger)

  • row.go:
    • Row.Project string, json project,omitempty. Follow the "Add, never rename" rule (row.go:41-44). Old rows decode with "".
    • add project to key, key(), overflowKeyFor and overflow
  • writer.go Record (~455): Project: rowLabel(e.Project).
  • query.go:
    • labelFor for GroupProject maps "" to unknown
    • Groupable is true
    • SeriesCurrenciesIn covers the axis
    • TestGroupable_MatchesWhatLabelForCanActuallyProduce must 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 project to costByAxes (line 1148) and to the --by help 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.
  • --agent together with --by stays refused (line 122). Do not change it here.
  • Tests in cmd/agentop/cmd_cost_test.go with fakeUsageServer: a --by project breakdown with an unknown row and a label over 34 runes.

5. Docs

  • docs/session-api.md: group=project and the unknown label.
  • cmd/agentop/README.md: the agentop cost section lists project.

Acceptance

  • Ring test: events with projects A, B and "" give series A, B and unknown for group=project, and the same for agent=claude-code&group=project on that agent's ring.
  • Ledger test: it writes rows with projects, reads group=project for a month window, and old rows with no field read as unknown.
  • Replay test from item 1.
  • go vet and the tests of core, cmd/cortex and cmd/agentop pass with GOWORK=off. Run go mod tidy -diff in 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

enhancementgolaptop

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions