@yaks/spend 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +159 -2
- package/_dist/graph.d.ts +26 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +11 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/routes.d.ts +10 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/service.d.ts +41 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tally.d.ts +111 -0
- package/_dist/tally.d.ts.map +1 -0
- package/_dist/tools.d.ts +52 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/use.d.ts +31 -0
- package/_dist/use.d.ts.map +1 -0
- package/_dist/views.d.ts +27 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/window.d.ts +132 -0
- package/_dist/window.d.ts.map +1 -0
- package/_dist/words.d.ts +40 -0
- package/_dist/words.d.ts.map +1 -0
- package/budget.js +131 -0
- package/budget.js.map +1 -0
- package/budget.ts +128 -0
- package/day.js +336 -0
- package/day.js.map +1 -0
- package/day.ts +364 -0
- package/deno.json +1 -0
- package/graph.js +59 -0
- package/graph.js.map +1 -0
- package/graph.ts +61 -0
- package/mod.js +11 -0
- package/mod.js.map +1 -0
- package/mod.ts +34 -0
- package/package.json +57 -4
- package/page.js +352 -0
- package/page.js.map +1 -0
- package/page.ts +491 -0
- package/routes.js +22 -0
- package/routes.js.map +1 -0
- package/routes.ts +19 -0
- package/service.js +124 -0
- package/service.js.map +1 -0
- package/service.ts +149 -0
- package/tally.js +385 -0
- package/tally.js.map +1 -0
- package/tally.ts +424 -0
- package/tools.js +243 -0
- package/tools.js.map +1 -0
- package/tools.ts +357 -0
- package/use.js +172 -0
- package/use.js.map +1 -0
- package/use.ts +226 -0
- package/views.js +79 -0
- package/views.js.map +1 -0
- package/views.ts +96 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +237 -0
- package/vocab.ts +18 -0
- package/window.js +259 -0
- package/window.js.map +1 -0
- package/window.ts +336 -0
- package/words.js +47 -0
- package/words.js.map +1 -0
- package/words.ts +77 -0
package/page.ts
ADDED
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the tokens went, as the browsing app draws it: the plan limits
|
|
3
|
+
* against their pace, then a span's tally (the Claude plan's week by
|
|
4
|
+
* default, beside the week before it) by part of the context, session, task,
|
|
5
|
+
* large entry and model. A day's row is the same tally for its day. Every
|
|
6
|
+
* number comes from the `spend` rows the duty keeps (./service.ts); nothing
|
|
7
|
+
* here reads a request. Rows a tally names are drawn by name through the
|
|
8
|
+
* shared renderers.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import { createElement as h, Fragment, type JSX, type ReactNode } from 'react'
|
|
13
|
+
import { and, eq, ge, lt, parse, stringify } from '@yaks/query'
|
|
14
|
+
import { labelOf } from '@yaks/model'
|
|
15
|
+
import { partQuery } from '@yaks/session/parts'
|
|
16
|
+
import { mention, type Props, useNamed, type View } from '@yaks/inspect'
|
|
17
|
+
import { Head, Loading, Meter, Section, Table } from '@yaks/ui'
|
|
18
|
+
import { merged, ranked, type Rows, type Spend, spendOf } from './tally.js'
|
|
19
|
+
import {
|
|
20
|
+
accountNames,
|
|
21
|
+
after,
|
|
22
|
+
current,
|
|
23
|
+
type Limit,
|
|
24
|
+
limitRates,
|
|
25
|
+
limitsOf,
|
|
26
|
+
paceOf,
|
|
27
|
+
rates,
|
|
28
|
+
span,
|
|
29
|
+
unpricedRates,
|
|
30
|
+
} from './window.js'
|
|
31
|
+
import { dollars, percent, tokens } from './words.js'
|
|
32
|
+
|
|
33
|
+
let { Body, Cell, Heading, Row } = Table
|
|
34
|
+
let TOP = 15
|
|
35
|
+
|
|
36
|
+
let sum = (ns: number[]) => ns.slice(0, 4).reduce((a, b) => a + b, 0)
|
|
37
|
+
|
|
38
|
+
let waiting = (got: Props['got'][string] | undefined, what: string) =>
|
|
39
|
+
got?.error
|
|
40
|
+
? h(Section.Sub, { role: 'alert' }, got.error)
|
|
41
|
+
: !got?.ready
|
|
42
|
+
? h(Loading, {}, `Loading ${what}…`)
|
|
43
|
+
: null
|
|
44
|
+
|
|
45
|
+
// The models a figure was built without a price for: it is missing them, not
|
|
46
|
+
// zero, so it says so in place of a number.
|
|
47
|
+
let Unpriced = ({ io, models }: { io: Props['io']; models: string[] }) => {
|
|
48
|
+
useNamed(io, models)
|
|
49
|
+
return h(
|
|
50
|
+
Fragment,
|
|
51
|
+
{},
|
|
52
|
+
'unpriced: ',
|
|
53
|
+
...models.flatMap((m, i) => [i ? ', ' : '', mention(io, m)]),
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A plan limit and its pace, on one line.
|
|
58
|
+
let Limits = (
|
|
59
|
+
{ io, limits, now, worth, unpriced }: {
|
|
60
|
+
io: Props['io']
|
|
61
|
+
limits: Limit[]
|
|
62
|
+
now: number
|
|
63
|
+
worth: Map<Limit, number>
|
|
64
|
+
unpriced: Map<string, string[]>
|
|
65
|
+
},
|
|
66
|
+
) =>
|
|
67
|
+
h(
|
|
68
|
+
Section,
|
|
69
|
+
{},
|
|
70
|
+
h(Section.Title, {}, 'Plan limits'),
|
|
71
|
+
h(
|
|
72
|
+
Section.Sub,
|
|
73
|
+
{},
|
|
74
|
+
'Each bar is what is used; the mark is how much of its window is gone, which is what an even spend would have used by now. The week % beside each row below is an estimate of its share of its plan’s weekly limit: the week’s used % over the dollars, at list price, its requests ran up since the week began. A request paid per token uses no plan.',
|
|
75
|
+
),
|
|
76
|
+
h(
|
|
77
|
+
Table,
|
|
78
|
+
{ cols: ['key', null, 'prose'] },
|
|
79
|
+
h(
|
|
80
|
+
Body,
|
|
81
|
+
{},
|
|
82
|
+
limits.filter((l) => current(l, now)).map((l) => {
|
|
83
|
+
let pace = paceOf(l, now)
|
|
84
|
+
return h(
|
|
85
|
+
Row,
|
|
86
|
+
{ key: `${l.of} ${l.model ?? ''} ${l.window}` },
|
|
87
|
+
h(
|
|
88
|
+
Cell,
|
|
89
|
+
{ mod: 'key' },
|
|
90
|
+
`${l.name} ${l.window == 10080 ? 'week' : 'five hours'}`,
|
|
91
|
+
),
|
|
92
|
+
h(Cell, {}, h(Meter, { value: l.used / 100, mark: pace })),
|
|
93
|
+
h(
|
|
94
|
+
Cell,
|
|
95
|
+
{ mod: 'prose' },
|
|
96
|
+
`${Math.round(l.used)}% used, ${
|
|
97
|
+
Math.round(pace * 100)
|
|
98
|
+
}% of it gone${
|
|
99
|
+
l.resets
|
|
100
|
+
? `, resets ${l.resets.slice(0, 16).replace('T', ' ')} UTC`
|
|
101
|
+
: ''
|
|
102
|
+
}`,
|
|
103
|
+
l.window == 10080 && !l.model && unpriced.has(l.of)
|
|
104
|
+
? h(
|
|
105
|
+
Fragment,
|
|
106
|
+
{},
|
|
107
|
+
'; 1% of it is ',
|
|
108
|
+
h(Unpriced, { io, models: unpriced.get(l.of)! }),
|
|
109
|
+
)
|
|
110
|
+
: l.window == 10080 && worth.get(l)
|
|
111
|
+
? `; 1% of it is about ${
|
|
112
|
+
dollars(1 / worth.get(l)!)
|
|
113
|
+
} at list price`
|
|
114
|
+
: '',
|
|
115
|
+
),
|
|
116
|
+
)
|
|
117
|
+
}),
|
|
118
|
+
),
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
// What share of each plan's weekly limit a row used, in words, from the
|
|
123
|
+
// dollars it billed each plan and what a dollar of that plan is worth. Shares
|
|
124
|
+
// of two plans don't add, so each is said apart, by its plan's name where
|
|
125
|
+
// more than one plan is measured.
|
|
126
|
+
type Share = (row: Rows, key: string, dollars: number) => ReactNode
|
|
127
|
+
|
|
128
|
+
let shareOf = (
|
|
129
|
+
io: Props['io'],
|
|
130
|
+
s: Spend,
|
|
131
|
+
rate: Map<string, number>,
|
|
132
|
+
unpriced: Map<string, string[]>,
|
|
133
|
+
names: Map<string, string>,
|
|
134
|
+
): Share =>
|
|
135
|
+
(row, key, spent) => {
|
|
136
|
+
let billed = Object.entries(s.plans).map(([p, plan]) =>
|
|
137
|
+
[p, plan[row]?.[key] ?? 0] as const
|
|
138
|
+
)
|
|
139
|
+
let rated = billed.filter(([p, n]) => n && rate.has(p))
|
|
140
|
+
let named = (p: string) => rate.size > 1 ? ` ${names.get(p) ?? p}` : ''
|
|
141
|
+
return rated.length
|
|
142
|
+
? rated.flatMap(([p, d], i) => [
|
|
143
|
+
i ? ' · ' : '',
|
|
144
|
+
unpriced.has(p)
|
|
145
|
+
? h(Unpriced, { io, models: unpriced.get(p)! })
|
|
146
|
+
: percent(d * rate.get(p)!),
|
|
147
|
+
named(p),
|
|
148
|
+
])
|
|
149
|
+
: billed.some(([, n]) => n)
|
|
150
|
+
? 'no reading'
|
|
151
|
+
: spent
|
|
152
|
+
? 'per token'
|
|
153
|
+
: ''
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// A ranking: each row's name, a bar of its share of the costliest, its
|
|
157
|
+
// dollars and what else the caller says about it.
|
|
158
|
+
let Ranking = (
|
|
159
|
+
{
|
|
160
|
+
title,
|
|
161
|
+
sub,
|
|
162
|
+
row,
|
|
163
|
+
rows,
|
|
164
|
+
at,
|
|
165
|
+
name,
|
|
166
|
+
more,
|
|
167
|
+
heads,
|
|
168
|
+
share,
|
|
169
|
+
paid = (_, n) => dollars(n),
|
|
170
|
+
}: {
|
|
171
|
+
title: string
|
|
172
|
+
sub: string
|
|
173
|
+
/** which of a tally's rows these are, for `share` */
|
|
174
|
+
row?: Rows
|
|
175
|
+
rows: [string, number[]][]
|
|
176
|
+
at: number
|
|
177
|
+
name: (key: string) => ReactNode
|
|
178
|
+
more: (ns: number[], key: string) => ReactNode[]
|
|
179
|
+
heads: string[]
|
|
180
|
+
share?: Share
|
|
181
|
+
/** what a row's dollars say (default: the dollars) */
|
|
182
|
+
paid?: (key: string, n: number) => ReactNode
|
|
183
|
+
},
|
|
184
|
+
) => {
|
|
185
|
+
let most = rows[0]?.[1][at] || 1
|
|
186
|
+
let shared = share ? ['num'] : []
|
|
187
|
+
return h(
|
|
188
|
+
Section,
|
|
189
|
+
{},
|
|
190
|
+
h(Section.Title, {}, title, ' ', h(Section.Count, {}, String(rows.length))),
|
|
191
|
+
h(Section.Sub, {}, sub),
|
|
192
|
+
h(
|
|
193
|
+
Table,
|
|
194
|
+
{ cols: [null, null, 'num', ...shared, ...heads.map(() => 'num')] },
|
|
195
|
+
h(
|
|
196
|
+
Table.Head,
|
|
197
|
+
{},
|
|
198
|
+
h(
|
|
199
|
+
Row,
|
|
200
|
+
{},
|
|
201
|
+
h(Heading, {}, ''),
|
|
202
|
+
h(Heading, {}, ''),
|
|
203
|
+
h(Heading, { mod: ['num', 'desc'] }, 'dollars'),
|
|
204
|
+
share ? h(Heading, { mod: 'num' }, 'week %') : null,
|
|
205
|
+
...heads.map((k) => h(Heading, { key: k, mod: 'num' }, k)),
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
h(
|
|
209
|
+
Body,
|
|
210
|
+
{},
|
|
211
|
+
rows.map(([k, ns]) =>
|
|
212
|
+
h(
|
|
213
|
+
Row,
|
|
214
|
+
{ key: k },
|
|
215
|
+
h(Cell, {}, name(k)),
|
|
216
|
+
h(Cell, {}, h(Meter, { value: ns[at] / most, width: 10 })),
|
|
217
|
+
h(Cell, { mod: 'num' }, paid(k, ns[at])),
|
|
218
|
+
share && row
|
|
219
|
+
? h(Cell, { mod: 'num' }, share(row, k, ns[at]))
|
|
220
|
+
: null,
|
|
221
|
+
...more(ns, k).map((s, i) => h(Cell, { key: i, mod: 'num' }, s)),
|
|
222
|
+
)
|
|
223
|
+
),
|
|
224
|
+
),
|
|
225
|
+
),
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// What each account ran up, beside its share of its own plan's week and the
|
|
230
|
+
// week before; what billed no plan is the last row, paid by the token.
|
|
231
|
+
let Accounts = (
|
|
232
|
+
{ io, s, before, rate, unpriced, names }: {
|
|
233
|
+
io: Props['io']
|
|
234
|
+
s: Spend
|
|
235
|
+
before: Spend
|
|
236
|
+
rate: Map<string, number>
|
|
237
|
+
unpriced: Map<string, string[]>
|
|
238
|
+
names: Map<string, string>
|
|
239
|
+
},
|
|
240
|
+
) => {
|
|
241
|
+
let rows: Record<string, number[]> = {}
|
|
242
|
+
for (let [k, p] of Object.entries(s.plans)) rows[k] = [p.dollars]
|
|
243
|
+
for (let k of Object.keys(before.plans)) rows[k] ??= [0]
|
|
244
|
+
let billed = Object.values(rows).reduce((n, [d]) => n + d, 0)
|
|
245
|
+
if (s.dollars - billed > 0.005) rows[''] = [s.dollars - billed]
|
|
246
|
+
let was = (k: string) =>
|
|
247
|
+
k ? before.plans[k]?.dollars ?? 0 : before.dollars -
|
|
248
|
+
Object.values(before.plans).reduce((n, p) => n + p.dollars, 0)
|
|
249
|
+
return h(Ranking, {
|
|
250
|
+
title: 'Accounts',
|
|
251
|
+
sub:
|
|
252
|
+
'What each account’s requests ran up at list price, and their share of that account’s own weekly limit. A request paid by the token bills no account.',
|
|
253
|
+
rows: ranked(rows, 0),
|
|
254
|
+
at: 0,
|
|
255
|
+
name: (k) => h('span', {}, k ? names.get(k) ?? k : 'paid by the token'),
|
|
256
|
+
more: (ns, k) => [
|
|
257
|
+
!k
|
|
258
|
+
? ''
|
|
259
|
+
: unpriced.has(k)
|
|
260
|
+
? h(Unpriced, { io, models: unpriced.get(k)! })
|
|
261
|
+
: rate.has(k)
|
|
262
|
+
? percent(ns[0] * rate.get(k)!)
|
|
263
|
+
: 'no reading',
|
|
264
|
+
dollars(was(k)),
|
|
265
|
+
],
|
|
266
|
+
heads: ['week %', 'week before'],
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// A part's name, linked to the entries it is made of that were written in
|
|
271
|
+
// the span the tally covers, where a query names them.
|
|
272
|
+
let part = (io: Props['io'], [from, to]: [string, string]) => (k: string) => {
|
|
273
|
+
let q = partQuery(k)
|
|
274
|
+
return q
|
|
275
|
+
? h('a', {
|
|
276
|
+
href: io.find(
|
|
277
|
+
stringify(
|
|
278
|
+
and(
|
|
279
|
+
q,
|
|
280
|
+
...(from == to
|
|
281
|
+
? [eq('created.at', from)]
|
|
282
|
+
: [ge('created.at', from), lt('created.at', after(to))]),
|
|
283
|
+
),
|
|
284
|
+
),
|
|
285
|
+
),
|
|
286
|
+
}, k)
|
|
287
|
+
: h('span', {}, k)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** A tally of the days from one to another, ranked, beside an earlier one to
|
|
291
|
+
* read it against. */
|
|
292
|
+
let Breakdown = (
|
|
293
|
+
{ io, s, span, before, share }: {
|
|
294
|
+
io: Props['io']
|
|
295
|
+
s: Spend
|
|
296
|
+
span: [string, string]
|
|
297
|
+
before?: Spend
|
|
298
|
+
share?: Share
|
|
299
|
+
},
|
|
300
|
+
) => {
|
|
301
|
+
let top = <T>(rows: T[]) => rows.slice(0, TOP)
|
|
302
|
+
let sessions = top(ranked(s.sessions, 1))
|
|
303
|
+
let tasks = top(ranked(s.tasks, 1))
|
|
304
|
+
let entries = top(ranked(s.entries, 1)).slice(0, 10)
|
|
305
|
+
let models = ranked(s.models, 4)
|
|
306
|
+
useNamed(io, [...sessions, ...tasks, ...entries, ...models].map(([k]) => k))
|
|
307
|
+
let named = (k: string) => k ? mention(io, k) : h('span', {}, 'none')
|
|
308
|
+
let was = (k: string) => before ? [dollars(before.parts[k]?.[4] ?? 0)] : []
|
|
309
|
+
return h(
|
|
310
|
+
Fragment,
|
|
311
|
+
{},
|
|
312
|
+
h(Ranking, {
|
|
313
|
+
title: 'What the context was made of',
|
|
314
|
+
row: 'parts',
|
|
315
|
+
share,
|
|
316
|
+
sub:
|
|
317
|
+
'Each part of what the requests carried, at what it cost as it was paid: read from the cache, written to it, sent fresh, or generated. A part is paid for on every request it rides in. The totals are measured; the split across parts is estimated from their sizes.',
|
|
318
|
+
rows: ranked(s.parts, 4).slice(0, 30),
|
|
319
|
+
at: 4,
|
|
320
|
+
name: part(io, span),
|
|
321
|
+
more: (ns, k) => [
|
|
322
|
+
tokens(ns[0]),
|
|
323
|
+
tokens(ns[1]),
|
|
324
|
+
tokens(ns[2]),
|
|
325
|
+
tokens(ns[3]),
|
|
326
|
+
...was(k),
|
|
327
|
+
],
|
|
328
|
+
heads: [
|
|
329
|
+
'read',
|
|
330
|
+
'written',
|
|
331
|
+
'fresh',
|
|
332
|
+
'generated',
|
|
333
|
+
...before ? ['week before'] : [],
|
|
334
|
+
],
|
|
335
|
+
}),
|
|
336
|
+
h(Ranking, {
|
|
337
|
+
title: 'Sessions',
|
|
338
|
+
row: 'sessions',
|
|
339
|
+
share,
|
|
340
|
+
sub:
|
|
341
|
+
'Each session by what its requests cost; a subagent is a session of its own.',
|
|
342
|
+
rows: sessions,
|
|
343
|
+
at: 1,
|
|
344
|
+
name: named,
|
|
345
|
+
more: (ns) => [String(ns[2]), tokens(ns[0])],
|
|
346
|
+
heads: ['requests', 'tokens'],
|
|
347
|
+
}),
|
|
348
|
+
h(Ranking, {
|
|
349
|
+
title: 'Tasks',
|
|
350
|
+
row: 'tasks',
|
|
351
|
+
share,
|
|
352
|
+
sub:
|
|
353
|
+
'Each session split evenly across the tasks it claimed, commented on, filed or finished, and a subagent across the tasks its brief names, else as the session that started it.',
|
|
354
|
+
rows: tasks,
|
|
355
|
+
at: 1,
|
|
356
|
+
name: named,
|
|
357
|
+
more: (ns) => [tokens(ns[0])],
|
|
358
|
+
heads: ['tokens'],
|
|
359
|
+
}),
|
|
360
|
+
h(Ranking, {
|
|
361
|
+
title: 'Entries carried most',
|
|
362
|
+
row: 'entries',
|
|
363
|
+
share,
|
|
364
|
+
sub:
|
|
365
|
+
'What one entry of a transcript cost as it was carried, request after request.',
|
|
366
|
+
rows: entries,
|
|
367
|
+
at: 1,
|
|
368
|
+
name: named,
|
|
369
|
+
more: (ns) => [tokens(ns[0])],
|
|
370
|
+
heads: ['tokens'],
|
|
371
|
+
}),
|
|
372
|
+
h(Ranking, {
|
|
373
|
+
title: 'Models',
|
|
374
|
+
row: 'models',
|
|
375
|
+
share,
|
|
376
|
+
sub:
|
|
377
|
+
'At the price the provider reported, else at the model’s list price.',
|
|
378
|
+
rows: models,
|
|
379
|
+
at: 4,
|
|
380
|
+
paid: (k, n) =>
|
|
381
|
+
s.unpriced.includes(k) ? h(Unpriced, { io, models: [k] }) : dollars(n),
|
|
382
|
+
name: named,
|
|
383
|
+
more: (ns) => [String(ns[5]), tokens(sum(ns))],
|
|
384
|
+
heads: ['requests', 'tokens'],
|
|
385
|
+
}),
|
|
386
|
+
)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
let facts = (s: Spend) => [
|
|
390
|
+
dollars(s.dollars),
|
|
391
|
+
`${s.requests.toLocaleString('en-US')} requests`,
|
|
392
|
+
`${tokens(sum(s.tokens))} tokens`,
|
|
393
|
+
]
|
|
394
|
+
|
|
395
|
+
/** The page: the limits, then the span the Claude week covers, beside the
|
|
396
|
+
* week before. */
|
|
397
|
+
export let Page = ({ io, got }: Props): JSX.Element => {
|
|
398
|
+
let now = Date.now()
|
|
399
|
+
let providers = new Map(
|
|
400
|
+
(got.providers?.rows ?? []).map((b) => [
|
|
401
|
+
b.entity.eid,
|
|
402
|
+
String((b.provider as { name?: unknown }).name ?? ''),
|
|
403
|
+
]),
|
|
404
|
+
)
|
|
405
|
+
let names = new Map([
|
|
406
|
+
...(got.models?.rows ?? []).map((b): [string, string] => [
|
|
407
|
+
b.entity.eid,
|
|
408
|
+
labelOf(b.model as { name?: unknown; label?: unknown }),
|
|
409
|
+
]),
|
|
410
|
+
...providers,
|
|
411
|
+
...accountNames(got.accounts?.rows ?? [], providers),
|
|
412
|
+
])
|
|
413
|
+
let limits = limitsOf(got.limits?.rows ?? [], names)
|
|
414
|
+
let days = (got.days?.rows ?? []).map(spendOf).filter((s): s is Spend => !!s)
|
|
415
|
+
let [from, to] = span(limits, now)
|
|
416
|
+
let within = (a: string, b: string) =>
|
|
417
|
+
days.filter((d) => d.day >= a && d.day <= b)
|
|
418
|
+
let week = merged(within(from, to))
|
|
419
|
+
let before = merged(within(after(from, -7), after(from, -1)))
|
|
420
|
+
let rate = rates(limits, days, now)
|
|
421
|
+
let worth = limitRates(limits, days, now)
|
|
422
|
+
let unpriced = unpricedRates(limits, days, now)
|
|
423
|
+
return h(
|
|
424
|
+
'article',
|
|
425
|
+
{},
|
|
426
|
+
h(
|
|
427
|
+
Head,
|
|
428
|
+
{},
|
|
429
|
+
h(Head.Title, {}, 'Where the tokens went'),
|
|
430
|
+
h(
|
|
431
|
+
Head.Sub,
|
|
432
|
+
{},
|
|
433
|
+
`${from} to ${to} (UTC): this Claude plan week so far, beside the week before. Tallied from every model request every ten minutes.`,
|
|
434
|
+
),
|
|
435
|
+
h(
|
|
436
|
+
Head.Facts,
|
|
437
|
+
{},
|
|
438
|
+
...facts(week),
|
|
439
|
+
`the week before ${dollars(before.dollars)}`,
|
|
440
|
+
),
|
|
441
|
+
),
|
|
442
|
+
waiting(got.limits, 'the plan limits') ??
|
|
443
|
+
h(Limits, { io, limits, now, worth, unpriced }),
|
|
444
|
+
waiting(got.days, 'the tallies') ??
|
|
445
|
+
(Object.keys(week.plans).length
|
|
446
|
+
? h(Accounts, { io, s: week, before, rate, unpriced, names })
|
|
447
|
+
: null),
|
|
448
|
+
waiting(got.days, 'the tallies') ??
|
|
449
|
+
h(Breakdown, {
|
|
450
|
+
io,
|
|
451
|
+
s: week,
|
|
452
|
+
span: [from, to],
|
|
453
|
+
before,
|
|
454
|
+
share: rate.size ? shareOf(io, week, rate, unpriced, names) : undefined,
|
|
455
|
+
}),
|
|
456
|
+
)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** A day's page: its tally. */
|
|
460
|
+
export let DayPage = ({ e, io }: Props): JSX.Element => {
|
|
461
|
+
let s = spendOf(e)
|
|
462
|
+
return h(
|
|
463
|
+
'article',
|
|
464
|
+
{},
|
|
465
|
+
h(
|
|
466
|
+
Head,
|
|
467
|
+
{},
|
|
468
|
+
h(Head.Title, {}, `Where the tokens went on ${s?.day ?? ''}`),
|
|
469
|
+
h(Head.Sub, {}, 'One UTC day, tallied from its model requests.'),
|
|
470
|
+
s ? h(Head.Facts, {}, ...facts(s)) : null,
|
|
471
|
+
),
|
|
472
|
+
s ? h(Breakdown, { io, s, span: [s.day, s.day] }) : null,
|
|
473
|
+
)
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** The views this package draws in the browsing app. */
|
|
477
|
+
export let inspectViews: View[] = [
|
|
478
|
+
{
|
|
479
|
+
view: 'Spend.Page',
|
|
480
|
+
match: true,
|
|
481
|
+
asks: () => ({
|
|
482
|
+
days: '.spend * .order=-spend.day .limit=21',
|
|
483
|
+
limits: '.rate_limit *',
|
|
484
|
+
providers: '.provider *',
|
|
485
|
+
accounts: '.account *',
|
|
486
|
+
models: '.model *',
|
|
487
|
+
}),
|
|
488
|
+
Render: Page,
|
|
489
|
+
},
|
|
490
|
+
{ view: 'Full', match: parse('.spend'), Render: DayPage },
|
|
491
|
+
]
|
package/routes.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/spend`: the Spend page at an address a person can type, and the line
|
|
3
|
+
* `yak serve` prints beside it when it starts listening, so someone who came
|
|
4
|
+
* for the page finds it without knowing the browsing app's `/?page=spend`
|
|
5
|
+
* (./views.ts `destinations`).
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/ /// routes()[0].handle(new Request('http://x/spend')).headers.get('location') -> '/?page=spend'
|
|
9
|
+
/** The page's address, sent on to the browsing app. */ export let routes = ()=>[
|
|
10
|
+
{
|
|
11
|
+
method: 'GET',
|
|
12
|
+
path: '/spend',
|
|
13
|
+
handle: ()=>new Response(null, {
|
|
14
|
+
status: 302,
|
|
15
|
+
headers: {
|
|
16
|
+
location: '/?page=spend'
|
|
17
|
+
}
|
|
18
|
+
}),
|
|
19
|
+
says: 'what your agents spent: Claude Code and Codex, by day and model'
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
//# sourceMappingURL=routes.js.map
|
package/routes.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sources":["./routes.ts"],"names":[],"mappings":"AAAA;;;;;;;CAOC,GAGD,+FAA+F;AAC/F,qDAAqD,GACrD,OAAO,IAAI,SAAS,IAAe;IAAC;MAClC,QAAQ;MACR,MAAM;MACN,QAAQ,IACN,IAAI,SAAS,MAAM;UAAE,QAAQ;UAAK,SAAS;YAAE,UAAU;UAAe;QAAE;MAC1E,MAAM;IACR;GAAE,CAAA"}
|
package/routes.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/spend`: the Spend page at an address a person can type, and the line
|
|
3
|
+
* `yak serve` prints beside it when it starts listening, so someone who came
|
|
4
|
+
* for the page finds it without knowing the browsing app's `/?page=spend`
|
|
5
|
+
* (./views.ts `destinations`).
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { Route } from 'jsr:@yaks/api@0.2.36'
|
|
10
|
+
|
|
11
|
+
/// routes()[0].handle(new Request('http://x/spend')).headers.get('location') -> '/?page=spend'
|
|
12
|
+
/** The page's address, sent on to the browsing app. */
|
|
13
|
+
export let routes = (): Route[] => [{
|
|
14
|
+
method: 'GET',
|
|
15
|
+
path: '/spend',
|
|
16
|
+
handle: () =>
|
|
17
|
+
new Response(null, { status: 302, headers: { location: '/?page=spend' } }),
|
|
18
|
+
says: 'what your agents spent: Claude Code and Codex, by day and model',
|
|
19
|
+
}]
|
package/service.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tally, as a duty: the module a server imports at `@yaks/spend/service`.
|
|
3
|
+
* Every pass tallies today again from its requests (./day.ts), and yesterday
|
|
4
|
+
* too for the first hours of a day, while its last requests are still being
|
|
5
|
+
* read in, and every past day marked `untallied` because requests reached it
|
|
6
|
+
* late, as a transcript read in after the fact does (./graph.ts); then it
|
|
7
|
+
* keeps the Claude plan's newest limits (./budget.ts). A
|
|
8
|
+
* page that shows where the tokens went reads these rows, never the
|
|
9
|
+
* requests, so looking costs nothing.
|
|
10
|
+
*
|
|
11
|
+
* It is a loop because a tally is owed as time passes, not when one write
|
|
12
|
+
* commits: a day of requests is thousands of writes, and tallying after each
|
|
13
|
+
* would cost more than the tally saves.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/ import { line } from "@yaks/query";
|
|
17
|
+
import { sleep } from "@yaks/fp";
|
|
18
|
+
import { recompute } from "./day.js";
|
|
19
|
+
import { after, dayOf, midnight } from "./window.js";
|
|
20
|
+
import { read } from "./budget.js";
|
|
21
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
22
|
+
// The plans a tally kept by provider, before each account kept its own.
|
|
23
|
+
let PROVIDERS = new Set([
|
|
24
|
+
'claude',
|
|
25
|
+
'codex'
|
|
26
|
+
].map((name)=>identityEid('provider', [
|
|
27
|
+
name
|
|
28
|
+
])));
|
|
29
|
+
/** How long a pass waits for the next. */ export let EVERY = 10 * 60 * 1000;
|
|
30
|
+
/** How often the duty looks for days marked `untallied` between passes, so
|
|
31
|
+
* transcripts read in on a first `yak serve` show within a minute. */ export let LOOK = 60 * 1000;
|
|
32
|
+
/** How long into a day yesterday is tallied again. */ export let LATE = 3 * 60 * 60 * 1000;
|
|
33
|
+
/** One pass: today, yesterday while it may still be arriving, the days
|
|
34
|
+
* requests reached late, the limits.
|
|
35
|
+
* Returns the models its tallies met with no price, which are missing from
|
|
36
|
+
* the dollars, not zero in them. */ export let pass = async (g, now = Date.now(), o = {})=>{
|
|
37
|
+
let today = dayOf(now);
|
|
38
|
+
let unpriced = new Set();
|
|
39
|
+
let tally = async (day)=>{
|
|
40
|
+
for (let m of (await recompute(g, day)).unpriced)unpriced.add(m);
|
|
41
|
+
};
|
|
42
|
+
if (now - Date.parse(midnight(today)) < LATE) {
|
|
43
|
+
await tally(after(today, -1));
|
|
44
|
+
}
|
|
45
|
+
await tally(today);
|
|
46
|
+
for (let m of (await owed(g)))unpriced.add(m);
|
|
47
|
+
// A day of the past week tallied before tallies kept their plans, or kept
|
|
48
|
+
// them by provider before each account had its own, is tallied again, so
|
|
49
|
+
// its week reads in each account's shares too.
|
|
50
|
+
let kept = await g.read(line`.spend .spend.day>=${after(today, -8)} .fields=spend.day,spend.plans`);
|
|
51
|
+
for (let b of kept){
|
|
52
|
+
let s = b.spend;
|
|
53
|
+
let byProvider = Object.keys(s.plans ?? {}).some((k)=>PROVIDERS.has(k));
|
|
54
|
+
if ((!s.plans || byProvider) && s.day != today) await tally(s.day);
|
|
55
|
+
}
|
|
56
|
+
await read(g, o.usage);
|
|
57
|
+
return [
|
|
58
|
+
...unpriced
|
|
59
|
+
];
|
|
60
|
+
};
|
|
61
|
+
/** Tally each day marked `untallied`. The mark comes off before the day is
|
|
62
|
+
* read, so a request written while it is tallied marks it again. Returns the
|
|
63
|
+
* models its tallies met with no price. */ export let owed = async (g)=>{
|
|
64
|
+
let unpriced = new Set();
|
|
65
|
+
for (let b of (await g.read('.untallied .fields=untallied.day'))){
|
|
66
|
+
let day = String(b.untallied.day);
|
|
67
|
+
await g.apply([
|
|
68
|
+
{
|
|
69
|
+
entity: b.entity,
|
|
70
|
+
untallied: null
|
|
71
|
+
}
|
|
72
|
+
], {
|
|
73
|
+
trusted: true
|
|
74
|
+
});
|
|
75
|
+
for (let m of (await recompute(g, day)).unpriced)unpriced.add(m);
|
|
76
|
+
}
|
|
77
|
+
return [
|
|
78
|
+
...unpriced
|
|
79
|
+
];
|
|
80
|
+
};
|
|
81
|
+
/** Tally, then again every `every`, and the owed days every `LOOK`, until the signal aborts. An already
|
|
82
|
+
* aborted signal is one pass. */ export let service = async (host, options = {}, signal = AbortSignal.abort())=>{
|
|
83
|
+
let told = new Set();
|
|
84
|
+
let every = options.every ?? EVERY;
|
|
85
|
+
let due = 0;
|
|
86
|
+
for(;;){
|
|
87
|
+
try {
|
|
88
|
+
let whole = Date.now() >= due;
|
|
89
|
+
if (whole) due = Date.now() + every;
|
|
90
|
+
let unpriced = whole ? await pass(host.graph, Date.now(), options) : await owed(host.graph);
|
|
91
|
+
for (let m of unpriced){
|
|
92
|
+
if (told.has(m)) continue;
|
|
93
|
+
told.add(m);
|
|
94
|
+
// A model with no price counts as nothing: a failure, fixed with a
|
|
95
|
+
// `price` on its row, told once while this server runs. A request
|
|
96
|
+
// that named no model is fixed where it was asked, so its ask names
|
|
97
|
+
// what ran (@yaks/spawn `asked`).
|
|
98
|
+
let row = m ? (await host.graph.get([
|
|
99
|
+
m
|
|
100
|
+
], [
|
|
101
|
+
'model'
|
|
102
|
+
]))[0]?.model : undefined;
|
|
103
|
+
(host.report ?? console.error)(new Error(m ? `model ${row?.name ?? m} has no price` : 'a request named no model, so it has no price'), {
|
|
104
|
+
tags: {
|
|
105
|
+
step: 'spend unpriced',
|
|
106
|
+
model: m
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} catch (e) {
|
|
111
|
+
// A pass that failed is told, and the next one tallies again.
|
|
112
|
+
;
|
|
113
|
+
(host.report ?? console.error)(e, {
|
|
114
|
+
tags: {
|
|
115
|
+
step: 'spend pass'
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (signal.aborted) return;
|
|
120
|
+
await sleep(Math.min(every, LOOK), signal);
|
|
121
|
+
if (signal.aborted) return;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=service.js.map
|
package/service.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.js","sources":["./service.ts"],"names":[],"mappings":"AAAA;;;;;;;;;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