@openrouter/ai-sdk-provider 2.2.2 → 2.2.4

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/dist/index.js CHANGED
@@ -2329,6 +2329,46 @@ var OpenRouterProviderOptionsSchema = import_v43.z.object({
2329
2329
  }).optional()
2330
2330
  }).optional();
2331
2331
 
2332
+ // src/utils/compute-token-usage.ts
2333
+ function computeTokenUsage(usage) {
2334
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2335
+ const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
2336
+ const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
2337
+ const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
2338
+ const cacheWriteTokens = (_f = (_e = usage.prompt_tokens_details) == null ? void 0 : _e.cache_write_tokens) != null ? _f : void 0;
2339
+ const reasoningTokens = (_h = (_g = usage.completion_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : 0;
2340
+ return {
2341
+ inputTokens: {
2342
+ total: promptTokens,
2343
+ noCache: promptTokens - cacheReadTokens,
2344
+ cacheRead: cacheReadTokens,
2345
+ cacheWrite: cacheWriteTokens
2346
+ },
2347
+ outputTokens: {
2348
+ total: completionTokens,
2349
+ text: completionTokens - reasoningTokens,
2350
+ reasoning: reasoningTokens
2351
+ },
2352
+ raw: usage
2353
+ };
2354
+ }
2355
+ function emptyUsage() {
2356
+ return {
2357
+ inputTokens: {
2358
+ total: 0,
2359
+ noCache: void 0,
2360
+ cacheRead: void 0,
2361
+ cacheWrite: void 0
2362
+ },
2363
+ outputTokens: {
2364
+ total: 0,
2365
+ text: void 0,
2366
+ reasoning: void 0
2367
+ },
2368
+ raw: void 0
2369
+ };
2370
+ }
2371
+
2332
2372
  // src/utils/map-finish-reason.ts
2333
2373
  function mapToUnified(finishReason) {
2334
2374
  switch (finishReason) {
@@ -2835,7 +2875,8 @@ var OpenRouterChatCompletionBaseResponseSchema = import_v46.z.object({
2835
2875
  usage: import_v46.z.object({
2836
2876
  prompt_tokens: import_v46.z.number(),
2837
2877
  prompt_tokens_details: import_v46.z.object({
2838
- cached_tokens: import_v46.z.number()
2878
+ cached_tokens: import_v46.z.number(),
2879
+ cache_write_tokens: import_v46.z.number().nullish()
2839
2880
  }).passthrough().nullish(),
2840
2881
  completion_tokens: import_v46.z.number(),
2841
2882
  completion_tokens_details: import_v46.z.object({
@@ -3110,7 +3151,7 @@ var OpenRouterChatLanguageModel = class {
3110
3151
  return baseArgs;
3111
3152
  }
3112
3153
  async doGenerate(options) {
3113
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
3154
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3114
3155
  const providerOptions = options.providerOptions || {};
3115
3156
  const openrouterOptions = providerOptions.openrouter || {};
3116
3157
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3149,34 +3190,8 @@ var OpenRouterChatLanguageModel = class {
3149
3190
  message: "No choice in response"
3150
3191
  });
3151
3192
  }
3152
- const usageInfo = response.usage ? {
3153
- inputTokens: {
3154
- total: (_a16 = response.usage.prompt_tokens) != null ? _a16 : 0,
3155
- noCache: void 0,
3156
- cacheRead: (_c = (_b16 = response.usage.prompt_tokens_details) == null ? void 0 : _b16.cached_tokens) != null ? _c : void 0,
3157
- cacheWrite: void 0
3158
- },
3159
- outputTokens: {
3160
- total: (_d = response.usage.completion_tokens) != null ? _d : 0,
3161
- text: void 0,
3162
- reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
3163
- },
3164
- raw: response.usage
3165
- } : {
3166
- inputTokens: {
3167
- total: 0,
3168
- noCache: void 0,
3169
- cacheRead: void 0,
3170
- cacheWrite: void 0
3171
- },
3172
- outputTokens: {
3173
- total: 0,
3174
- text: void 0,
3175
- reasoning: void 0
3176
- },
3177
- raw: void 0
3178
- };
3179
- const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
3193
+ const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3194
+ const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3180
3195
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3181
3196
  switch (detail.type) {
3182
3197
  case "reasoning.text" /* Text */: {
@@ -3245,9 +3260,9 @@ var OpenRouterChatLanguageModel = class {
3245
3260
  for (const toolCall of choice.message.tool_calls) {
3246
3261
  content.push({
3247
3262
  type: "tool-call",
3248
- toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3263
+ toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3249
3264
  toolName: toolCall.function.name,
3250
- input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3265
+ input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3251
3266
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3252
3267
  openrouter: {
3253
3268
  reasoning_details: reasoningDetails
@@ -3274,19 +3289,19 @@ var OpenRouterChatLanguageModel = class {
3274
3289
  sourceType: "url",
3275
3290
  id: annotation.url_citation.url,
3276
3291
  url: annotation.url_citation.url,
3277
- title: (_j = annotation.url_citation.title) != null ? _j : "",
3292
+ title: (_d = annotation.url_citation.title) != null ? _d : "",
3278
3293
  providerMetadata: {
3279
3294
  openrouter: {
3280
- content: (_k = annotation.url_citation.content) != null ? _k : "",
3281
- startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
3282
- endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
3295
+ content: (_e = annotation.url_citation.content) != null ? _e : "",
3296
+ startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3297
+ endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3283
3298
  }
3284
3299
  }
3285
3300
  });
3286
3301
  }
3287
3302
  }
3288
3303
  }
3289
- const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
3304
+ const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3290
3305
  (a) => a.type === "file"
3291
3306
  );
3292
3307
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3294,7 +3309,7 @@ var OpenRouterChatLanguageModel = class {
3294
3309
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3295
3310
  );
3296
3311
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3297
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3312
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3298
3313
  return {
3299
3314
  content,
3300
3315
  finishReason: effectiveFinishReason,
@@ -3302,22 +3317,22 @@ var OpenRouterChatLanguageModel = class {
3302
3317
  warnings: [],
3303
3318
  providerMetadata: {
3304
3319
  openrouter: OpenRouterProviderMetadataSchema.parse({
3305
- provider: (_p = response.provider) != null ? _p : "",
3306
- reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
3320
+ provider: (_j = response.provider) != null ? _j : "",
3321
+ reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3307
3322
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3308
3323
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3309
- promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
3310
- completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
3311
- totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
3312
- }, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
3324
+ promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3325
+ completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3326
+ totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3327
+ }, ((_p = response.usage) == null ? void 0 : _p.cost) != null ? { cost: response.usage.cost } : {}), ((_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? {
3313
3328
  promptTokensDetails: {
3314
3329
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3315
3330
  }
3316
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3331
+ } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3317
3332
  completionTokensDetails: {
3318
3333
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3319
3334
  }
3320
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3335
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3321
3336
  costDetails: {
3322
3337
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3323
3338
  }
@@ -3388,7 +3403,7 @@ var OpenRouterChatLanguageModel = class {
3388
3403
  stream: response.pipeThrough(
3389
3404
  new TransformStream({
3390
3405
  transform(chunk, controller) {
3391
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
3406
+ var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3392
3407
  if (options.includeRawChunks) {
3393
3408
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3394
3409
  }
@@ -3420,30 +3435,29 @@ var OpenRouterChatLanguageModel = class {
3420
3435
  });
3421
3436
  }
3422
3437
  if (value.usage != null) {
3423
- usage.inputTokens.total = value.usage.prompt_tokens;
3424
- usage.outputTokens.total = value.usage.completion_tokens;
3438
+ const computed = computeTokenUsage(value.usage);
3439
+ Object.assign(usage.inputTokens, computed.inputTokens);
3440
+ Object.assign(usage.outputTokens, computed.outputTokens);
3425
3441
  rawUsage = value.usage;
3426
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
3442
+ const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3443
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3444
+ openrouterUsage.promptTokens = promptTokens;
3427
3445
  if (value.usage.prompt_tokens_details) {
3428
- const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
3429
- usage.inputTokens.cacheRead = cachedInputTokens;
3430
3446
  openrouterUsage.promptTokensDetails = {
3431
- cachedTokens: cachedInputTokens
3447
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
3432
3448
  };
3433
3449
  }
3434
- openrouterUsage.completionTokens = value.usage.completion_tokens;
3450
+ openrouterUsage.completionTokens = completionTokens;
3435
3451
  if (value.usage.completion_tokens_details) {
3436
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
3437
- usage.outputTokens.reasoning = reasoningTokens;
3438
3452
  openrouterUsage.completionTokensDetails = {
3439
- reasoningTokens
3453
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
3440
3454
  };
3441
3455
  }
3442
3456
  if (value.usage.cost != null) {
3443
3457
  openrouterUsage.cost = value.usage.cost;
3444
3458
  }
3445
3459
  openrouterUsage.totalTokens = value.usage.total_tokens;
3446
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3460
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
3447
3461
  if (upstreamInferenceCost != null) {
3448
3462
  openrouterUsage.costDetails = {
3449
3463
  upstreamInferenceCost
@@ -3492,15 +3506,13 @@ var OpenRouterChatLanguageModel = class {
3492
3506
  }
3493
3507
  const reasoningMetadata = {
3494
3508
  openrouter: {
3495
- reasoning_details: delta.reasoning_details
3509
+ reasoning_details: accumulatedReasoningDetails.map((d) => __spreadValues({}, d))
3496
3510
  }
3497
3511
  };
3498
3512
  for (const detail of delta.reasoning_details) {
3499
3513
  switch (detail.type) {
3500
3514
  case "reasoning.text" /* Text */: {
3501
- if (detail.text) {
3502
- emitReasoningChunk(detail.text, reasoningMetadata);
3503
- }
3515
+ emitReasoningChunk(detail.text || "", reasoningMetadata);
3504
3516
  break;
3505
3517
  }
3506
3518
  case "reasoning.encrypted" /* Encrypted */: {
@@ -3563,12 +3575,12 @@ var OpenRouterChatLanguageModel = class {
3563
3575
  sourceType: "url",
3564
3576
  id: annotation.url_citation.url,
3565
3577
  url: annotation.url_citation.url,
3566
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3578
+ title: (_f = annotation.url_citation.title) != null ? _f : "",
3567
3579
  providerMetadata: {
3568
3580
  openrouter: {
3569
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3570
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3571
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3581
+ content: (_g = annotation.url_citation.content) != null ? _g : "",
3582
+ startIndex: (_h = annotation.url_citation.start_index) != null ? _h : 0,
3583
+ endIndex: (_i = annotation.url_citation.end_index) != null ? _i : 0
3572
3584
  }
3573
3585
  }
3574
3586
  });
@@ -3584,7 +3596,7 @@ var OpenRouterChatLanguageModel = class {
3584
3596
  }
3585
3597
  if (delta.tool_calls != null) {
3586
3598
  for (const toolCallDelta of delta.tool_calls) {
3587
- const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3599
+ const index = (_j = toolCallDelta.index) != null ? _j : toolCalls.length - 1;
3588
3600
  if (toolCalls[index] == null) {
3589
3601
  if (toolCallDelta.type !== "function") {
3590
3602
  throw new InvalidResponseDataError({
@@ -3598,7 +3610,7 @@ var OpenRouterChatLanguageModel = class {
3598
3610
  message: `Expected 'id' to be a string.`
3599
3611
  });
3600
3612
  }
3601
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3613
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.name) == null) {
3602
3614
  throw new InvalidResponseDataError({
3603
3615
  data: toolCallDelta,
3604
3616
  message: `Expected 'function.name' to be a string.`
@@ -3609,7 +3621,7 @@ var OpenRouterChatLanguageModel = class {
3609
3621
  type: "function",
3610
3622
  function: {
3611
3623
  name: toolCallDelta.function.name,
3612
- arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3624
+ arguments: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3613
3625
  },
3614
3626
  inputStarted: false,
3615
3627
  sent: false
@@ -3621,7 +3633,7 @@ var OpenRouterChatLanguageModel = class {
3621
3633
  message: `Tool call at index ${index} is missing after creation.`
3622
3634
  });
3623
3635
  }
3624
- if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3636
+ if (((_m = toolCall2.function) == null ? void 0 : _m.name) != null && ((_n = toolCall2.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3625
3637
  toolCall2.inputStarted = true;
3626
3638
  controller.enqueue({
3627
3639
  type: "tool-input-start",
@@ -3672,18 +3684,18 @@ var OpenRouterChatLanguageModel = class {
3672
3684
  toolName: toolCall.function.name
3673
3685
  });
3674
3686
  }
3675
- if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
3676
- toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
3687
+ if (((_o = toolCallDelta.function) == null ? void 0 : _o.arguments) != null) {
3688
+ toolCall.function.arguments += (_q = (_p = toolCallDelta.function) == null ? void 0 : _p.arguments) != null ? _q : "";
3677
3689
  }
3678
3690
  controller.enqueue({
3679
3691
  type: "tool-input-delta",
3680
3692
  id: toolCall.id,
3681
- delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3693
+ delta: (_r = toolCallDelta.function.arguments) != null ? _r : ""
3682
3694
  });
3683
- if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3695
+ if (((_s = toolCall.function) == null ? void 0 : _s.name) != null && ((_t = toolCall.function) == null ? void 0 : _t.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3684
3696
  controller.enqueue({
3685
3697
  type: "tool-call",
3686
- toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3698
+ toolCallId: (_u = toolCall.id) != null ? _u : generateId(),
3687
3699
  toolName: toolCall.function.name,
3688
3700
  input: toolCall.function.arguments,
3689
3701
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3913,7 +3925,8 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
3913
3925
  usage: import_v47.z.object({
3914
3926
  prompt_tokens: import_v47.z.number(),
3915
3927
  prompt_tokens_details: import_v47.z.object({
3916
- cached_tokens: import_v47.z.number()
3928
+ cached_tokens: import_v47.z.number(),
3929
+ cache_write_tokens: import_v47.z.number().nullish()
3917
3930
  }).passthrough().nullish(),
3918
3931
  completion_tokens: import_v47.z.number(),
3919
3932
  completion_tokens_details: import_v47.z.object({
@@ -4003,7 +4016,7 @@ var OpenRouterCompletionLanguageModel = class {
4003
4016
  }, this.config.extraBody), this.settings.extraBody);
4004
4017
  }
4005
4018
  async doGenerate(options) {
4006
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
4019
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
4007
4020
  const providerOptions = options.providerOptions || {};
4008
4021
  const openrouterOptions = providerOptions.openrouter || {};
4009
4022
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -4049,37 +4062,24 @@ var OpenRouterCompletionLanguageModel = class {
4049
4062
  }
4050
4063
  ],
4051
4064
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
4052
- usage: {
4053
- inputTokens: {
4054
- total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
4055
- noCache: void 0,
4056
- cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
4057
- cacheWrite: void 0
4058
- },
4059
- outputTokens: {
4060
- total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
4061
- text: void 0,
4062
- reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
4063
- },
4064
- raw: (_l = response.usage) != null ? _l : void 0
4065
- },
4065
+ usage: response.usage ? computeTokenUsage(response.usage) : emptyUsage(),
4066
4066
  warnings: [],
4067
4067
  providerMetadata: {
4068
4068
  openrouter: OpenRouterProviderMetadataSchema.parse({
4069
- provider: (_m = response.provider) != null ? _m : "",
4069
+ provider: (_b16 = response.provider) != null ? _b16 : "",
4070
4070
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4071
- promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
4072
- completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
4073
- totalTokens: ((_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens) != null ? _s : 0) + ((_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens) != null ? _u : 0)
4074
- }, ((_v = response.usage) == null ? void 0 : _v.cost) != null ? { cost: response.usage.cost } : {}), ((_x = (_w = response.usage) == null ? void 0 : _w.prompt_tokens_details) == null ? void 0 : _x.cached_tokens) != null ? {
4071
+ promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
4072
+ completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0,
4073
+ totalTokens: ((_h = (_g = response.usage) == null ? void 0 : _g.prompt_tokens) != null ? _h : 0) + ((_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens) != null ? _j : 0)
4074
+ }, ((_k = response.usage) == null ? void 0 : _k.cost) != null ? { cost: response.usage.cost } : {}), ((_m = (_l = response.usage) == null ? void 0 : _l.prompt_tokens_details) == null ? void 0 : _m.cached_tokens) != null ? {
4075
4075
  promptTokensDetails: {
4076
4076
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
4077
4077
  }
4078
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
4078
+ } : {}), ((_o = (_n = response.usage) == null ? void 0 : _n.completion_tokens_details) == null ? void 0 : _o.reasoning_tokens) != null ? {
4079
4079
  completionTokensDetails: {
4080
4080
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
4081
4081
  }
4082
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
4082
+ } : {}), ((_q = (_p = response.usage) == null ? void 0 : _p.cost_details) == null ? void 0 : _q.upstream_inference_cost) != null ? {
4083
4083
  costDetails: {
4084
4084
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
4085
4085
  }
@@ -4135,7 +4135,7 @@ var OpenRouterCompletionLanguageModel = class {
4135
4135
  stream: response.pipeThrough(
4136
4136
  new TransformStream({
4137
4137
  transform(chunk, controller) {
4138
- var _a16, _b16, _c;
4138
+ var _a16, _b16, _c, _d, _e;
4139
4139
  if (options.includeRawChunks) {
4140
4140
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4141
4141
  }
@@ -4154,30 +4154,29 @@ var OpenRouterCompletionLanguageModel = class {
4154
4154
  provider = value.provider;
4155
4155
  }
4156
4156
  if (value.usage != null) {
4157
- usage.inputTokens.total = value.usage.prompt_tokens;
4158
- usage.outputTokens.total = value.usage.completion_tokens;
4157
+ const computed = computeTokenUsage(value.usage);
4158
+ Object.assign(usage.inputTokens, computed.inputTokens);
4159
+ Object.assign(usage.outputTokens, computed.outputTokens);
4159
4160
  rawUsage = value.usage;
4160
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
4161
+ const promptTokens = (_a16 = value.usage.prompt_tokens) != null ? _a16 : 0;
4162
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
4163
+ openrouterUsage.promptTokens = promptTokens;
4161
4164
  if (value.usage.prompt_tokens_details) {
4162
- const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
4163
- usage.inputTokens.cacheRead = cachedInputTokens;
4164
4165
  openrouterUsage.promptTokensDetails = {
4165
- cachedTokens: cachedInputTokens
4166
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
4166
4167
  };
4167
4168
  }
4168
- openrouterUsage.completionTokens = value.usage.completion_tokens;
4169
+ openrouterUsage.completionTokens = completionTokens;
4169
4170
  if (value.usage.completion_tokens_details) {
4170
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
4171
- usage.outputTokens.reasoning = reasoningTokens;
4172
4171
  openrouterUsage.completionTokensDetails = {
4173
- reasoningTokens
4172
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
4174
4173
  };
4175
4174
  }
4176
4175
  if (value.usage.cost != null) {
4177
4176
  openrouterUsage.cost = value.usage.cost;
4178
4177
  }
4179
4178
  openrouterUsage.totalTokens = value.usage.total_tokens;
4180
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
4179
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
4181
4180
  if (upstreamInferenceCost != null) {
4182
4181
  openrouterUsage.costDetails = {
4183
4182
  upstreamInferenceCost
@@ -4557,7 +4556,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4557
4556
  }
4558
4557
 
4559
4558
  // src/version.ts
4560
- var VERSION2 = false ? "0.0.0-test" : "2.2.2";
4559
+ var VERSION2 = false ? "0.0.0-test" : "2.2.4";
4561
4560
 
4562
4561
  // src/provider.ts
4563
4562
  function createOpenRouter(options = {}) {