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.
This commit is contained in:
tgiachi 2026-11-13 11:14:45 +01:00
commit 1b79a7370d

View file

@ -253,6 +253,9 @@ func run() error {
})
// Claude names playlists when an admin turned the provider on with a key. Redirects are never followed there.
claudeClient := anthropic.New()
namingRepo := storage.NewNamingRepo(db)
namingService := naming.NewService(providerSettings, claudeClient, namingRepo, logger)
namingService.SetLedger(namingRepo) // every question to Claude is counted, to show what it costs
providerSettings.SetChecker(providers.Anthropic, claudeClient.Probe)
descriptions := about.NewService(discoveryRepo, about.WikipediaSource{MusicBrainz: musicbrainzClient, Wikipedia: wikipediaClient},
providerSettings, cfg.Wikipedia.Languages, logger)
@ -301,7 +304,7 @@ func run() error {
Playlists: playlists.NewService(storage.NewPlaylistRepo(db)),
Mixes: mixes.NewService(storage.NewMixRepo(db), tasteService, cfg.Listening.MinListen()),
Smart: smart.NewService(storage.NewSmartRepo(db), tasteService, cfg.Listening.MinListen()),
Naming: naming.NewService(providerSettings, claudeClient, storage.NewNamingRepo(db), logger),
Naming: namingService,
Images: pictures,
CoversJob: jobs.Runner{Ctx: ctx, Job: images.NewCoversJob(pictures, imageRepo)},
ArtistImagesJob: jobs.Runner{Ctx: ctx, Job: images.NewArtistImagesJob(pictures, imageRepo)},