diff --git a/README.md b/README.md index 8d32786..1127e1b 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,7 @@ The address can have a path if Lidarr runs under a URL base, and it cannot carry The key is stored in the database, in clear text, and is never returned by the API or written to the log: the list only says `has_api_key`. Because the database holds it, treat database backups like secrets. Removing the key turns the provider off, and each provider describes in the list exactly what it sends. -**Claude (Anthropic)** is the one provider that names things: with it on, a playlist, a mix or a smart playlist can get a name and a line of description that fit its songs. An admin turns it on with an Anthropic API key (`PUT /providers/anthropic`, the key is write-only, and `POST /providers/anthropic/test` reads one model, which costs nothing and sends no text). `POST /playlists/{id}/suggest-name`, `POST /mixes/{id}/suggest-name` and `POST /smart-playlists/{id}/suggest-name` answer with a name, a description and a `source`. They are a pipeline: the facts about the list (up to 5 genres, up to 8 artist names, how many songs, for a smart playlist a few words about its rules, and the language) go to a namer. With the provider off, or when Claude fails or declines, the local namer builds a name from the genres and the artists on this server, and nothing leaves it. When Claude answers, the name is kept (under a hash of the facts, which holds no artist name), so the same facts are not asked, or paid, twice. A suggestion changes nothing: the client decides what to do with it, and the web app offers it where a playlist is renamed. With the provider on, names are also made by themselves: a mix, a smart playlist or a suggested smart playlist that is kept as a playlist with `auto_name` (the web app always sends it) gets Claude's name and line of description, with `name` as the fallback when Claude is off, fails or declines; a smart playlist can be made with no name at all and Claude names it from the songs its rules give (without Claude that is `invalid_input`); and `GET /naming` says whether names are automatic, so that the editor does not ask for one. The model is `claude-opus-5-5`, at low effort; the key is never taken from the environment of the server, and a redirect is never followed. +**Claude (Anthropic)** is the one provider that names things: with it on, a playlist, a mix or a smart playlist can get a name and a line of description that fit its songs. An admin turns it on with an Anthropic API key (`PUT /providers/anthropic`, the key is write-only, and `POST /providers/anthropic/test` reads one model, which costs nothing and sends no text). `POST /playlists/{id}/suggest-name`, `POST /mixes/{id}/suggest-name` and `POST /smart-playlists/{id}/suggest-name` answer with a name, a description and a `source`. They are a pipeline: the facts about the list (up to 5 genres, up to 8 artist names, how many songs, for a smart playlist a few words about its rules, and the language) go to a namer. With the provider off, or when Claude fails or declines, the local namer builds a name from the genres and the artists on this server, and nothing leaves it. When Claude answers, the name is kept (under a hash of the facts, which holds no artist name), so the same facts are not asked, or paid, twice. A suggestion changes nothing: the client decides what to do with it, and the web app offers it where a playlist is renamed. With the provider on, names are also made by themselves: a mix, a smart playlist or a suggested smart playlist that is kept as a playlist with `auto_name` (the web app always sends it) gets Claude's name and line of description, with `name` as the fallback when Claude is off, fails or declines; a smart playlist can be made with no name at all and Claude names it from the songs its rules give (without Claude that is `invalid_input`); and `GET /naming` says whether names are automatic, so that the editor does not ask for one. Every question to Claude is counted (a made name, a name taken from the kept ones, a failure with its reason, and the tokens that were billed, also for a failure that was billed) and nothing of what was sent is kept; `GET /providers/anthropic/usage?days=30` (admin) gives the totals, a row for each day and an **estimate** of the cost in dollars, worked out from the tokens and the price of the model (the invoice of Anthropic is the truth), and the Costs section of the admin area shows it for 7, 30 or 90 days; records older than a year are removed. The model is `claude-opus-5-5`, at low effort; the key is never taken from the environment of the server, and a redirect is never followed. **What leaves your server, when a provider is on:** to Anthropic, only when someone asks for a name, up to 5 genres and 8 artist names of that list, how many songs it has, a few words about the rules of a smart playlist, the language, your API key, your IP address and a User-Agent (never your users, your listens, track titles or file paths); to Last.fm, the artist name (or MusicBrainz id) being looked up, your API key, your IP address and the `User-Agent`; to MusicBrainz, the MusicBrainz id of the artist (and, only for the covers of albums that have no id, the artist's name and the album's title: see **Covers**), your IP address and a `User-Agent` that carries your contact. Never your users, your listens or file paths. Requests only happen when someone asks for them, never as a bulk pass over your library (the exceptions are the three jobs that an admin starts on purpose, each of them one request that goes through the whole library, one item after the other, spaced out, and each stops when its providers keep failing: `POST /library/covers`, with the Cover Art Archive on, sends the release group id of every album that has no picture in its files (and, for the albums whose files have no id, with MusicBrainz on, the artist and the title, one request a second); `POST /library/artist-images`, with Commons and MusicBrainz on, sends the MusicBrainz id of every artist that has one, to MusicBrainz, Wikidata and Commons; `POST /library/descriptions`, with Wikipedia and MusicBrainz on, sends it to MusicBrainz, Wikidata and Wikipedia), and they are spaced out, retried with backoff, and stopped by a circuit breaker if a provider is down. When MusicBrainz answers that an id was merged into another, Sonda follows that one redirect, and only to an artist or a release group of musicbrainz.org over https; the new id is used for that request and is not stored, and any other redirect is refused. **Descriptions** (the Wikipedia provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata and the title of the page to Wikipedia, with your IP address and a `User-Agent` that carries a contact (the one set for each provider; Wikimedia refuses requests without one), and never a name. **Photos** (the Commons provider, which also needs MusicBrainz on) send the MusicBrainz id to MusicBrainz, the Wikidata item to Wikidata, the name of the picture to Commons and the address of a copy to Wikimedia, with your IP address and a `User-Agent` with a contact, and never a name. **Covers** (the Cover Art Archive provider) send the MusicBrainz release group id of an album, read from the tags of its own files, and only when the files have no picture. When the files have no id either and the MusicBrainz provider is on too, the name of the album's artist and its title go to MusicBrainz, which is asked for the release group: a result is taken only when MusicBrainz scores it 95 or more and its title and artist are the ones asked for (compared without case, accents or punctuation), and anything doubtful is no cover. A title that ends with a note about the disc or the edition (`(disc 2)`, `(bonus disc)`, `(Deluxe Edition)`) is asked as it is first and, if MusicBrainz is not sure, once more without the note, because a release group is named after the album and not after a disc. The id that comes back is used for that request and is not stored: it is not what your files say, and it is never used to read an artist or to send anything to Lidarr; the redirects of the archive are followed only to archive.org, over https. Its covers have no licence of their own: Sonda keeps a copy for your library and never treats it as free. **Discover** asks Last.fm for the similar artists of each seed (the seed's name or MusicBrainz id) and, for the best suggestions that have no tags yet, for the tags of that suggestion (its name); never MusicBrainz. Seeds you name are sent when you ask. Seeds taken from your listening, which are the names of the artists you play most, are sent only if you turned on `discover_from_listening` yourself; otherwise Discover refuses to run without a `seed`. **Lidarr** (your own server, at the address an admin set) gets the MusicBrainz id of an artist an admin sends, or of an artist someone opens `GET /artists/{id}/lidarr` for, and the API key, nothing else; asking it what it offers and testing the key send only the key. Never a name, your users, your listens or your files. What Lidarr then does is up to its own indexers and download clients. Besides the three jobs, the other exception to "only when someone asks" is `POST /providers/{name}/test`, which an admin sends to check a key (or, for Lidarr, the address and the key); it sends nothing but the key. @@ -316,6 +316,7 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er | admin | `POST /library/scan`, `POST /libraries/{id}/scan` | Scan everything, or one library. `?full=true` reads every file again. | | admin | `GET /library/duplicates`; `POST`, `GET`, `DELETE /library/duplicates/check`; `POST /library/duplicates/clean` | Find the songs that are in more than one file, read their files to tell the identical ones, and delete the extra copies. See **Duplicates**. | | admin | `GET /providers`, `PUT /providers/{name}`, `POST /providers/{name}/test`, `DELETE /providers/{name}/data` | See, set up and check the providers (Last.fm, MusicBrainz, Wikipedia, Commons, the Cover Art Archive, Lidarr), and remove what they gave. The API key is write-only. | +| admin | `GET /providers/anthropic/usage` | What the Claude provider costs over the last days (query `days`, 1 to 365): totals, a row for each day with calls and an estimate in dollars. | | admin | `GET /providers/lidarr/options` | The profiles and root folders that Lidarr offers, to choose the ones artists are sent with. | | admin | `POST /artists/{id}/lidarr` | Send an artist to your own Lidarr, by its MusicBrainz id, which then looks for the music. Never by name, never by itself. | | signed in | `GET /artists/{id}/lidarr` | Whether Lidarr has the artist and monitors it. | diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index e1eeba9..e8f7d25 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -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)}, diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 58ae356..c6767d5 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -112,7 +112,9 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun deps.Playlists = playlists.NewService(storage.NewPlaylistRepo(db)) deps.Mixes = mixes.NewService(storage.NewMixRepo(db), deps.Taste.(mixes.Taster), 20*time.Second) deps.Smart = smart.NewService(storage.NewSmartRepo(db), deps.Taste.(smart.Taster), 20*time.Second) - deps.Naming = naming.NewService(prov, noClaude{}, storage.NewNamingRepo(db), nil) + namingSvc := naming.NewService(prov, noClaude{}, storage.NewNamingRepo(db), nil) + namingSvc.SetLedger(storage.NewNamingRepo(db)) + deps.Naming = namingSvc imageRepo := storage.NewImageRepo(db) imageSvc := images.NewService(filepath.Join(t.TempDir(), "data"), imageRepo, imageRepo) deps.Images, deps.ArtistImages = imageSvc, imageSvc diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index c2fe3c2..f2e2aac 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -1085,6 +1085,12 @@ func (c *contract) smartPlaylists(t *testing.T, token string) { // names asks for names with the Claude provider off, so that the local ones answer. func (c *contract) names(t *testing.T, token string) { t.Helper() + c.expect(c.call("GET", "/providers/anthropic/usage", "", nil), 401, "the costs without a token", "unauthorized") + use := c.expect(c.call("GET", "/providers/anthropic/usage?days=7", token, nil), 200, "the costs") + if use["days"] != float64(7) || use["estimate"] != true || len(use["daily"].([]any)) != 0 { + t.Fatalf("costs = %v", use) + } + c.expect(c.call("GET", "/providers/anthropic/usage?days=366", token, nil), 400, "the costs of too many days", "invalid_input") c.expect(c.call("GET", "/naming", "", nil), 401, "naming status without a token", "unauthorized") if st := c.expect(c.call("GET", "/naming", token, nil), 200, "naming status"); st["auto"] != false { t.Fatalf("naming status = %v, want off", st) diff --git a/internal/api/http/handlers_naming.go b/internal/api/http/handlers_naming.go index 2ce52e9..eb94a4b 100644 --- a/internal/api/http/handlers_naming.go +++ b/internal/api/http/handlers_naming.go @@ -1,6 +1,7 @@ package httpapi import ( + "errors" "net/http" "github.com/gin-gonic/gin" @@ -140,3 +141,64 @@ func automaticName(c *gin.Context, svc namingService, auto bool, lang, kind stri } return s.Name, s.Description } + +type usageTotalsJSON struct { + NamesMade int `json:"names_made"` + NamesKept int `json:"names_kept"` + Failures int `json:"failures"` + InputTokens int64 `json:"input_tokens"` + OutputTokens int64 `json:"output_tokens"` + CacheReadTokens int64 `json:"cache_read_tokens"` + CacheWriteTokens int64 `json:"cache_write_tokens"` + CostUSD float64 `json:"cost_usd"` +} + +type usageDayJSON struct { + Date string `json:"date"` + usageTotalsJSON +} + +type usageJSON struct { + Days int `json:"days"` + Model string `json:"model"` + Estimate bool `json:"estimate"` + Unpriced bool `json:"unpriced"` + Totals usageTotalsJSON `json:"totals"` + Daily []usageDayJSON `json:"daily"` +} + +func toUsageTotals(t naming.Totals) usageTotalsJSON { + return usageTotalsJSON{ + NamesMade: t.Made, NamesKept: t.Kept, Failures: t.Failed, + InputTokens: t.Usage.Input, OutputTokens: t.Usage.Output, CacheReadTokens: t.Usage.CacheRead, CacheWriteTokens: t.Usage.CacheWrite, + CostUSD: t.CostUSD, + } +} + +// usage says what the calls to Claude cost over the last days (30 by default): the totals, a line for each day that had +// calls, and an estimate in dollars worked out from the tokens. It is for admins, and it holds nothing of the facts that +// were sent. +func (h *namingHandlers) usage(c *gin.Context) { + days, ok := queryInt(c, "days") + if !ok { + return + } + if days == 0 { + days = 30 + } + r, err := h.svc.Report(c.Request.Context(), clampInt(days)) + if errors.Is(err, naming.ErrInvalidDays) { + writeError(c, http.StatusBadRequest, "invalid_input", "days must be a whole number from 1 to 365") + return + } + if err != nil { + _ = c.Error(err) + writeError(c, http.StatusInternalServerError, "internal", "internal error") + return + } + out := usageJSON{Days: r.Days, Model: r.Model, Estimate: r.Estimate, Unpriced: r.Unpriced, Totals: toUsageTotals(r.Totals), Daily: make([]usageDayJSON, len(r.Daily))} + for i, d := range r.Daily { + out.Daily[i] = usageDayJSON{Date: d.Date, usageTotalsJSON: toUsageTotals(d.Totals)} + } + c.JSON(http.StatusOK, out) +} diff --git a/internal/api/http/naming_test.go b/internal/api/http/naming_test.go index a336d0b..1f2c7d2 100644 --- a/internal/api/http/naming_test.go +++ b/internal/api/http/naming_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "slices" + "strings" "testing" "github.com/uptrace/bun" @@ -28,7 +29,7 @@ func (c *askedClaude) Name(_ context.Context, key string, r anthropic.Request) ( if c.err != nil { return anthropic.Result{}, c.err } - return anthropic.Result{Name: "Riff senza sosta", Description: "Hard rock che non hai ancora ascoltato."}, nil + return anthropic.Result{Name: "Riff senza sosta", Description: "Hard rock che non hai ancora ascoltato.", Usage: anthropic.Usage{Input: 1000, Output: 500}}, nil } const claudeKey = "sk-ant-api03-0123456789abcdefghijklmnopqrstuv" @@ -49,7 +50,9 @@ func newNamingAPI(t *testing.T, claude *askedClaude, on bool) *mixAPI { } } d.Providers = prov - d.Naming = naming.NewService(prov, claude, storage.NewNamingRepo(db), nil) + svc := naming.NewService(prov, claude, storage.NewNamingRepo(db), nil) + svc.SetLedger(storage.NewNamingRepo(db)) + d.Naming = svc }) } @@ -325,3 +328,76 @@ func TestTheClientCanAskWhetherNamesAreAutomatic(t *testing.T) { t.Errorf("no token: %d", r.Code) } } + +// ---- what it costs ---- + +func TestTheCostsAreForAdminsOnly(t *testing.T) { + m := newNamingAPI(t, &askedClaude{}, true) + _, bob := addUser(t, m.h, m.alice, "bob", false) + if r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage", "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("no token: %d", r.Code) + } + if r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage", bob, nil); r.Code != http.StatusForbidden { + t.Errorf("a user who is not an admin: %d", r.Code) + } + if r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage", m.alice, nil); r.Code != http.StatusOK { + t.Errorf("an admin: %d %v", r.Code, r.Body) + } +} + +func TestTheCostsCountWhatWasAskedWhatWasKeptAndWhatFailed(t *testing.T) { + c := &askedClaude{} + m := newNamingAPI(t, c, true) + empty := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage", m.alice, nil) + if empty.Body["days"] != float64(30) || empty.Body["estimate"] != true || empty.Body["model"] != anthropic.Model || len(empty.Body["daily"].([]any)) != 0 { + t.Fatalf("empty report = %v", empty.Body) + } + id := m.playlistWithSongs(t, "hard song", 6) + suggest := func() { + do(t, m.h, "POST", fmt.Sprintf("/api/v1/playlists/%d/suggest-name", id), m.alice, map[string]any{"lang": "it"}) + } + suggest() // asked: made + suggest() // the same facts: kept + c.err = providers.ErrRateLimited + do(t, m.h, "POST", "/api/v1/mixes/genre%3Ahard%20rock/suggest-name", m.alice, nil) // asked: failed + + r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage?days=7", m.alice, nil) + if r.Code != http.StatusOK || r.Body["days"] != float64(7) { + t.Fatalf("%d %v", r.Code, r.Body) + } + totals := r.Body["totals"].(map[string]any) + if totals["names_made"] != float64(1) || totals["names_kept"] != float64(1) || totals["failures"] != float64(1) || + totals["input_tokens"] != float64(1000) || totals["output_tokens"] != float64(500) { + t.Errorf("totals = %v", totals) + } + // 1000 tokens in and 500 out at $4 and $20 per million. + if cost := totals["cost_usd"].(float64); cost < 0.01399 || cost > 0.01401 { + t.Errorf("cost = %v", cost) + } + daily := r.Body["daily"].([]any) + if len(daily) != 1 || daily[0].(map[string]any)["names_made"] != float64(1) || daily[0].(map[string]any)["date"] == "" { + t.Errorf("daily = %v", daily) + } + if r.Body["unpriced"] != false { + t.Errorf("unpriced = %v", r.Body["unpriced"]) + } + // Nothing of what was sent is in the answer. + raw := fmt.Sprint(r.Body) + for _, never := range []string{"Kiss", "hard rock", "AC/DC"} { + if strings.Contains(raw, never) { + t.Errorf("the report has %q: %s", never, raw) + } + } +} + +func TestThePeriodOfTheCostsIsChecked(t *testing.T) { + m := newNamingAPI(t, &askedClaude{}, true) + for _, q := range []string{"?days=-1", "?days=366", "?days=x"} { + if r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage"+q, m.alice, nil); r.Code != http.StatusBadRequest { + t.Errorf("%s: %d %v", q, r.Code, r.Body) + } + } + if r := do(t, m.h, "GET", "/api/v1/providers/anthropic/usage?days=365", m.alice, nil); r.Code != http.StatusOK { + t.Errorf("a year: %d", r.Code) + } +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go index b3e5ed4..57d7edf 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -113,6 +113,7 @@ type namingService interface { FactsOf(ctx context.Context, kind string, tracks []catalog.Track, hint string) (naming.Facts, error) Suggest(ctx context.Context, f naming.Facts, lang string) (naming.Suggestion, error) Auto(ctx context.Context) bool + Report(ctx context.Context, days int) (naming.Report, error) } // smartService is what the handlers need from smart.Service. @@ -354,6 +355,7 @@ func NewRouter(d Deps) *gin.Engine { // Admins only. admin := protected.Group("", requireAdmin(d.Auth)) + admin.GET("/providers/anthropic/usage", nm.usage) admin.POST("/library/scan", lib.startScan) admin.POST("/library/covers", cj.start) admin.DELETE("/library/covers", cj.stop) diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 20d0e05..8ebe824 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -2653,6 +2653,34 @@ paths: '401': {$ref: '#/components/responses/Unauthorized'} '403': {$ref: '#/components/responses/AdminRequired'} + /providers/anthropic/usage: + get: + tags: [Providers] + operationId: getClaudeUsage + summary: What the Claude provider costs + description: | + Admin only. Every question that Sonda asked of Claude is counted: what it made, what was taken from the names + that were kept (which cost nothing), what failed, and the tokens that were billed, also for a failure that was + billed. From them it works out an **estimate** of the cost in dollars, with the price of the model: the invoice + of Anthropic is the truth. `unpriced` says that some calls were to a model whose price is not known and are not + in the cost. A day is a day in UTC and only the days that had calls are listed, the newest first. Nothing of what + was sent (no artist, no genre, no name) is kept, and records older than a year are removed. + security: [{bearerAuth: []}] + parameters: + - name: days + in: query + description: How many days back, from 1 to 365. 30 when absent or 0. + schema: {type: integer, minimum: 0, maximum: 365, default: 30} + responses: + '200': + description: The totals and the days. + content: + application/json: + schema: {$ref: '#/components/schemas/ClaudeUsage'} + '400': {$ref: '#/components/responses/InvalidInput'} + '401': {$ref: '#/components/responses/Unauthorized'} + '403': {$ref: '#/components/responses/AdminRequired'} + /providers/lidarr/options: get: tags: [Providers] @@ -3637,6 +3665,49 @@ components: name: {type: string} tracks: {type: integer, description: Playable songs that have the genre.} + ClaudeUsageTotals: + type: object + additionalProperties: false + required: [names_made, names_kept, failures, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd] + properties: + names_made: {type: integer, description: Names that Claude made.} + names_kept: {type: integer, description: "Names taken from the ones kept, which cost nothing."} + failures: {type: integer, description: Questions that gave no name.} + input_tokens: {type: integer, format: int64} + output_tokens: {type: integer, format: int64} + cache_read_tokens: {type: integer, format: int64} + cache_write_tokens: {type: integer, format: int64} + cost_usd: {type: number, description: "An estimate in dollars, worked out from the tokens and the price of the model."} + + ClaudeUsageDay: + type: object + additionalProperties: false + required: [date, names_made, names_kept, failures, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd] + properties: + date: {type: string, format: date, description: "The day, in UTC."} + names_made: {type: integer} + names_kept: {type: integer} + failures: {type: integer} + input_tokens: {type: integer, format: int64} + output_tokens: {type: integer, format: int64} + cache_read_tokens: {type: integer, format: int64} + cache_write_tokens: {type: integer, format: int64} + cost_usd: {type: number, description: "An estimate in dollars."} + + ClaudeUsage: + type: object + additionalProperties: false + required: [days, model, estimate, unpriced, totals, daily] + properties: + days: {type: integer} + model: {type: string, description: The model that is asked now.} + estimate: {type: boolean, description: "Always true: the cost is worked out from the tokens, it is not the invoice."} + unpriced: {type: boolean, description: "Some calls were to a model whose price is not known and are not in the cost."} + totals: {$ref: '#/components/schemas/ClaudeUsageTotals'} + daily: + type: array + items: {$ref: '#/components/schemas/ClaudeUsageDay'} + SuggestName: type: object additionalProperties: false diff --git a/internal/naming/naming.go b/internal/naming/naming.go index d8c5a93..7eeb964 100644 --- a/internal/naming/naming.go +++ b/internal/naming/naming.go @@ -11,6 +11,7 @@ import ( "context" "crypto/sha256" "encoding/hex" + "errors" "fmt" "slices" "sort" @@ -66,15 +67,75 @@ type Store interface { GenreCounts(ctx context.Context, trackIDs []int64) (map[string]int, error) } +// How a call to Claude ended, and why it failed. +const ( + OutcomeMade = "made" // Claude made a name + OutcomeKept = "kept" // the name was one that Claude had made before, taken from the kept ones: nothing was billed + OutcomeFailed = "failed" // Claude gave no name + + ReasonNoName = "no_name" // it declined, was cut short or did not answer with a name + ReasonKey = "key" + ReasonRate = "rate" + ReasonUnreachable = "unreachable" + ReasonOther = "other" +) + +// Call is one question to Claude, for the account of what it costs. It holds nothing of the facts that were sent: no +// artist, no genre, no name. +type Call struct { + At time.Time + Kind string // "playlist", "mix" or "smart" + Model string + Outcome string + Reason string // for a failure + Usage anthropic.Usage +} + +// Meter keeps the account of the calls. +type Meter interface { + Record(ctx context.Context, c Call) error +} + // Service makes the suggestions. type Service struct { settings Settings claude Claude store Store + meter Meter + ledger Ledger log *zap.Logger now func() time.Time } +// SetMeter makes the service record every question it asks Claude. Call it before the service is used. +func (s *Service) SetMeter(m Meter) { s.meter = m } + +// record keeps a call; the account never stops a name. +func (s *Service) record(ctx context.Context, c Call) { + if s.meter == nil { + return + } + c.At, c.Model = s.now(), anthropic.Model + if err := s.meter.Record(ctx, c); err != nil { + s.log.Warn("a call to Claude could not be recorded", zap.Error(err)) + } +} + +// reasonOf says why a call failed, in one word. +func reasonOf(err error) string { + switch { + case errors.Is(err, anthropic.ErrNoName): + return ReasonNoName + case errors.Is(err, providers.ErrKeyRefused): + return ReasonKey + case errors.Is(err, providers.ErrRateLimited): + return ReasonRate + case errors.Is(err, providers.ErrUnreachable): + return ReasonUnreachable + } + return ReasonOther +} + // NewService builds a Service. log may be nil. func NewService(settings Settings, claude Claude, store Store, log *zap.Logger) *Service { if log == nil { @@ -139,6 +200,7 @@ func (s *Service) Suggest(ctx context.Context, f Facts, language string) (Sugges if got, ok, err := s.store.Get(ctx, key); err != nil { s.log.Warn("a kept name could not be read", zap.Error(err)) } else if ok { + s.record(ctx, Call{Kind: f.Kind, Outcome: OutcomeKept}) return got, nil } res, err := s.claude.Name(ctx, set.APIKey, anthropic.Request{ @@ -146,8 +208,10 @@ func (s *Service) Suggest(ctx context.Context, f Facts, language string) (Sugges }) if err != nil { s.log.Warn("Claude gave no name", zap.String("provider", providers.Anthropic), zap.Error(err)) + s.record(ctx, Call{Kind: f.Kind, Outcome: OutcomeFailed, Reason: reasonOf(err), Usage: res.Usage}) return local(f, language), nil } + s.record(ctx, Call{Kind: f.Kind, Outcome: OutcomeMade, Usage: res.Usage}) out := Suggestion{Name: res.Name, Description: res.Description, Source: SourceClaude} if err := s.store.Put(ctx, key, out, s.now()); err != nil { s.log.Warn("a name could not be kept", zap.Error(err)) diff --git a/internal/naming/naming_test.go b/internal/naming/naming_test.go index 09f950a..92b65e2 100644 --- a/internal/naming/naming_test.go +++ b/internal/naming/naming_test.go @@ -72,6 +72,16 @@ func (f *fakeStore) GenreCounts(_ context.Context, ids []int64) (map[string]int, return out, nil } +type fakeMeter struct { + calls []Call + err error +} + +func (f *fakeMeter) Record(_ context.Context, c Call) error { + f.calls = append(f.calls, c) + return f.err +} + func newService(set fakeSettings, c *fakeClaude, st *fakeStore) *Service { s := NewService(set, c, st, nil) s.now = func() time.Time { return time.Unix(1_700_000_000, 0) } @@ -271,3 +281,179 @@ func TestFactsOfNoSongs(t *testing.T) { t.Errorf("%+v, %v", f, err) } } + +// ---- what is asked of Claude is recorded ---- + +func metered(set fakeSettings, c *fakeClaude) (*Service, *fakeMeter) { + m := &fakeMeter{} + s := newService(set, c, newStore()) + s.SetMeter(m) + return s, m +} + +func TestEveryCallToClaudeIsRecordedWithItsTokens(t *testing.T) { + c := &fakeClaude{res: anthropic.Result{Name: "N", Usage: anthropic.Usage{Input: 120, Output: 45}}} + s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c) + if _, err := s.Suggest(context.Background(), rock, "en"); err != nil { + t.Fatal(err) + } + if len(m.calls) != 1 { + t.Fatalf("calls recorded = %+v", m.calls) + } + got := m.calls[0] + if got.Outcome != OutcomeMade || got.Kind != "mix" || got.Model != anthropic.Model || got.Usage != (anthropic.Usage{Input: 120, Output: 45}) || got.Reason != "" { + t.Errorf("recorded %+v", got) + } + if !got.At.Equal(time.Unix(1_700_000_000, 0)) { + t.Errorf("at = %v", got.At) + } +} + +func TestANameTakenFromTheKeptOnesIsRecordedAsSavedAndCostsNothing(t *testing.T) { + c := &fakeClaude{res: anthropic.Result{Name: "N", Usage: anthropic.Usage{Input: 100, Output: 40}}} + s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c) + _, _ = s.Suggest(context.Background(), rock, "en") + _, _ = s.Suggest(context.Background(), rock, "en") + if len(m.calls) != 2 || m.calls[1].Outcome != OutcomeKept || m.calls[1].Usage != (anthropic.Usage{}) { + t.Errorf("recorded %+v", m.calls) + } +} + +func TestAFailureIsRecordedWithItsReasonAndTheTokensThatWereBilled(t *testing.T) { + for name, tc := range map[string]struct { + err error + reason string + usage anthropic.Usage + }{ + "no name, but billed": {anthropic.ErrNoName, ReasonNoName, anthropic.Usage{Input: 90, Output: 300}}, + "key refused": {providers.ErrKeyRefused, ReasonKey, anthropic.Usage{}}, + "rate limited": {providers.ErrRateLimited, ReasonRate, anthropic.Usage{}}, + "unreachable": {providers.ErrUnreachable, ReasonUnreachable, anthropic.Usage{}}, + "anything else": {context.DeadlineExceeded, ReasonOther, anthropic.Usage{}}, + } { + t.Run(name, func(t *testing.T) { + c := &fakeClaude{res: anthropic.Result{Usage: tc.usage}, err: tc.err} + s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c) + if got, err := s.Suggest(context.Background(), rock, "en"); err != nil || got.Source != SourceLocal { + t.Fatalf("%+v, %v", got, err) + } + if len(m.calls) != 1 || m.calls[0].Outcome != OutcomeFailed || m.calls[0].Reason != tc.reason || m.calls[0].Usage != tc.usage { + t.Errorf("recorded %+v, want a failure for %q with %+v", m.calls, tc.reason, tc.usage) + } + }) + } +} + +func TestNothingIsRecordedWhenClaudeIsNotAsked(t *testing.T) { + s, m := metered(fakeSettings{on: false}, &fakeClaude{}) + _, _ = s.Suggest(context.Background(), rock, "en") + _, _ = s.Suggest(context.Background(), Facts{Kind: "playlist"}, "en") // nothing to say, nothing asked + if len(m.calls) != 0 { + t.Errorf("recorded %+v", m.calls) + } +} + +func TestAMeterThatFailsDoesNotStopTheName(t *testing.T) { + c := &fakeClaude{res: anthropic.Result{Name: "N"}} + s, m := metered(fakeSettings{on: true, key: "sk-ant-key"}, c) + m.err = errors.New("db down") + got, err := s.Suggest(context.Background(), rock, "en") + if err != nil || got.Name != "N" || got.Source != SourceClaude { + t.Errorf("%+v, %v", got, err) + } +} + +// ---- the report of what it costs ---- + +func TestTheReportSumsTheDaysAndPricesTheTokens(t *testing.T) { + d := func(day int) time.Time { return time.Date(2026, 11, day, 0, 0, 0, 0, time.UTC) } + rows := []DayRow{ + {Day: d(12), Model: anthropic.Model, Made: 3, Kept: 5, Failed: 1, Usage: anthropic.Usage{Input: 3000, Output: 1500}}, + {Day: d(10), Model: anthropic.Model, Made: 1, Usage: anthropic.Usage{Input: 1000, Output: 500}}, + } + r := Summarize(rows, 30, anthropic.Model) + if r.Days != 30 || r.Model != anthropic.Model || !r.Estimate { + t.Errorf("report = %+v", r) + } + if r.Totals.Made != 4 || r.Totals.Kept != 5 || r.Totals.Failed != 1 || r.Totals.Usage.Input != 4000 || r.Totals.Usage.Output != 2000 { + t.Errorf("totals = %+v", r.Totals) + } + // 4000 tokens in and 2000 out at $4 and $20 per million: $0.016 + $0.040. + if r.Totals.CostUSD < 0.05599 || r.Totals.CostUSD > 0.05601 || r.Unpriced { + t.Errorf("cost = %v unpriced = %v", r.Totals.CostUSD, r.Unpriced) + } + if len(r.Daily) != 2 || r.Daily[0].Date != "2026-11-12" || r.Daily[1].Date != "2026-11-10" || r.Daily[0].Made != 3 { + t.Errorf("daily = %+v: the newest day first", r.Daily) + } + if got := r.Daily[1].CostUSD; got < 0.01399 || got > 0.01401 { + t.Errorf("a day's cost = %v", got) + } +} + +func TestTwoModelsOnTheSameDayAreOneDayAndAModelWithNoPriceIsSaidSo(t *testing.T) { + day := time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC) + r := Summarize([]DayRow{ + {Day: day, Model: anthropic.Model, Made: 1, Usage: anthropic.Usage{Input: 1000, Output: 500}}, + {Day: day, Model: "claude-future", Made: 2, Usage: anthropic.Usage{Input: 9000, Output: 9000}}, + }, 7, anthropic.Model) + if len(r.Daily) != 1 || r.Daily[0].Made != 3 || r.Daily[0].Usage.Input != 10000 { + t.Fatalf("daily = %+v", r.Daily) + } + if !r.Unpriced || r.Totals.CostUSD < 0.01399 || r.Totals.CostUSD > 0.01401 { + t.Errorf("cost = %v unpriced = %v: only the model that has a price is counted, and the report says there is another", r.Totals.CostUSD, r.Unpriced) + } +} + +func TestAReportWithNoCallsIsEmptyNotMissing(t *testing.T) { + r := Summarize(nil, 30, anthropic.Model) + if r.Daily == nil || len(r.Daily) != 0 || r.Totals.CostUSD != 0 || r.Unpriced { + t.Errorf("report = %+v", r) + } +} + +type fakeLedger struct { + fakeMeter + rows []DayRow + since time.Time +} + +func (f *fakeLedger) Since(_ context.Context, since time.Time) ([]DayRow, error) { + f.since = since + return f.rows, nil +} + +func TestTheServiceReadsTheLedgerFromTheStartOfThePeriod(t *testing.T) { + l := &fakeLedger{rows: []DayRow{{Day: time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC), Model: anthropic.Model, Made: 2}}} + s := newService(fakeSettings{}, &fakeClaude{}, newStore()) + s.SetLedger(l) + r, err := s.Report(context.Background(), 30) + if err != nil || r.Totals.Made != 2 { + t.Fatalf("%+v, %v", r, err) + } + if want := s.now().Add(-30 * 24 * time.Hour); !l.since.Equal(want) { + t.Errorf("since = %v, want %v", l.since, want) + } + // The ledger is also what records. + s.SetLedger(l) + _, _ = newServiceWithLedger(l).Suggest(context.Background(), rock, "en") + if _, err := s.Report(context.Background(), 0); !errors.Is(err, ErrInvalidDays) { + t.Errorf("0 days: %v", err) + } + if _, err := s.Report(context.Background(), MaxReportDays+1); !errors.Is(err, ErrInvalidDays) { + t.Errorf("too many days: %v", err) + } +} + +func newServiceWithLedger(l *fakeLedger) *Service { + s := newService(fakeSettings{on: true, key: "sk-ant-key"}, &fakeClaude{res: anthropic.Result{Name: "N"}}, newStore()) + s.SetLedger(l) + return s +} + +func TestAServiceWithNoLedgerGivesAnEmptyReport(t *testing.T) { + s := newService(fakeSettings{}, &fakeClaude{}, newStore()) + r, err := s.Report(context.Background(), 30) + if err != nil || len(r.Daily) != 0 { + t.Errorf("%+v, %v", r, err) + } +} diff --git a/internal/naming/report.go b/internal/naming/report.go new file mode 100644 index 0000000..718fdb5 --- /dev/null +++ b/internal/naming/report.go @@ -0,0 +1,117 @@ +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 +} diff --git a/internal/providers/anthropic/anthropic.go b/internal/providers/anthropic/anthropic.go index 732e718..5f84639 100644 --- a/internal/providers/anthropic/anthropic.go +++ b/internal/providers/anthropic/anthropic.go @@ -57,10 +57,41 @@ type Request struct { Language string } -// Result is a name and a line of description. +// Usage is what a call used, in tokens, which is what it is billed by. Cache tokens are the ones read from and written +// to the cache of Anthropic; Sonda sends nothing it asks to cache, so they are zero unless that changes. +type Usage struct { + Input, Output, CacheRead, CacheWrite int64 +} + +// Result is a name and a line of description. Usage is set also when the call gave no name (ErrNoName), because +// that answer was billed all the same. type Result struct { Name string Description string + Usage Usage +} + +// Price is what a model costs, in dollars for each million tokens. +type Price struct { + Input, Output, CacheRead, CacheWrite float64 +} + +// prices are the published prices of the models that Sonda asks. They are an estimate that the admin page says is one: +// the invoice of Anthropic is the truth. A cache write is 1.25 times the price of an input token, as published. +var prices = map[string]Price{ + "claude-opus-5-5": {Input: 4, Output: 20, CacheRead: 0.20, CacheWrite: 5}, +} + +// PriceOf returns the price of a model, ok=false for one that is not known. +func PriceOf(model string) (Price, bool) { + p, ok := prices[model] + return p, ok +} + +// Cost is what the usage costs, in dollars. +func (p Price) Cost(u Usage) float64 { + const million = 1_000_000 + return (float64(u.Input)*p.Input + float64(u.Output)*p.Output + float64(u.CacheRead)*p.CacheRead + float64(u.CacheWrite)*p.CacheWrite) / million } // Client calls the Claude API. It is safe for concurrent use. @@ -207,8 +238,12 @@ func (c *Client) Name(ctx context.Context, key string, req Request) (Result, err if err != nil { return Result{}, failure(err) } + used := Usage{ + Input: resp.Usage.InputTokens, Output: resp.Usage.OutputTokens, + CacheRead: resp.Usage.CacheReadInputTokens, CacheWrite: resp.Usage.CacheCreationInputTokens, + } if resp.StopReason != sdk.StopReasonEndTurn { - return Result{}, fmt.Errorf("%w: stopped with %s", ErrNoName, strconv.Quote(string(resp.StopReason))) + return Result{Usage: used}, fmt.Errorf("%w: stopped with %s", ErrNoName, strconv.Quote(string(resp.StopReason))) } var text string for _, block := range resp.Content { @@ -221,13 +256,13 @@ func (c *Client) Name(ctx context.Context, key string, req Request) (Result, err Description string `json:"description"` } if err := json.Unmarshal([]byte(text), &out); err != nil { - return Result{}, fmt.Errorf("%w: not JSON", ErrNoName) + return Result{Usage: used}, fmt.Errorf("%w: not JSON", ErrNoName) } name := truncate(strings.Trim(clean(out.Name), " \"'“”‘’«»"), MaxNameLen) if name == "" { - return Result{}, fmt.Errorf("%w: empty", ErrNoName) + return Result{Usage: used}, fmt.Errorf("%w: empty", ErrNoName) } - return Result{Name: name, Description: truncate(clean(out.Description), MaxDescriptionLen)}, nil + return Result{Name: name, Description: truncate(clean(out.Description), MaxDescriptionLen), Usage: used}, nil } // clean takes out control characters and the blanks around and inside, so that a name is one line. diff --git a/internal/providers/anthropic/anthropic_test.go b/internal/providers/anthropic/anthropic_test.go index e025b01..ce615dc 100644 --- a/internal/providers/anthropic/anthropic_test.go +++ b/internal/providers/anthropic/anthropic_test.go @@ -51,7 +51,7 @@ func message(stop, text string) string { b, _ := json.Marshal(map[string]any{ "id": "msg_1", "type": "message", "role": "assistant", "model": Model, "stop_reason": stop, "content": []any{map[string]any{"type": "text", "text": text}}, - "usage": map[string]any{"input_tokens": 10, "output_tokens": 10}, + "usage": map[string]any{"input_tokens": 120, "output_tokens": 45, "cache_read_input_tokens": 7, "cache_creation_input_tokens": 3}, }) return string(b) } @@ -213,3 +213,39 @@ func TestNothingIsTakenFromTheEnvironmentOfTheServer(t *testing.T) { t.Error("a header of the environment was sent") } } + +func TestTheTokensThatWereBilledComeBackWithTheNameAndAlsoWithoutOne(t *testing.T) { + want := Usage{Input: 120, Output: 45, CacheRead: 7, CacheWrite: 3} + ok := newServer(t, 200, message("end_turn", `{"name":"x","description":"y"}`)) + got, err := ok.client().Name(context.Background(), "sk-ant-x", req) + if err != nil || got.Usage != want { + t.Errorf("a name: usage = %+v, err = %v, want %+v", got.Usage, err, want) + } + // A refusal and an answer that is no name still cost what they cost. + for name, reply := range map[string]string{"refusal": message("refusal", ""), "cut": message("max_tokens", `{"name":"x"`), "not json": message("end_turn", "nope")} { + s := newServer(t, 200, reply) + res, err := s.client().Name(context.Background(), "sk-ant-x", req) + if !errors.Is(err, ErrNoName) || res.Usage != want { + t.Errorf("%s: usage = %+v, err = %v, want %+v with ErrNoName", name, res.Usage, err, want) + } + } + // A call that did not get through has nothing to bill. + bad := newServer(t, 500, `{"type":"error","error":{"type":"x","message":"y"}}`) + if res, _ := bad.client().Name(context.Background(), "sk-ant-x", req); res.Usage != (Usage{}) { + t.Errorf("a failed call: usage = %+v", res.Usage) + } +} + +func TestEveryModelThatIsUsedHasAPrice(t *testing.T) { + p, ok := PriceOf(Model) + if !ok || p.Input <= 0 || p.Output <= 0 { + t.Fatalf("price of %s = %+v, %v", Model, p, ok) + } + // 1000 tokens read and 500 written at $4 and $20 per million. + if got := p.Cost(Usage{Input: 1000, Output: 500}); got < 0.01399 || got > 0.01401 { + t.Errorf("cost = %v, want 0.014", got) + } + if _, ok := PriceOf("claude-nobody"); ok { + t.Error("a model that is not known has a price") + } +} diff --git a/internal/storage/naming.go b/internal/storage/naming.go index 2709ea1..6584a4a 100644 --- a/internal/storage/naming.go +++ b/internal/storage/naming.go @@ -67,3 +67,49 @@ func (r *NamingRepo) GenreCounts(ctx context.Context, trackIDs []int64) (map[str } return out, rows.Err() } + +// keepUsage is how long the account of the calls is kept. +const keepUsage = 365 * 24 * time.Hour + +// Record keeps the account of one call to Claude, and forgets the ones that are older than a year. +func (r *NamingRepo) Record(ctx context.Context, c naming.Call) error { + _, err := r.db.ExecContext(ctx, + `INSERT INTO claude_usage (at, kind, model, outcome, reason, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, + c.At.Unix(), c.Kind, c.Model, c.Outcome, c.Reason, c.Usage.Input, c.Usage.Output, c.Usage.CacheRead, c.Usage.CacheWrite) + if err != nil { + return fmt.Errorf("record a call: %w", err) + } + if _, err := r.db.ExecContext(ctx, "DELETE FROM claude_usage WHERE at < ?", c.At.Add(-keepUsage).Unix()); err != nil { + return fmt.Errorf("forget old calls: %w", err) + } + return nil +} + +// Since returns what happened from a moment on, a row for each day (UTC) and model, the newest day first. +func (r *NamingRepo) Since(ctx context.Context, since time.Time) ([]naming.DayRow, error) { + rows, err := r.db.QueryContext(ctx, + `SELECT at / 86400, model, + SUM(CASE WHEN outcome = 'made' THEN 1 ELSE 0 END), + SUM(CASE WHEN outcome = 'kept' THEN 1 ELSE 0 END), + SUM(CASE WHEN outcome = 'failed' THEN 1 ELSE 0 END), + SUM(input_tokens), SUM(output_tokens), SUM(cache_read_tokens), SUM(cache_write_tokens) + FROM claude_usage WHERE at >= ? GROUP BY 1, 2 ORDER BY 1 DESC, 2`, since.Unix()) + if err != nil { + return nil, fmt.Errorf("read what Claude cost: %w", err) + } + defer rows.Close() + out := []naming.DayRow{} + for rows.Next() { + var ( + d naming.DayRow + day int64 + ) + if err := rows.Scan(&day, &d.Model, &d.Made, &d.Kept, &d.Failed, &d.Usage.Input, &d.Usage.Output, &d.Usage.CacheRead, &d.Usage.CacheWrite); err != nil { + return nil, err + } + d.Day = time.Unix(day*86400, 0).UTC() + out = append(out, d) + } + return out, rows.Err() +} diff --git a/internal/storage/naming_test.go b/internal/storage/naming_test.go index 81528bc..fb9e1ac 100644 --- a/internal/storage/naming_test.go +++ b/internal/storage/naming_test.go @@ -8,6 +8,7 @@ import ( "github.com/uptrace/bun" "codeberg.org/squidd/sonda/internal/naming" + "codeberg.org/squidd/sonda/internal/providers/anthropic" ) func TestNamingRepoKeepsAndReadsANameAndKeepsTheFirstOneWhenAskedTwice(t *testing.T) { @@ -52,3 +53,74 @@ func TestNamingRepoCountsTheGenresOfSongs(t *testing.T) { } }) } + +func TestUsageRepoSumsEachDayAndModelAndForgetsWhatIsOlderThanAYear(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + ctx := context.Background() + r := NewNamingRepo(db) + day := func(n int) time.Time { return time.Date(2026, 11, n, 15, 30, 0, 0, time.UTC) } + rec := func(at time.Time, kind, model, outcome, reason string, in, out int64) { + t.Helper() + err := r.Record(ctx, naming.Call{At: at, Kind: kind, Model: model, Outcome: outcome, Reason: reason, Usage: anthropic.Usage{Input: in, Output: out, CacheRead: 1, CacheWrite: 2}}) + if err != nil { + t.Fatal(err) + } + } + rec(day(12), "mix", "claude-a", naming.OutcomeMade, "", 100, 40) + rec(day(12), "smart", "claude-a", naming.OutcomeMade, "", 200, 60) + rec(day(12), "mix", "claude-a", naming.OutcomeKept, "", 0, 0) + rec(day(12), "mix", "claude-a", naming.OutcomeFailed, naming.ReasonNoName, 50, 300) + rec(day(12), "mix", "claude-b", naming.OutcomeMade, "", 10, 10) + rec(day(10), "playlist", "claude-a", naming.OutcomeFailed, naming.ReasonKey, 0, 0) + rec(day(1), "mix", "claude-a", naming.OutcomeMade, "", 7, 7) + + rows, err := r.Since(ctx, day(5)) + if err != nil { + t.Fatal(err) + } + if len(rows) != 3 { + t.Fatalf("rows = %+v, want 3: the 12th with two models and the 10th; the 1st is before", rows) + } + first := rows[0] + if !first.Day.Equal(time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC)) || first.Model != "claude-a" || first.Made != 2 || first.Kept != 1 || first.Failed != 1 || + first.Usage != (anthropic.Usage{Input: 350, Output: 400, CacheRead: 4, CacheWrite: 8}) { + t.Errorf("first = %+v", first) + } + if rows[1].Model != "claude-b" || rows[1].Made != 1 || rows[1].Usage.Input != 10 { + t.Errorf("second = %+v", rows[1]) + } + if !rows[2].Day.Equal(time.Date(2026, 11, 10, 0, 0, 0, 0, time.UTC)) || rows[2].Failed != 1 { + t.Errorf("third = %+v", rows[2]) + } + if none, _ := r.Since(ctx, day(30)); len(none) != 0 { + t.Errorf("a period with no calls = %+v", none) + } + + // A call more than a year after the oldest one makes the oldest go. + if err := r.Record(ctx, naming.Call{At: day(1).Add(366 * 24 * time.Hour), Kind: "mix", Model: "claude-a", Outcome: naming.OutcomeMade}); err != nil { + t.Fatal(err) + } + var n int + if err := db.QueryRow("SELECT COUNT(*) FROM claude_usage WHERE at < ?", day(1).Unix()+1).Scan(&n); err != nil || n != 0 { + t.Errorf("%d records older than a year are left, %v", n, err) + } + if err := db.QueryRow("SELECT COUNT(*) FROM claude_usage").Scan(&n); err != nil || n == 0 { + t.Errorf("the recent ones went too: %d, %v", n, err) + } + }) +} + +func TestUsageRepoKeepsNothingOfTheFacts(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + cols, err := db.Query("SELECT * FROM claude_usage LIMIT 0") + if err != nil { + t.Fatal(err) + } + defer cols.Close() + names, _ := cols.Columns() + want := []string{"id", "at", "kind", "model", "outcome", "reason", "input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens"} + if len(names) != len(want) { + t.Errorf("columns = %v, want %v: nothing but the account", names, want) + } + }) +} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go index abe86ba..2a6ead2 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/storage_test.go @@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) { if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil { t.Fatal(err) } - if applied != 21 { - t.Errorf("applied migrations = %d, want 21", applied) + if applied != 22 { + t.Errorf("applied migrations = %d, want 22", applied) } }) } diff --git a/migrations/postgres/0022_claude_usage.sql b/migrations/postgres/0022_claude_usage.sql new file mode 100644 index 0000000..b6e5d23 --- /dev/null +++ b/migrations/postgres/0022_claude_usage.sql @@ -0,0 +1,17 @@ +-- What the calls to Claude cost: one row for each question asked of the provider. PostgreSQL flavour of +-- sqlite/0022_claude_usage.sql. + +CREATE TABLE claude_usage ( + id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + at BIGINT NOT NULL, -- unix seconds + kind TEXT NOT NULL, + model TEXT NOT NULL, + outcome TEXT NOT NULL, + reason TEXT NOT NULL DEFAULT '', + input_tokens BIGINT NOT NULL DEFAULT 0, + output_tokens BIGINT NOT NULL DEFAULT 0, + cache_read_tokens BIGINT NOT NULL DEFAULT 0, + cache_write_tokens BIGINT NOT NULL DEFAULT 0 +); + +CREATE INDEX idx_claude_usage_at ON claude_usage(at); diff --git a/migrations/sqlite/0022_claude_usage.sql b/migrations/sqlite/0022_claude_usage.sql new file mode 100644 index 0000000..7eb4036 --- /dev/null +++ b/migrations/sqlite/0022_claude_usage.sql @@ -0,0 +1,17 @@ +-- What the calls to Claude cost: one row for each question asked of the provider, with the tokens that were billed and how +-- it ended. Nothing of the facts that were sent is kept: no artist, no genre, no name. Rows older than a year are removed. + +CREATE TABLE claude_usage ( + id INTEGER PRIMARY KEY, + at INTEGER NOT NULL, -- unix seconds + kind TEXT NOT NULL, -- playlist, mix or smart + model TEXT NOT NULL, + outcome TEXT NOT NULL, -- made, kept or failed + reason TEXT NOT NULL DEFAULT '',-- for a failure + input_tokens INTEGER NOT NULL DEFAULT 0, + output_tokens INTEGER NOT NULL DEFAULT 0, + cache_read_tokens INTEGER NOT NULL DEFAULT 0, + cache_write_tokens INTEGER NOT NULL DEFAULT 0 +); + +CREATE INDEX idx_claude_usage_at ON claude_usage(at); diff --git a/web/src/api/adminCosts.ts b/web/src/api/adminCosts.ts new file mode 100644 index 0000000..67a6c0f --- /dev/null +++ b/web/src/api/adminCosts.ts @@ -0,0 +1,19 @@ +import { keepPreviousData, useQuery } from '@tanstack/react-query' +import { api } from './client' +import type { components } from './schema' + +export type ClaudeUsage = components['schemas']['ClaudeUsage'] +export type ClaudeUsageDay = components['schemas']['ClaudeUsageDay'] + +/** The periods the admin can look at, in days. */ +export const PERIODS = [7, 30, 90] as const +export type Period = (typeof PERIODS)[number] + +/** What the calls to Claude cost over the last days. The numbers on screen stay until the next period arrives. */ +export function useClaudeUsage(days: Period) { + return useQuery({ + queryKey: ['claude-usage', days], + queryFn: () => api.get('/providers/anthropic/usage', { days }), + placeholderData: keepPreviousData, + }) +} diff --git a/web/src/api/schema.d.ts b/web/src/api/schema.d.ts index c07ec00..638b433 100644 --- a/web/src/api/schema.d.ts +++ b/web/src/api/schema.d.ts @@ -1793,6 +1793,31 @@ export interface paths { patch?: never; trace?: never; }; + "/providers/anthropic/usage": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * What the Claude provider costs + * @description Admin only. Every question that Sonda asked of Claude is counted: what it made, what was taken from the names + * that were kept (which cost nothing), what failed, and the tokens that were billed, also for a failure that was + * billed. From them it works out an **estimate** of the cost in dollars, with the price of the model: the invoice + * of Anthropic is the truth. `unpriced` says that some calls were to a model whose price is not known and are not + * in the cost. A day is a day in UTC and only the days that had calls are listed, the newest first. Nothing of what + * was sent (no artist, no genre, no name) is kept, and records older than a year are removed. + */ + get: operations["getClaudeUsage"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; "/providers/lidarr/options": { parameters: { query?: never; @@ -2464,6 +2489,55 @@ export interface components { tracks: number; }[]; }; + ClaudeUsageTotals: { + /** @description Names that Claude made. */ + names_made: number; + /** @description Names taken from the ones kept, which cost nothing. */ + names_kept: number; + /** @description Questions that gave no name. */ + failures: number; + /** Format: int64 */ + input_tokens: number; + /** Format: int64 */ + output_tokens: number; + /** Format: int64 */ + cache_read_tokens: number; + /** Format: int64 */ + cache_write_tokens: number; + /** @description An estimate in dollars, worked out from the tokens and the price of the model. */ + cost_usd: number; + }; + ClaudeUsageDay: { + /** + * Format: date + * @description The day, in UTC. + */ + date: string; + names_made: number; + names_kept: number; + failures: number; + /** Format: int64 */ + input_tokens: number; + /** Format: int64 */ + output_tokens: number; + /** Format: int64 */ + cache_read_tokens: number; + /** Format: int64 */ + cache_write_tokens: number; + /** @description An estimate in dollars. */ + cost_usd: number; + }; + ClaudeUsage: { + days: number; + /** @description The model that is asked now. */ + model: string; + /** @description Always true: the cost is worked out from the tokens, it is not the invoice. */ + estimate: boolean; + /** @description Some calls were to a model whose price is not known and are not in the cost. */ + unpriced: boolean; + totals: components["schemas"]["ClaudeUsageTotals"]; + daily: components["schemas"]["ClaudeUsageDay"][]; + }; SuggestName: { /** @description The language of the name: `en` or `it`. Absent or another: English. */ lang?: string; @@ -6116,6 +6190,32 @@ export interface operations { 403: components["responses"]["AdminRequired"]; }; }; + getClaudeUsage: { + parameters: { + query?: { + /** @description How many days back, from 1 to 365. 30 when absent or 0. */ + days?: number; + }; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description The totals and the days. */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ClaudeUsage"]; + }; + }; + 400: components["responses"]["InvalidInput"]; + 401: components["responses"]["Unauthorized"]; + 403: components["responses"]["AdminRequired"]; + }; + }; getLidarrOptions: { parameters: { query?: never; diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 31a9952..40d312e 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -75,7 +75,27 @@ "sectionLibraries": "Libraries", "sectionUsers": "Users", "sectionProviders": "Providers", + "sectionCosts": "Costs", "sectionJobs": "Jobs", + "costs": { + "intro": "Every question that Sonda asks of Claude is counted here: the names it made, the ones taken from the names it kept (which cost nothing), the ones that failed, and the tokens that were billed. Nothing of what was sent is kept.", + "period": "Period", + "days_one": "{{count}} day", + "days_other": "{{count}} days", + "last_one": "Last {{count}} day", + "last_other": "Last {{count}} days", + "made": "Names made", + "kept": "Names taken from the kept ones", + "failed": "Failures", + "tokensIn": "Tokens read", + "tokensOut": "Tokens written", + "tokens": "Tokens", + "cost": "Estimated cost", + "day": "Day (UTC)", + "estimate": "The cost is an estimate, worked out from the tokens and the price of {{model}}. The invoice of Anthropic is the truth.", + "unpriced": "Some calls were to a model whose price is not known: they are not in the cost.", + "nothing": "Nothing was asked of Claude in this period." + }, "jobs": { "intro": "These jobs go through the whole library in the background, one item after the other. Each card says what it sends and to whom.", "needs": "Needs {{names}} turned on: use the Providers section.", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index 9d0a9b7..9854c44 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -75,7 +75,27 @@ "sectionLibraries": "Librerie", "sectionUsers": "Utenti", "sectionProviders": "Provider", + "sectionCosts": "Costi", "sectionJobs": "Lavori", + "costs": { + "intro": "Qui si contano tutte le domande che Sonda fa a Claude: i nomi fatti, quelli presi dai nomi già salvati (che non costano niente), quelli falliti e i token fatturati. Di quello che è stato inviato non si conserva niente.", + "period": "Periodo", + "days_one": "{{count}} giorno", + "days_other": "{{count}} giorni", + "last_one": "Ultimo giorno", + "last_other": "Ultimi {{count}} giorni", + "made": "Nomi fatti", + "kept": "Nomi presi da quelli salvati", + "failed": "Fallimenti", + "tokensIn": "Token letti", + "tokensOut": "Token scritti", + "tokens": "Token", + "cost": "Costo stimato", + "day": "Giorno (UTC)", + "estimate": "Il costo è una stima, calcolata dai token e dal prezzo di {{model}}. La fattura di Anthropic è quella che conta.", + "unpriced": "Alcune chiamate erano a un modello di cui non si conosce il prezzo: non sono nel costo.", + "nothing": "In questo periodo non è stato chiesto niente a Claude." + }, "jobs": { "intro": "Questi lavori passano in rassegna tutta la libreria in background, un elemento alla volta. Ogni scheda dice cosa manda e a chi.", "needs": "Serve che {{names}} sia acceso: vai nella sezione Provider.", diff --git a/web/src/pages/admin/AdminLayout.tsx b/web/src/pages/admin/AdminLayout.tsx index 0b87de9..03cdde3 100644 --- a/web/src/pages/admin/AdminLayout.tsx +++ b/web/src/pages/admin/AdminLayout.tsx @@ -10,6 +10,7 @@ const SECTIONS = [ { to: '/admin/users', key: 'admin.sectionUsers' }, { to: '/admin/providers', key: 'admin.sectionProviders' }, { to: '/admin/jobs', key: 'admin.sectionJobs' }, + { to: '/admin/costs', key: 'admin.sectionCosts' }, ] /** diff --git a/web/src/pages/admin/CostsPage.test.tsx b/web/src/pages/admin/CostsPage.test.tsx new file mode 100644 index 0000000..bdd75e0 --- /dev/null +++ b/web/src/pages/admin/CostsPage.test.tsx @@ -0,0 +1,105 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen, waitFor, within } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { beforeEach, expect, test, vi } from 'vitest' +import { sessionStore } from '@/auth/session' +import { initI18n } from '@/i18n' +import { apiError, mockFetch } from '@/test/mockFetch' +import CostsPage from './CostsPage' + +const totals = (over: object = {}) => ({ + names_made: 0, names_kept: 0, failures: 0, input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0, cost_usd: 0, ...over, +}) + +function usage(over: object = {}) { + return { + days: 30, + model: 'claude-opus-5-5', + estimate: true, + unpriced: false, + totals: totals({ names_made: 4, names_kept: 5, failures: 1, input_tokens: 4000, output_tokens: 2000, cost_usd: 0.056 }), + daily: [ + { date: '2026-11-12', ...totals({ names_made: 3, names_kept: 5, failures: 1, input_tokens: 3000, output_tokens: 1500, cost_usd: 0.042 }) }, + { date: '2026-11-10', ...totals({ names_made: 1, input_tokens: 1000, output_tokens: 500, cost_usd: 0.014 }) }, + ], + ...over, + } +} + +function renderPage() { + const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) + return render( + + + , + ) +} + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) +}) + +test('the page shows the totals of the last 30 days and says that the cost is an estimate', async () => { + const { calls } = mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => usage() }) + renderPage() + const summary = await screen.findByRole('region', { name: 'Last 30 days' }) + expect(within(summary).getByText('4')).toBeInTheDocument() // names made + expect(within(summary).getByText('Names made')).toBeInTheDocument() + expect(within(summary).getByText('Names taken from the kept ones')).toBeInTheDocument() + expect(within(summary).getByText('$0.056')).toBeInTheDocument() + expect(within(summary).getByText('4,000')).toBeInTheDocument() + expect(screen.getByText(/is an estimate/)).toBeInTheDocument() + expect(screen.getByText(/claude-opus-5-5/)).toBeInTheDocument() + expect(calls[0].url.searchParams.get('days')).toBe('30') +}) + +test('each day with calls has a row, the newest first', async () => { + mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => usage() }) + renderPage() + const table = await screen.findByRole('table') + const rows = within(table).getAllByRole('row').slice(1) + expect(rows).toHaveLength(2) + expect(rows[0]).toHaveTextContent('2026-11-12') + expect(rows[0]).toHaveTextContent('$0.042') + expect(rows[0]).toHaveTextContent('4,500') // tokens: 3000 in and 1500 out + expect(rows[1]).toHaveTextContent('2026-11-10') +}) + +test('the period can be changed, and the page asks again for it', async () => { + const { calls } = mockFetch({ 'GET /api/v1/providers/anthropic/usage': ({ url }) => usage({ days: Number(url.searchParams.get('days')) }) }) + renderPage() + await screen.findByRole('region', { name: 'Last 30 days' }) + await userEvent.click(screen.getByRole('radio', { name: '7 days' })) + expect(await screen.findByRole('region', { name: 'Last 7 days' })).toBeInTheDocument() + await userEvent.click(screen.getByRole('radio', { name: '90 days' })) + expect(await screen.findByRole('region', { name: 'Last 90 days' })).toBeInTheDocument() + expect(calls.map((c) => c.url.searchParams.get('days'))).toEqual(['30', '7', '90']) +}) + +test('with no call yet the page says so, and a failure to read says why', async () => { + mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => usage({ totals: totals(), daily: [] }) }) + renderPage() + expect(await screen.findByText('Nothing was asked of Claude in this period.')).toBeInTheDocument() + expect(screen.queryByRole('table')).not.toBeInTheDocument() +}) + +test('a calls to a model with no known price are said not to be in the cost', async () => { + mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => usage({ unpriced: true }) }) + renderPage() + expect(await screen.findByText(/price is not known/)).toBeInTheDocument() +}) + +test('an error is shown with a way to try again', async () => { + mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => apiError(500, 'unknown') }) + renderPage() + expect(await screen.findByRole('alert')).toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Try again' })).toBeInTheDocument() +}) + +test('very small costs keep their digits', async () => { + mockFetch({ 'GET /api/v1/providers/anthropic/usage': () => usage({ totals: totals({ names_made: 1, cost_usd: 0.0123 }), daily: [{ date: '2026-11-12', ...totals({ names_made: 1, cost_usd: 0.0123 }) }] }) }) + renderPage() + await waitFor(() => expect(screen.getAllByText('$0.0123').length).toBeGreaterThan(0)) +}) diff --git a/web/src/pages/admin/CostsPage.tsx b/web/src/pages/admin/CostsPage.tsx new file mode 100644 index 0000000..65ede06 --- /dev/null +++ b/web/src/pages/admin/CostsPage.tsx @@ -0,0 +1,88 @@ +import { useState } from 'react' +import { useTranslation } from 'react-i18next' +import { PERIODS, useClaudeUsage, type Period } from '@/api/adminCosts' +import ChoiceGroup from '@/components/ChoiceGroup' +import Section from '@/components/Section' +import { ErrorMessage, Loading } from '@/components/Status' + +/** What Claude costs: the totals of a period, and a row for each day that had calls. The cost is an estimate. */ +export default function CostsPage() { + const { t, i18n } = useTranslation() + const [days, setDays] = useState(30) + const query = useClaudeUsage(days) + // Dollars with the cents, and more digits when the amounts are as small as a name costs. + const money = new Intl.NumberFormat(i18n.language, { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 4 }) + const count = new Intl.NumberFormat(i18n.language) + + return ( +
+

{t('admin.costs.intro')}

+ setDays(Number(v) as Period)} + choices={PERIODS.map((p) => ({ value: String(p) as `${Period}`, label: t('admin.costs.days', { count: p }) }))} + /> + + {query.isError ? ( + query.refetch()} /> + ) : !query.data ? ( + + ) : ( + <> +
+

{t('admin.costs.last', { count: query.data.days })}

+
+ {[ + [t('admin.costs.made'), count.format(query.data.totals.names_made)], + [t('admin.costs.kept'), count.format(query.data.totals.names_kept)], + [t('admin.costs.failed'), count.format(query.data.totals.failures)], + [t('admin.costs.tokensIn'), count.format(query.data.totals.input_tokens)], + [t('admin.costs.tokensOut'), count.format(query.data.totals.output_tokens)], + [t('admin.costs.cost'), money.format(query.data.totals.cost_usd)], + ].map(([label, value]) => ( +
+
{label}
+
{value}
+
+ ))} +
+

{t('admin.costs.estimate', { model: query.data.model })}

+ {query.data.unpriced &&

{t('admin.costs.unpriced')}

} +
+ + {query.data.daily.length === 0 ? ( +

{t('admin.costs.nothing')}

+ ) : ( +
+ + + + + + + + + + + + + {query.data.daily.map((d) => ( + + + + + + + + + ))} + +
{t('admin.costs.day')}{t('admin.costs.made')}{t('admin.costs.kept')}{t('admin.costs.failed')}{t('admin.costs.tokens')}{t('admin.costs.cost')}
{d.date}{count.format(d.names_made)}{count.format(d.names_kept)}{count.format(d.failures)}{count.format(d.input_tokens + d.output_tokens + d.cache_read_tokens + d.cache_write_tokens)}{money.format(d.cost_usd)}
+
+ )} + + )} +
+ ) +} diff --git a/web/src/router.tsx b/web/src/router.tsx index 827cf7e..2b86df4 100644 --- a/web/src/router.tsx +++ b/web/src/router.tsx @@ -9,6 +9,7 @@ import HomePage from '@/pages/HomePage' import MixesPage from '@/pages/MixesPage' import MixPage from '@/pages/MixPage' import AdminLayout from '@/pages/admin/AdminLayout' +import CostsPage from '@/pages/admin/CostsPage' import JobsPage from '@/pages/admin/JobsPage' import LibrariesPage from '@/pages/admin/LibrariesPage' import ProvidersPage from '@/pages/admin/ProvidersPage' @@ -55,6 +56,7 @@ export const routes: RouteObject[] = [ { path: 'users', element: }, { path: 'providers', element: }, { path: 'jobs', element: }, + { path: 'costs', element: }, ], }, { path: '*', element: },