sonda/internal/naming/report.go
tgiachi 1b79a7370d feat(naming): an admin section that shows what Claude costs
- Every question asked of Claude is recorded: when, the kind of list, the model, the tokens read and written (also for an answer that was no name but was billed) and how it ended: a name made, a name taken from the kept ones, or a failure and why. Nothing of the facts that were sent is recorded. Records older than a year are removed (migration 0022).
- GET /providers/anthropic/usage (admin) gives the totals of the last days (1 to 365, 30 by default), a row for each day with calls and an estimate of the cost in dollars, worked out from the tokens and the price of the model. It says it is an estimate and flags calls to a model whose price is not known.
- A Costs section in the admin area shows the last 7, 30 or 90 days.
2026-11-13 11:14:45 +01:00

117 lines
3.4 KiB
Go

package naming
import (
"context"
"errors"
"fmt"
"sort"
"time"
"codeberg.org/squidd/sonda/internal/providers/anthropic"
)
// The longest period a report covers: the records are kept for a year.
const MaxReportDays = 365
// ErrInvalidDays is what a report gives for a period that is not from 1 to MaxReportDays days.
var ErrInvalidDays = errors.New("naming: days must be from 1 to 365")
// DayRow is what happened on one day (UTC) with one model.
type DayRow struct {
Day time.Time
Model string
Made int
Kept int
Failed int
Usage anthropic.Usage
}
// Ledger is the account of the calls: it records them and tells what happened since a moment.
type Ledger interface {
Meter
// Since returns what happened from that moment on, a row for each day and model, the newest day first.
Since(ctx context.Context, since time.Time) ([]DayRow, error)
}
// SetLedger makes the service record every question it asks Claude and lets it report on them. Call it before the
// service is used.
func (s *Service) SetLedger(l Ledger) {
s.meter = l
s.ledger = l
}
// Totals are the calls of a period, or of a day. CostUSD is an estimate worked out from the tokens and the price of the
// model, not the invoice of Anthropic.
type Totals struct {
Made int // names Claude made
Kept int // names taken from the kept ones, which cost nothing
Failed int
Usage anthropic.Usage
CostUSD float64
}
// Day is the totals of one day (UTC).
type Day struct {
Date string // 2006-01-02
Totals
}
// Report is what Claude costs over a period.
type Report struct {
Days int
Model string // the model that is asked now
Estimate bool // always true: the cost is worked out from the tokens
Unpriced bool // some calls were to a model whose price is not known and are not in the cost
Totals Totals
Daily []Day // the days that had calls, the newest first
}
// Summarize makes the report of the rows: the totals, a line for each day, and the cost of each in dollars.
func Summarize(rows []DayRow, days int, model string) Report {
r := Report{Days: days, Model: model, Estimate: true, Daily: []Day{}}
byDay := map[string]*Day{}
for _, row := range rows {
date := row.Day.UTC().Format("2006-01-02")
d := byDay[date]
if d == nil {
d = &Day{Date: date}
byDay[date] = d
}
var cost float64
if p, ok := anthropic.PriceOf(row.Model); ok {
cost = p.Cost(row.Usage)
} else if row.Usage != (anthropic.Usage{}) {
r.Unpriced = true
}
for _, t := range []*Totals{&d.Totals, &r.Totals} {
t.Made += row.Made
t.Kept += row.Kept
t.Failed += row.Failed
t.Usage.Input += row.Usage.Input
t.Usage.Output += row.Usage.Output
t.Usage.CacheRead += row.Usage.CacheRead
t.Usage.CacheWrite += row.Usage.CacheWrite
t.CostUSD += cost
}
}
for _, d := range byDay {
r.Daily = append(r.Daily, *d)
}
sort.Slice(r.Daily, func(i, j int) bool { return r.Daily[i].Date > r.Daily[j].Date })
return r
}
// Report says what Claude cost over the last days (from 1 to MaxReportDays).
func (s *Service) Report(ctx context.Context, days int) (Report, error) {
if days < 1 || days > MaxReportDays {
return Report{}, ErrInvalidDays
}
if s.ledger == nil {
return Summarize(nil, days, anthropic.Model), nil
}
rows, err := s.ledger.Since(ctx, s.now().Add(-time.Duration(days)*24*time.Hour))
if err != nil {
return Report{}, fmt.Errorf("read what Claude cost: %w", err)
}
return Summarize(rows, days, anthropic.Model), nil
}