@trazum/core 2.7.1 → 2.7.2

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/src/gateway.ts CHANGED
@@ -95,6 +95,12 @@ export interface GatewayCall {
95
95
  * not content. Absent or null prices the call at the standard rate.
96
96
  */
97
97
  speed?: string | null;
98
+ /**
99
+ * The service tier the request asked for, when it named one: OpenAI's
100
+ * `service_tier`, `"flex"` at about half the standard rate, `"priority"` or
101
+ * `"fast"` at up to twice, `"ultrafast"`. A setting, not content.
102
+ */
103
+ serviceTier?: string | null;
98
104
  }
99
105
 
100
106
  /** Where the budget stands, as the gateway was told at the last refresh. */
@@ -216,8 +222,11 @@ export interface GatewayOptions {
216
222
  waivers?: readonly WaiveEntry[];
217
223
  }
218
224
 
219
- const speedOf = (call: GatewayCall): { speed?: string } =>
220
- typeof call.speed === 'string' && call.speed !== '' ? { speed: call.speed } : {};
225
+ /** How the call asked to be run, where that changes its price: speed and service tier. */
226
+ const speedOf = (call: GatewayCall): { speed?: string; serviceTier?: string } => ({
227
+ ...(typeof call.speed === 'string' && call.speed !== '' ? { speed: call.speed } : {}),
228
+ ...(typeof call.serviceTier === 'string' && call.serviceTier !== '' ? { serviceTier: call.serviceTier } : {}),
229
+ });
221
230
 
222
231
  /** What this call costs at a model's rates, or null when it cannot be priced. */
223
232
  function priceCall(
@@ -501,6 +510,8 @@ export interface GatewayUsage {
501
510
  cacheWrite1hTokens?: number;
502
511
  /** Anthropic's `usage.speed`, when the response named it: fast mode bills at a multiple. */
503
512
  speed?: string;
513
+ /** OpenAI's `service_tier`, the tier that actually answered: each has its own table. */
514
+ serviceTier?: string;
504
515
  }
505
516
 
506
517
  /**
@@ -693,6 +704,8 @@ export function streamingUsageReader(
693
704
  }
694
705
 
695
706
  if (shape === 'openai') {
707
+ // Every chunk names the tier that answered; the last one before usage is as good as any.
708
+ if (typeof doc.service_tier === 'string' && doc.service_tier !== '') usage.serviceTier = doc.service_tier;
696
709
  const u = doc.usage;
697
710
  if (typeof u !== 'object' || u === null) return;
698
711
  const o = u as Record<string, unknown>;
@@ -806,6 +819,12 @@ export function usageFromResponse(
806
819
  cacheReadTokens: cached,
807
820
  // GPT-5.6 and later bill writes above input; taken out of the prompt the same way.
808
821
  ...openAiWrites(details),
822
+ // The tier that answered, which is not always the one asked for: a Fast
823
+ // request downgraded under load says "default" and is billed as such.
824
+ ...(typeof (body as { service_tier?: unknown }).service_tier === 'string'
825
+ && (body as { service_tier: string }).service_tier !== ''
826
+ ? { serviceTier: (body as { service_tier: string }).service_tier }
827
+ : {}),
809
828
  };
810
829
  }
811
830
  if (shape === 'google') {
package/src/index.ts CHANGED
@@ -443,6 +443,7 @@ export {
443
443
  cheapestOfTierIn,
444
444
  multipliersFor,
445
445
  isOffered,
446
+ serviceTierOf,
446
447
  } from './pricing.js';
447
448
  export type { PricingCatalogue, PromptSize } from './pricing.js';
448
449
  export type { CachingMode, Capability, CostMultipliers } from './types.js';