@trazum/cli 1.51.2 → 1.52.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trazum/cli",
3
- "version": "1.51.2",
3
+ "version": "1.52.0",
4
4
  "description": "Trazum CLI: find where your LLM bill goes, price every finding per month, and enforce token budgets in CI.",
5
5
  "license": "MIT",
6
6
  "author": "David Mu\u00f1oz Rey",
@@ -37,7 +37,7 @@
37
37
  "prepublishOnly": "npm run build && npm test"
38
38
  },
39
39
  "dependencies": {
40
- "@trazum/core": "1.51.2"
40
+ "@trazum/core": "1.52.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^26.2.0",
@@ -68,6 +68,9 @@ export const UPSTREAMS: Readonly<Record<string, { origin: string; path: string }
68
68
  * Headers Trazum adds or removes. Everything else the caller sent is forwarded
69
69
  * verbatim, including their credential, which this never reads.
70
70
  */
71
+ /** Why a forwarded call's cost could not be measured. */
72
+ export type UnmeasuredCause = 'stream-broke' | 'no-usage-event';
73
+
71
74
  const HOP_BY_HOP = new Set([
72
75
  'connection',
73
76
  'keep-alive',
@@ -103,6 +106,26 @@ export interface GatewayContext {
103
106
  cacheWriteTokens: number;
104
107
  substituted: boolean;
105
108
  }) => void;
109
+ /**
110
+ * A call that was forwarded and whose cost could not be measured.
111
+ *
112
+ * The money is spent either way — the provider generated what it generated —
113
+ * and the period's total will be lower than the bill by however much these
114
+ * came to. Naming them is the only honest option: a zero would be a
115
+ * measurement, and inventing an estimate would merge the two halves this
116
+ * product spent an arc separating.
117
+ *
118
+ * Two causes, and the second is not a failure at all:
119
+ *
120
+ * - `stream-broke` — the connection died before the event carrying the
121
+ * counts. Rare, and a real error.
122
+ * - `no-usage-event` — the stream simply carried no counts. On OpenAI that is
123
+ * **every streaming call** unless the caller passed `stream_options:
124
+ * {include_usage: true}`, so this is the common case rather than the
125
+ * exception, and an operator who is not told will read a total that is
126
+ * quietly missing most of their traffic.
127
+ */
128
+ unmeasured?: (cause: UnmeasuredCause) => void;
106
129
  /** A line for the operator's terminal. Never given a body, ever. */
107
130
  note: (line: string) => void;
108
131
  /** Injected so the proxy is testable against a stub upstream. */
@@ -378,11 +401,22 @@ export function buildGateway(context: GatewayContext): Server {
378
401
  context.note(
379
402
  `stream broke before its usage event: ${error instanceof Error ? error.message : String(error)} — this call is unmeasured`,
380
403
  );
404
+ context.unmeasured?.('stream-broke');
381
405
  response.destroy();
382
406
  return;
383
407
  }
384
408
 
385
- recordUsage(reader.done());
409
+ const streamed = reader.done();
410
+ if (streamed === null) {
411
+ /**
412
+ * The stream ended cleanly and carried no counts. Not an error, and on
413
+ * OpenAI not even unusual — but the call is still unmeasured, and the
414
+ * operator has to hear it from here rather than infer it from a total
415
+ * that looks too small.
416
+ */
417
+ context.unmeasured?.('no-usage-event');
418
+ }
419
+ recordUsage(streamed);
386
420
  response.end();
387
421
  })();
388
422
  });
package/src/i18n/en.ts CHANGED
@@ -1265,6 +1265,10 @@ ${bold('EXAMPLES')}
1265
1265
  needsPolicy: (policies) =>
1266
1266
  `--on-cannot-tell is required, and there is no default: ${policies}. When the gateway cannot judge a call — no budget, nothing measured, an unpriced model — one of these happens, and only you know which failure your product can survive. fail-open keeps it working and lets the bill run; fail-closed stops the bill and takes it down with it. Picking one for you would be the most consequential decision in your architecture, made silently at install time.`,
1267
1267
  listening: (where, provider) => `Gateway on ${where}, in front of ${provider}`,
1268
+ unmeasured: (cause, sofar) =>
1269
+ cause === 'stream-broke'
1270
+ ? `unmeasured: the stream broke before its usage event — the provider billed this call and this session cannot see it (${sofar} unmeasured so far)`
1271
+ : `unmeasured: the stream carried no usage event — on OpenAI that is every streaming call without stream_options.include_usage, so the total below is short by these (${sofar} unmeasured so far)`,
1268
1272
  pointYourSdk: (where) =>
1269
1273
  `Point your SDK's base URL at ${where} and change nothing else. It speaks the provider's own wire format, so no code changes and no new client.`,
1270
1274
  credential: () =>
package/src/i18n/es.ts CHANGED
@@ -1302,6 +1302,10 @@ ${bold('EJEMPLOS')}
1302
1302
  needsPolicy: (policies) =>
1303
1303
  `--on-cannot-tell es obligatorio, y no hay valor por defecto: ${policies}. Cuando la pasarela no puede juzgar una llamada — sin presupuesto, sin nada medido, un modelo sin precio — pasa una de las dos, y solo tú sabes qué fallo puede sobrevivir tu producto. fail-open lo mantiene funcionando y deja correr la factura; fail-closed para la factura y se lo lleva por delante. Elegir por ti sería tomar la decisión más consecuente de tu arquitectura, en silencio, al instalar.`,
1304
1304
  listening: (where, provider) => `Pasarela en ${where}, delante de ${provider}`,
1305
+ unmeasured: (cause, sofar) =>
1306
+ cause === 'stream-broke'
1307
+ ? `sin medir: el stream se rompió antes de su evento de uso — el proveedor facturó esta llamada y esta sesión no puede verla (${sofar} sin medir hasta ahora)`
1308
+ : `sin medir: el stream no llevó evento de uso — en OpenAI eso es toda llamada en streaming sin stream_options.include_usage, así que el total de abajo se queda corto (${sofar} sin medir hasta ahora)`,
1305
1309
  pointYourSdk: (where) =>
1306
1310
  `Apunta la URL base de tu SDK a ${where} y no cambies nada más. Habla el formato del propio proveedor, así que no hay cambios de código ni cliente nuevo.`,
1307
1311
  credential: () =>
package/src/i18n/types.ts CHANGED
@@ -379,6 +379,7 @@ export interface CliMessages {
379
379
  badProvider(given: string, known: string): string;
380
380
  needsPolicy(policies: string): string;
381
381
  listening(where: string, provider: string): string;
382
+ unmeasured(cause: 'stream-broke' | 'no-usage-event', sofar: number): string;
382
383
  pointYourSdk(where: string): string;
383
384
  credential(): string;
384
385
  neverSubstitutes(): string;
package/src/index.ts CHANGED
@@ -2178,6 +2178,14 @@ async function commandGateway(
2178
2178
  };
2179
2179
 
2180
2180
  const measured: { calls: number; usd: number } = { calls: 0, usd: 0 };
2181
+ /**
2182
+ * Forwarded calls whose cost this session cannot see.
2183
+ *
2184
+ * Counted separately and never folded into `measured`: the money was spent,
2185
+ * so the two are not interchangeable, and a total that quietly absorbed them
2186
+ * would be the flattering direction.
2187
+ */
2188
+ let unmeasured = 0;
2181
2189
  const server = buildGateway({
2182
2190
  provider,
2183
2191
  catalogue: pricing,
@@ -2200,6 +2208,10 @@ async function commandGateway(
2200
2208
  ),
2201
2209
  );
2202
2210
  },
2211
+ unmeasured: (cause) => {
2212
+ unmeasured += 1;
2213
+ console.error(c.yellow(` ${t.gateway.unmeasured(cause, unmeasured)}`));
2214
+ },
2203
2215
  note: (line) => {
2204
2216
  console.error(c.yellow(` ${line}`));
2205
2217
  },