@trazum/core 1.81.0 → 1.83.0
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/README.md +34 -18
- package/dist/advisories.d.ts +33 -1
- package/dist/advisories.d.ts.map +1 -1
- package/dist/advisories.js +130 -20
- package/dist/advisories.js.map +1 -1
- package/dist/band.d.ts +131 -0
- package/dist/band.d.ts.map +1 -0
- package/dist/band.js +210 -0
- package/dist/band.js.map +1 -0
- package/dist/conform.d.ts +1 -1
- package/dist/conform.d.ts.map +1 -1
- package/dist/conform.js +84 -1
- package/dist/conform.js.map +1 -1
- package/dist/helicone.d.ts +92 -0
- package/dist/helicone.d.ts.map +1 -0
- package/dist/helicone.js +194 -0
- package/dist/helicone.js.map +1 -0
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +16 -3
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +17 -3
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +60 -1
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.d.ts +16 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/dist/langsmith.d.ts +104 -0
- package/dist/langsmith.d.ts.map +1 -0
- package/dist/langsmith.js +270 -0
- package/dist/langsmith.js.map +1 -0
- package/dist/levers.d.ts +1 -1
- package/dist/levers.js +2 -2
- package/dist/levers.js.map +1 -1
- package/dist/litellm.d.ts +98 -0
- package/dist/litellm.d.ts.map +1 -0
- package/dist/litellm.js +207 -0
- package/dist/litellm.js.map +1 -0
- package/dist/measured-profile.d.ts +4 -0
- package/dist/measured-profile.d.ts.map +1 -1
- package/dist/measured-profile.js +22 -1
- package/dist/measured-profile.js.map +1 -1
- package/dist/pricing.d.ts +98 -3
- package/dist/pricing.d.ts.map +1 -1
- package/dist/pricing.js +393 -35
- package/dist/pricing.js.map +1 -1
- package/dist/receipt.d.ts +253 -0
- package/dist/receipt.d.ts.map +1 -0
- package/dist/receipt.js +111 -0
- package/dist/receipt.js.map +1 -0
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +36 -0
- package/dist/schemas.js.map +1 -1
- package/dist/tokenizer.d.ts +45 -49
- package/dist/tokenizer.d.ts.map +1 -1
- package/dist/tokenizer.js +100 -51
- package/dist/tokenizer.js.map +1 -1
- package/dist/types.d.ts +108 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/verdict-bridge.d.ts +113 -0
- package/dist/verdict-bridge.d.ts.map +1 -0
- package/dist/verdict-bridge.js +127 -0
- package/dist/verdict-bridge.js.map +1 -0
- package/package.json +1 -1
- package/src/advisories.ts +166 -19
- package/src/band.ts +213 -0
- package/src/conform.ts +100 -1
- package/src/helicone.ts +227 -0
- package/src/i18n/en.ts +19 -3
- package/src/i18n/es.ts +20 -3
- package/src/i18n/types.ts +63 -1
- package/src/index.ts +31 -2
- package/src/langsmith.ts +315 -0
- package/src/levers.ts +3 -3
- package/src/litellm.ts +243 -0
- package/src/measured-profile.ts +26 -2
- package/src/pricing.ts +430 -30
- package/src/receipt.ts +351 -0
- package/src/schemas.ts +36 -0
- package/src/tokenizer.ts +101 -51
- package/src/types.ts +104 -0
- package/src/verdict-bridge.ts +200 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import type { PricingCatalogue } from './pricing.js';
|
|
2
|
+
import type { UsageProfileReport } from './usage.js';
|
|
3
|
+
/**
|
|
4
|
+
* The receipt: a bill's counts, with the provenance of every figure attached.
|
|
5
|
+
*
|
|
6
|
+
* ## What it is for
|
|
7
|
+
*
|
|
8
|
+
* A profile answers a question on the terminal where it ran. A receipt is the
|
|
9
|
+
* same figures shaped so they can still answer it somewhere else: aggregated by
|
|
10
|
+
* somebody's dashboard, filed against an invoice, or compared against last
|
|
11
|
+
* month's by a reader who was not there when the command ran.
|
|
12
|
+
*
|
|
13
|
+
* That is why every line carries the rate behind its money and the date that
|
|
14
|
+
* rate was last read. A dollar figure with no provenance cannot tell a
|
|
15
|
+
* repricing from a team whose spend moved, and the consumer that most needs
|
|
16
|
+
* that distinction is the one that cannot read a sentence explaining it.
|
|
17
|
+
*
|
|
18
|
+
* ## Why a line carries money per bucket and not only a rate
|
|
19
|
+
*
|
|
20
|
+
* The first version of this module published the catalogue's `inputPerMTok`
|
|
21
|
+
* and `outputPerMTok` beside a dollar figure computed by `profileUsage`. Those
|
|
22
|
+
* are not always the same rates. A model in a promotional window, or one whose
|
|
23
|
+
* long-context tier applied, is billed at `effectivePricing`'s figure, and a
|
|
24
|
+
* cached read is billed at a fraction of input that no published field named at
|
|
25
|
+
* all. **A consumer multiplying the stated rates by the stated tokens got a
|
|
26
|
+
* different number from the stated total, with nothing saying which was
|
|
27
|
+
* wrong.**
|
|
28
|
+
*
|
|
29
|
+
* That is a bad defect in a document whose entire purpose is that a figure can
|
|
30
|
+
* be recomputed by whoever receives it, and it is the exact failure the
|
|
31
|
+
* doctrine line at the foot of this comment is about.
|
|
32
|
+
*
|
|
33
|
+
* So a line now carries the **money as it was actually apportioned** -- input,
|
|
34
|
+
* cache reads, cache writes and output -- and `usd` is their sum. The rates stay
|
|
35
|
+
* as provenance, labelled as the catalogue's published figures rather than as
|
|
36
|
+
* the arithmetic. `receipt-arithmetic.test.js` fails if the four stop adding up,
|
|
37
|
+
* which is a property a reader can check on any receipt they are handed rather
|
|
38
|
+
* than a promise they have to take.
|
|
39
|
+
*
|
|
40
|
+
* ## What it carries, and what it has no field for
|
|
41
|
+
*
|
|
42
|
+
* Counts, model, provider, the money split by bucket, the published rates, the
|
|
43
|
+
* review date, the label the log carried, the period, and what could not be
|
|
44
|
+
* priced. There is **no field
|
|
45
|
+
* for prompt text, for an answer, for a file path, for a branch name or for a
|
|
46
|
+
* credential** — not redacted, not hashed, absent, because the input this takes
|
|
47
|
+
* has nowhere to hold them either.
|
|
48
|
+
*
|
|
49
|
+
* `receipt-redaction.test.js` holds that by planting all 4 in a log — in the
|
|
50
|
+
* session field among other places, which is the value this package has
|
|
51
|
+
* promised since it shipped never comes back out — and failing if any reaches
|
|
52
|
+
* the output. A property about what a document does not contain is exactly the
|
|
53
|
+
* kind this repository proves by breaking rather than by asserting.
|
|
54
|
+
*
|
|
55
|
+
* ## What it is not
|
|
56
|
+
*
|
|
57
|
+
* **Not a transport.** Nothing here opens a socket, reads an environment
|
|
58
|
+
* variable or takes an endpoint. `receiptFrom` returns a value; where it goes
|
|
59
|
+
* is the caller's decision. A module in this package that phoned home would
|
|
60
|
+
* break the roadmap's first rule, which is that no feature may make a network
|
|
61
|
+
* call a prerequisite for anything.
|
|
62
|
+
*
|
|
63
|
+
* **Not a new measurement.** Every figure is one `profileUsage` already
|
|
64
|
+
* produced. This adds provenance around them and takes nothing away.
|
|
65
|
+
*
|
|
66
|
+
* Doctrine: [A machine reader gets the provenance too](../../../docs/doctrine.md#a-machine-reader-gets-the-provenance-too)
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* The catalogue's published rates for this model, and when a human last read
|
|
70
|
+
* them off the provider's own page.
|
|
71
|
+
*
|
|
72
|
+
* **Provenance, not the arithmetic.** These are list rates. The money that was
|
|
73
|
+
* actually apportioned is in `ReceiptLine.money`, and the two differ whenever a
|
|
74
|
+
* promotion or a long-context tier applied, or for any token billed at a
|
|
75
|
+
* fraction of input -- which is every cached read. A consumer wanting the rate
|
|
76
|
+
* that was really charged divides a bucket's money by its tokens; a consumer
|
|
77
|
+
* wanting to know whether a provider repriced compares these.
|
|
78
|
+
*/
|
|
79
|
+
export interface ReceiptPricing {
|
|
80
|
+
provider: string;
|
|
81
|
+
inputPerMTok: number;
|
|
82
|
+
outputPerMTok: number;
|
|
83
|
+
/**
|
|
84
|
+
* The date this provider's rates were last read off its own page, `null`
|
|
85
|
+
* when the catalogue carries no date for that provider.
|
|
86
|
+
*
|
|
87
|
+
* Per provider rather than per catalogue, because that is how the prices are
|
|
88
|
+
* actually reviewed: looking at one page and finding nothing changed is not
|
|
89
|
+
* the same event as not looking. A server comparing two receipts a month
|
|
90
|
+
* apart reads a repricing as a repricing instead of as a team missing a
|
|
91
|
+
* target.
|
|
92
|
+
*/
|
|
93
|
+
reviewedOn: string | null;
|
|
94
|
+
}
|
|
95
|
+
/** Where a line's money went. The four add to `ReceiptLine.usd`. */
|
|
96
|
+
export interface ReceiptMoney {
|
|
97
|
+
inputUsd: number;
|
|
98
|
+
cacheReadUsd: number;
|
|
99
|
+
cacheWriteUsd: number;
|
|
100
|
+
outputUsd: number;
|
|
101
|
+
}
|
|
102
|
+
/** One slice of a bill: a label's calls to one model. */
|
|
103
|
+
export interface ReceiptLine {
|
|
104
|
+
/**
|
|
105
|
+
* The attribution the log carried, or `null` for the unlabelled bucket.
|
|
106
|
+
*
|
|
107
|
+
* `null` rather than the sentinel, which is an internal grouping key and is
|
|
108
|
+
* never serialised. A consumer that saw the raw sentinel would have to know
|
|
109
|
+
* this package's internals to render it.
|
|
110
|
+
*/
|
|
111
|
+
label: string | null;
|
|
112
|
+
model: string;
|
|
113
|
+
calls: number;
|
|
114
|
+
inputTokens: number;
|
|
115
|
+
cacheReadTokens: number;
|
|
116
|
+
/** Every cache write, whatever its TTL. The sum of the two fields below. */
|
|
117
|
+
cacheWriteTokens: number;
|
|
118
|
+
/**
|
|
119
|
+
* The two write TTLs kept apart, because they are billed at different rates.
|
|
120
|
+
*
|
|
121
|
+
* They are here for the same reason `UsageBreakdown` keeps them: the ratio
|
|
122
|
+
* between them is not a constant across providers, so a total that has lost
|
|
123
|
+
* the split can be repriced only by guessing at it. A consumer repricing this
|
|
124
|
+
* traffic against another model's rates needs both.
|
|
125
|
+
*/
|
|
126
|
+
cacheWrite5mTokens: number;
|
|
127
|
+
cacheWrite1hTokens: number;
|
|
128
|
+
outputTokens: number;
|
|
129
|
+
/**
|
|
130
|
+
* What it cost, which is exactly the sum of `money` below.
|
|
131
|
+
*
|
|
132
|
+
* Every line here is priced; what is not is in `gaps`.
|
|
133
|
+
*/
|
|
134
|
+
usd: number;
|
|
135
|
+
/**
|
|
136
|
+
* Where the money went, as it was apportioned rather than as it might be
|
|
137
|
+
* recomputed.
|
|
138
|
+
*
|
|
139
|
+
* This is the field that makes a receipt checkable. `usd` alone can only be
|
|
140
|
+
* believed; these four can be added up, and each can be divided by its own
|
|
141
|
+
* token count to recover the rate that was really charged -- including the
|
|
142
|
+
* one for cached reads, which no published rate names.
|
|
143
|
+
*/
|
|
144
|
+
money: ReceiptMoney;
|
|
145
|
+
/** The catalogue's published rates, as provenance. */
|
|
146
|
+
pricing: ReceiptPricing;
|
|
147
|
+
}
|
|
148
|
+
/** Everything a receipt refuses to answer, typed rather than left implicit. */
|
|
149
|
+
export type ReceiptGap =
|
|
150
|
+
/**
|
|
151
|
+
* Models the catalogue does not price, with the size of what they used.
|
|
152
|
+
*
|
|
153
|
+
* The counts are here rather than in a line, and the first draft of this
|
|
154
|
+
* module got that wrong in a way its own guard caught. `byLabelAndModel`
|
|
155
|
+
* excludes unpriced calls on purpose — the profile keeps them apart so that
|
|
156
|
+
* a total is never tokens from one set of calls over dollars from another —
|
|
157
|
+
* so a receipt built from that list alone silently loses them.
|
|
158
|
+
*
|
|
159
|
+
* The obvious repair was worse than the bug: emit a line with `label: null`.
|
|
160
|
+
* That reads as *this call carried no label*, when in fact it carried one the
|
|
161
|
+
* profile could not attach to a priced slice. Conflating "unlabelled" with
|
|
162
|
+
* "unattributable" is the exact failure this project refuses elsewhere, so
|
|
163
|
+
* the gap carries the magnitude instead and nothing pretends to an
|
|
164
|
+
* attribution it does not have.
|
|
165
|
+
*/
|
|
166
|
+
{
|
|
167
|
+
kind: 'unpriced';
|
|
168
|
+
models: string[];
|
|
169
|
+
calls: number;
|
|
170
|
+
inputTokens: number;
|
|
171
|
+
outputTokens: number;
|
|
172
|
+
} | {
|
|
173
|
+
kind: 'unread-lines';
|
|
174
|
+
count: number;
|
|
175
|
+
} | {
|
|
176
|
+
kind: 'no-clock';
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Calls whose cache-write TTL the log did not state, so the cheaper rate was
|
|
180
|
+
* assumed.
|
|
181
|
+
*
|
|
182
|
+
* Reported because it changes what kind of figure this is. A total that
|
|
183
|
+
* includes an assumed TTL is a **floor** on those calls, not a measurement,
|
|
184
|
+
* and a document that says `counting: 'counted'` while quietly resting on an
|
|
185
|
+
* assumption is claiming a precision it does not have.
|
|
186
|
+
*/
|
|
187
|
+
| {
|
|
188
|
+
kind: 'assumed-write-ttl';
|
|
189
|
+
calls: number;
|
|
190
|
+
};
|
|
191
|
+
export interface ReceiptDocument {
|
|
192
|
+
/** The one thing a consumer must branch on. Set here, by the builder. */
|
|
193
|
+
schemaVersion: 1;
|
|
194
|
+
/**
|
|
195
|
+
* When this receipt was made, or `null`.
|
|
196
|
+
*
|
|
197
|
+
* Optional on purpose, and the reason is the same one `plan.json` gives: a
|
|
198
|
+
* document without a stamp is undated, which a reader can act on. A document
|
|
199
|
+
* with an invented stamp is wrong, which they cannot.
|
|
200
|
+
*/
|
|
201
|
+
emittedAt: string | null;
|
|
202
|
+
/** The period the counts cover, from the log's own clock. */
|
|
203
|
+
span: {
|
|
204
|
+
fromMs: number;
|
|
205
|
+
toMs: number;
|
|
206
|
+
calls: number;
|
|
207
|
+
} | null;
|
|
208
|
+
/**
|
|
209
|
+
* How the tokens were arrived at.
|
|
210
|
+
*
|
|
211
|
+
* `counted` for every receipt this function builds, because a usage log
|
|
212
|
+
* carries the provider's own counts. The field exists because a receipt built
|
|
213
|
+
* from an estimate would be a different kind of evidence, and a consumer must
|
|
214
|
+
* never have to guess which one it is holding.
|
|
215
|
+
*/
|
|
216
|
+
counting: 'counted';
|
|
217
|
+
lines: ReceiptLine[];
|
|
218
|
+
total: {
|
|
219
|
+
calls: number;
|
|
220
|
+
usd: number;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* What this receipt cannot say, and why.
|
|
224
|
+
*
|
|
225
|
+
* Present and empty rather than absent when there is nothing to report:
|
|
226
|
+
* "nothing was refused" and "this document does not record refusals" are
|
|
227
|
+
* different statements, and a server aggregating a thousand receipts has to
|
|
228
|
+
* be able to tell them apart.
|
|
229
|
+
*/
|
|
230
|
+
gaps: ReceiptGap[];
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The fields that leave this machine, listed once, in one place.
|
|
234
|
+
*
|
|
235
|
+
* The redaction guard reads this array rather than a copy of it, so a field
|
|
236
|
+
* added to `ReceiptLine` and not added here fails the guard. A whitelist
|
|
237
|
+
* maintained in two places is a whitelist with a hole in it.
|
|
238
|
+
*/
|
|
239
|
+
export declare const RECEIPT_LINE_FIELDS: readonly ["label", "model", "calls", "inputTokens", "cacheReadTokens", "cacheWriteTokens", "cacheWrite5mTokens", "cacheWrite1hTokens", "outputTokens", "usd", "money", "pricing"];
|
|
240
|
+
export interface ReceiptOptions {
|
|
241
|
+
/** Stamp the document. Omit for an undated receipt, which is valid. */
|
|
242
|
+
emittedAt?: Date;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Build a receipt from a profile the caller already computed.
|
|
246
|
+
*
|
|
247
|
+
* Takes the report rather than the log text, and that is the guarantee rather
|
|
248
|
+
* than an ergonomic choice: `UsageProfileReport` has no field that can hold
|
|
249
|
+
* prompt content, so this function could not emit any even if it tried. The
|
|
250
|
+
* narrow input is what makes the promise checkable instead of merely stated.
|
|
251
|
+
*/
|
|
252
|
+
export declare function receiptFrom(report: UsageProfileReport, catalogue: PricingCatalogue, options?: ReceiptOptions): ReceiptDocument;
|
|
253
|
+
//# sourceMappingURL=receipt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"receipt.d.ts","sourceRoot":"","sources":["../src/receipt.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,KAAK,EAAkB,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAErE;;;;;;;;;;;;;;;;;;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AC,KAAK,EAAE,MAAM,CAAA;CAAE,GACvC;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE;AACtB;;;;;;;;GAQG;GACD;IAAE,IAAI,EAAE,mBAAmB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjD,MAAM,WAAW,eAAe;IAC9B,yEAAyE;IACzE,aAAa,EAAE,CAAC,CAAC;IACjB;;;;;;OAMG;IACH,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,6DAA6D;IAC7D,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IAC7D;;;;;;;OAOG;IACH,QAAQ,EAAE,SAAS,CAAC;IACpB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,KAAK,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC;IACtC;;;;;;;OAOG;IACH,IAAI,EAAE,UAAU,EAAE,CAAC;CACpB;AAED;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,mLAarB,CAAC;AA8CZ,MAAM,WAAW,cAAc;IAC7B,uEAAuE;IACvE,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,kBAAkB,EAC1B,SAAS,EAAE,gBAAgB,EAC3B,OAAO,GAAE,cAAmB,GAC3B,eAAe,CA8CjB"}
|
package/dist/receipt.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { PROVIDER_REVIEWED, modelFrom } from './pricing.js';
|
|
2
|
+
import { UNLABELLED } from './usage.js';
|
|
3
|
+
/**
|
|
4
|
+
* The fields that leave this machine, listed once, in one place.
|
|
5
|
+
*
|
|
6
|
+
* The redaction guard reads this array rather than a copy of it, so a field
|
|
7
|
+
* added to `ReceiptLine` and not added here fails the guard. A whitelist
|
|
8
|
+
* maintained in two places is a whitelist with a hole in it.
|
|
9
|
+
*/
|
|
10
|
+
export const RECEIPT_LINE_FIELDS = Object.freeze([
|
|
11
|
+
'label',
|
|
12
|
+
'model',
|
|
13
|
+
'calls',
|
|
14
|
+
'inputTokens',
|
|
15
|
+
'cacheReadTokens',
|
|
16
|
+
'cacheWriteTokens',
|
|
17
|
+
'cacheWrite5mTokens',
|
|
18
|
+
'cacheWrite1hTokens',
|
|
19
|
+
'outputTokens',
|
|
20
|
+
'usd',
|
|
21
|
+
'money',
|
|
22
|
+
'pricing',
|
|
23
|
+
]);
|
|
24
|
+
const lineFrom = (label, model, breakdown, catalogue) => {
|
|
25
|
+
/*
|
|
26
|
+
* Every slice reaching here is priced: `byLabelAndModel` holds only what the
|
|
27
|
+
* catalogue knows. What it does not know is reported by the `unpriced` gap,
|
|
28
|
+
* with its size, so the receipt's total is never mistaken for the whole bill.
|
|
29
|
+
*/
|
|
30
|
+
const priced = modelFrom(catalogue, model);
|
|
31
|
+
return {
|
|
32
|
+
label: label === UNLABELLED ? null : label,
|
|
33
|
+
model,
|
|
34
|
+
calls: breakdown.calls,
|
|
35
|
+
inputTokens: breakdown.inputTokens,
|
|
36
|
+
cacheReadTokens: breakdown.cacheReadTokens,
|
|
37
|
+
cacheWriteTokens: breakdown.cacheWriteTokens,
|
|
38
|
+
cacheWrite5mTokens: breakdown.cacheWrite5mTokens,
|
|
39
|
+
cacheWrite1hTokens: breakdown.cacheWrite1hTokens,
|
|
40
|
+
outputTokens: breakdown.outputTokens,
|
|
41
|
+
usd: breakdown.totalUsd,
|
|
42
|
+
/*
|
|
43
|
+
* Copied from the breakdown rather than recomputed from the rates below.
|
|
44
|
+
* Recomputing would reintroduce the defect this field exists to fix: the
|
|
45
|
+
* published rates are list rates, and a promotion, a long-context tier or
|
|
46
|
+
* any cached token is billed at something else.
|
|
47
|
+
*/
|
|
48
|
+
money: {
|
|
49
|
+
inputUsd: breakdown.inputUsd,
|
|
50
|
+
cacheReadUsd: breakdown.cacheReadUsd,
|
|
51
|
+
cacheWriteUsd: breakdown.cacheWriteUsd,
|
|
52
|
+
outputUsd: breakdown.outputUsd,
|
|
53
|
+
},
|
|
54
|
+
pricing: {
|
|
55
|
+
provider: priced.provider ?? 'unknown',
|
|
56
|
+
inputPerMTok: priced.inputPerMTok,
|
|
57
|
+
outputPerMTok: priced.outputPerMTok,
|
|
58
|
+
reviewedOn: PROVIDER_REVIEWED[priced.provider ?? ''] ?? null,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Build a receipt from a profile the caller already computed.
|
|
64
|
+
*
|
|
65
|
+
* Takes the report rather than the log text, and that is the guarantee rather
|
|
66
|
+
* than an ergonomic choice: `UsageProfileReport` has no field that can hold
|
|
67
|
+
* prompt content, so this function could not emit any even if it tried. The
|
|
68
|
+
* narrow input is what makes the promise checkable instead of merely stated.
|
|
69
|
+
*/
|
|
70
|
+
export function receiptFrom(report, catalogue, options = {}) {
|
|
71
|
+
const lines = report.byLabelAndModel.map((slice) => lineFrom(slice.label, slice.model, slice.breakdown, catalogue));
|
|
72
|
+
const gaps = [];
|
|
73
|
+
if (report.unpricedModels.length > 0) {
|
|
74
|
+
gaps.push({
|
|
75
|
+
kind: 'unpriced',
|
|
76
|
+
models: [...report.unpricedModels],
|
|
77
|
+
calls: report.unpriced.calls,
|
|
78
|
+
inputTokens: report.unpriced.inputTokens,
|
|
79
|
+
outputTokens: report.unpriced.outputTokens,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
if (report.skippedLines.length > 0) {
|
|
83
|
+
gaps.push({ kind: 'unread-lines', count: report.skippedLines.length });
|
|
84
|
+
}
|
|
85
|
+
if (report.span === null)
|
|
86
|
+
gaps.push({ kind: 'no-clock' });
|
|
87
|
+
/*
|
|
88
|
+
* Summed across the lines rather than read off a total, because the profile
|
|
89
|
+
* keeps this per slice and a receipt reports it once for the document.
|
|
90
|
+
*/
|
|
91
|
+
const assumed = report.byLabelAndModel.reduce((sum, slice) => sum + slice.breakdown.assumedWriteTtlCalls, 0);
|
|
92
|
+
if (assumed > 0)
|
|
93
|
+
gaps.push({ kind: 'assumed-write-ttl', calls: assumed });
|
|
94
|
+
return {
|
|
95
|
+
schemaVersion: 1,
|
|
96
|
+
emittedAt: options.emittedAt?.toISOString() ?? null,
|
|
97
|
+
span: report.span === null ? null : { ...report.span },
|
|
98
|
+
counting: 'counted',
|
|
99
|
+
lines,
|
|
100
|
+
/*
|
|
101
|
+
* `report.total` already excludes what could not be priced — the profile
|
|
102
|
+
* keeps those in `unpriced` precisely so a total is never tokens from one
|
|
103
|
+
* set of calls divided by dollars from another. The receipt inherits that
|
|
104
|
+
* separation rather than re-deriving it, and `gaps` carries the size of
|
|
105
|
+
* what is missing.
|
|
106
|
+
*/
|
|
107
|
+
total: { calls: report.total.calls, usd: report.total.totalUsd },
|
|
108
|
+
gaps,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=receipt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"receipt.js","sourceRoot":"","sources":["../src/receipt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE5D,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AA2NxC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC/C,OAAO;IACP,OAAO;IACP,OAAO;IACP,aAAa;IACb,iBAAiB;IACjB,kBAAkB;IAClB,oBAAoB;IACpB,oBAAoB;IACpB,cAAc;IACd,KAAK;IACL,OAAO;IACP,SAAS;CACD,CAAC,CAAC;AAEZ,MAAM,QAAQ,GAAG,CACf,KAAa,EACb,KAAa,EACb,SAAyB,EACzB,SAA2B,EACd,EAAE;IACf;;;;OAIG;IACH,MAAM,MAA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,EAAE,SAAS;QACnB,KAAK;QACL;;;;;;WAMG;QACH,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE;QAChE,IAAI;KACL,CAAC;AACJ,CAAC"}
|
package/dist/schemas.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AA6CjD;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AA6CjD;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAwI1E,CAAC;AAEF,6EAA6E;AAC7E,wBAAgB,cAAc,CAAC,IAAI,EAAE,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAE1E"}
|
package/dist/schemas.js
CHANGED
|
@@ -145,6 +145,42 @@ export const CONTRACT_SCHEMAS = {
|
|
|
145
145
|
cannotSay: array,
|
|
146
146
|
unpricedRecords: number,
|
|
147
147
|
}),
|
|
148
|
+
'routing-measurement': doc('routing-measurement', {
|
|
149
|
+
slice: object,
|
|
150
|
+
evaluation: object,
|
|
151
|
+
}),
|
|
152
|
+
'example-pruning': doc('example-pruning', {
|
|
153
|
+
provider: string,
|
|
154
|
+
model: string,
|
|
155
|
+
selfAgreement: number,
|
|
156
|
+
recoverableTokens: number,
|
|
157
|
+
callsMade: number,
|
|
158
|
+
contributions: array,
|
|
159
|
+
}),
|
|
160
|
+
'store-inventory': doc('store-inventory', {
|
|
161
|
+
providers: array,
|
|
162
|
+
totalRecords: number,
|
|
163
|
+
span: objectOrNull,
|
|
164
|
+
possiblyDouble: number,
|
|
165
|
+
unknownVersion: number,
|
|
166
|
+
totalUsd: number,
|
|
167
|
+
unreadable: array,
|
|
168
|
+
}),
|
|
169
|
+
conformance: doc('conformance', {
|
|
170
|
+
contract: stringOrNull,
|
|
171
|
+
because: stringOrNull,
|
|
172
|
+
problems: array,
|
|
173
|
+
unavailable: array,
|
|
174
|
+
records: { type: ['number', 'null'] },
|
|
175
|
+
conforms: boolean,
|
|
176
|
+
}),
|
|
177
|
+
'watch-cycle': doc('watch-cycle', {
|
|
178
|
+
firedAtMs: number,
|
|
179
|
+
crossings: array,
|
|
180
|
+
suppressed: array,
|
|
181
|
+
abstentions: array,
|
|
182
|
+
gap: objectOrNull,
|
|
183
|
+
}),
|
|
148
184
|
};
|
|
149
185
|
/** The schema for one named contract — the object `trazum schema` prints. */
|
|
150
186
|
export function contractSchema(name) {
|
package/dist/schemas.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemas.js","sourceRoot":"","sources":["../src/schemas.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,KAAK,GAAG,8CAA8C,CAAC;AAE7D,4DAA4D;AAC5D,SAAS,GAAG,CAAC,IAAkB,EAAE,UAAmC;IAClE,OAAO;QACL,OAAO,EAAE,KAAK;QACd,GAAG,EAAE,8CAA8C,IAAI,UAAU;QACjE,KAAK,EAAE,UAAU,IAAI,4BAA4B;QACjD,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,eAAe,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACvD,UAAU,EAAE;YACV,aAAa,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;YAC3B,GAAG,UAA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AAE;QAC9B,QAAQ,EAAE,YAAY;QACtB,OAAO,EAAE,YAAY;QACrB,QAAQ,EAAE,KAAK;QACf,WAAW,EAAE,KAAK;QAClB,OAAO,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE;QACrC,QAAQ,EAAE,OAAO;KAClB,CAAC;IACF,aAAa,EAAE,GAAG,CAAC,aAAa,EAAE;QAChC,SAAS,EAAE,MAAM;QACjB,SAAS,EAAE,KAAK;QAChB,UAAU,EAAE,KAAK;QACjB,WAAW,EAAE,KAAK;QAClB,GAAG,EAAE,YAAY;KAClB,CAAC;CACH,CAAC;AAEF,6EAA6E;AAC7E,MAAM,UAAU,cAAc,CAAC,IAAkB;IAC/C,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC"}
|
package/dist/tokenizer.d.ts
CHANGED
|
@@ -2,71 +2,67 @@
|
|
|
2
2
|
* Dependency-free token estimator.
|
|
3
3
|
*
|
|
4
4
|
* This is NOT a real tokenizer: it is a heuristic calibrated per character
|
|
5
|
-
* class
|
|
6
|
-
*
|
|
7
|
-
* two versions of the same prompt — but do NOT bill anyone from it.
|
|
5
|
+
* class, built for comparing two versions of the same prompt — but do NOT bill
|
|
6
|
+
* anyone from it.
|
|
8
7
|
*
|
|
9
|
-
* **
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
8
|
+
* **Its accuracy is not one number, and the years it was written as one were
|
|
9
|
+
* the mistake.** The branches below treat CJK, digits and punctuation quite
|
|
10
|
+
* differently from words, and there was never a reason those should land on the
|
|
11
|
+
* same accuracy. Measured over 47 samples against the official counting
|
|
12
|
+
* endpoint they do not: the worst error is 3.2% on CJK, 5.6% on Latin prose,
|
|
13
|
+
* 25.1% on code and quoting, and 32.5% on a digit-dominant table. `band.ts`
|
|
14
|
+
* holds the bands those measurements earn and `bandFor(text)` is what a caller
|
|
15
|
+
* should print; the constant below is only the widest of them, for callers that
|
|
16
|
+
* have no text in hand.
|
|
16
17
|
*
|
|
17
|
-
* `test/token-band.test.js`
|
|
18
|
-
*
|
|
18
|
+
* `test/token-band.test.js` holds every sample against the band `bandFor` gives
|
|
19
|
+
* it, and a sample edited since it was measured fails rather than passing
|
|
20
|
+
* quietly.
|
|
19
21
|
*
|
|
20
22
|
* For exact numbers use `countTokensAnthropic` (the official token-counting
|
|
21
23
|
* endpoint, which is free) or pass your own `TokenCounter`.
|
|
22
24
|
*/
|
|
23
25
|
/**
|
|
24
|
-
* The
|
|
26
|
+
* The widest band this estimator is published under, as a percentage.
|
|
25
27
|
*
|
|
26
|
-
* **
|
|
27
|
-
* releases as a design target nobody had checked; the first
|
|
28
|
-
* of eight samples outside it and it went to `25`; fixing
|
|
29
|
-
* calibrating per language brought it back to `15
|
|
30
|
-
*
|
|
28
|
+
* **A history worth keeping, because each value was believed at the time.** It
|
|
29
|
+
* was `15` for eight releases as a design target nobody had checked; the first
|
|
30
|
+
* measurement found two of eight samples outside it and it went to `25`; fixing
|
|
31
|
+
* the digit divisor and calibrating per language brought it back to `15`, and
|
|
32
|
+
* splitting kana from han — Japanese had been +11.2% and Chinese −3.2% under one
|
|
33
|
+
* rule — brought it to `10`. Four values, each an improvement, and all four
|
|
34
|
+
* wrong in the same way.
|
|
31
35
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
+
* **The corpus they were measured on was the corpus they were fitted to.**
|
|
37
|
+
* Twenty-one samples held thirteen files of Latin prose and exactly one each of
|
|
38
|
+
* code, numeric and punctuation, and those single files were where the constants
|
|
39
|
+
* came from. The worst error it could show was 6.4%, and 10 looked like a
|
|
40
|
+
* comfortable margin over it. Neither figure was about the estimator.
|
|
36
41
|
*
|
|
37
|
-
* **
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* safe direction for a tool that reports money.
|
|
42
|
+
* **It is no longer one number, and this one is the widest.** A corpus of
|
|
43
|
+
* thirteen prose files and one each of code, numeric and punctuation put the
|
|
44
|
+
* band at 10, and those single files were the set the constants had been fitted
|
|
45
|
+
* to. Measured on forty-seven, the same estimator is 6% out on prose and 33%
|
|
46
|
+
* out on a CSV ledger. `bandFor` in `band.ts` is what a caller that has the
|
|
47
|
+
* text should use.
|
|
44
48
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* **Which samples the band rests on matters more than the number.** Eight of the
|
|
52
|
-
* twenty-one had a constant fitted to them — seven Latin divisors and the digit
|
|
53
|
-
* divisor — so their residuals are optimistic by construction. The two worst
|
|
54
|
-
* errors in the corpus, `code-heavy` at 6.4% and `punctuation-heavy` at 5.7%, are
|
|
55
|
-
* fitted to nothing at all, and they are what sets this figure.
|
|
56
|
-
*
|
|
57
|
-
* Exported so every report, README and tool description reads the same number. It
|
|
58
|
-
* was a literal in twenty-four files before this, with the only machine-readable
|
|
59
|
-
* copy in a test, and `token-band.test.js` now fails any file that states a
|
|
60
|
-
* different one.
|
|
49
|
+
* This stays for the callers that do not have it — a figure covering the whole
|
|
50
|
+
* catalogue of text, which can only be the worst of them. Widening it from 10
|
|
51
|
+
* to 33 makes every claim that reads it true where it had been false, at the
|
|
52
|
+
* cost of understating the estimator on prose. That trade is deliberate: a
|
|
53
|
+
* caller with no text cannot know which it is holding, and there is only one
|
|
54
|
+
* safe direction to guess in.
|
|
61
55
|
*/
|
|
62
|
-
export declare const ESTIMATE_ERROR_BAND_PCT =
|
|
56
|
+
export declare const ESTIMATE_ERROR_BAND_PCT = 33;
|
|
63
57
|
/**
|
|
64
58
|
* Whose tokenizer that band was measured against.
|
|
65
59
|
*
|
|
66
60
|
* The number above is not a property of the estimator; it is a property of the
|
|
67
|
-
* estimator **against Claude**.
|
|
68
|
-
* family. Trazum prices seven, and
|
|
69
|
-
*
|
|
61
|
+
* estimator **against Claude**. Forty-seven samples, one counting endpoint, one
|
|
62
|
+
* family. Trazum prices seven, and the two that have since been run are far
|
|
63
|
+
* outside anything published here: DeepSeek 94.5% at worst, Mistral 103.1%. On
|
|
64
|
+
* a GPT or a Gemini prompt nobody has measured it at all, and
|
|
65
|
+
* `measuredForeignError` answers null rather than the nearest number.
|
|
70
66
|
*
|
|
71
67
|
* Exported as a name rather than left implicit in a comment because the band
|
|
72
68
|
* had already leaked out of its domain in three places: an advisory that told a
|
package/dist/tokenizer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../src/tokenizer.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"tokenizer.d.ts","sourceRoot":"","sources":["../src/tokenizer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAKH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAE1C;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,wBAAwB,cAAc,CAAC;AAEpD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,CAExE;AA+HD;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiJnD;AAED,sDAAsD;AACtD,MAAM,MAAM,iBAAiB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;AAElE,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;IACzB,gFAAgF;IAChF,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,uBAAuB,GAAG,iBAAiB,CA6BxF"}
|