@openrouter/ai-sdk-provider 2.2.2 → 2.2.3

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.mjs CHANGED
@@ -2296,6 +2296,46 @@ var OpenRouterProviderOptionsSchema = z3.object({
2296
2296
  }).optional()
2297
2297
  }).optional();
2298
2298
 
2299
+ // src/utils/compute-token-usage.ts
2300
+ function computeTokenUsage(usage) {
2301
+ var _a16, _b16, _c, _d, _e, _f, _g, _h;
2302
+ const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
2303
+ const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
2304
+ const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
2305
+ const cacheWriteTokens = (_f = (_e = usage.prompt_tokens_details) == null ? void 0 : _e.cache_write_tokens) != null ? _f : void 0;
2306
+ const reasoningTokens = (_h = (_g = usage.completion_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : 0;
2307
+ return {
2308
+ inputTokens: {
2309
+ total: promptTokens,
2310
+ noCache: promptTokens - cacheReadTokens,
2311
+ cacheRead: cacheReadTokens,
2312
+ cacheWrite: cacheWriteTokens
2313
+ },
2314
+ outputTokens: {
2315
+ total: completionTokens,
2316
+ text: completionTokens - reasoningTokens,
2317
+ reasoning: reasoningTokens
2318
+ },
2319
+ raw: usage
2320
+ };
2321
+ }
2322
+ function emptyUsage() {
2323
+ return {
2324
+ inputTokens: {
2325
+ total: 0,
2326
+ noCache: void 0,
2327
+ cacheRead: void 0,
2328
+ cacheWrite: void 0
2329
+ },
2330
+ outputTokens: {
2331
+ total: 0,
2332
+ text: void 0,
2333
+ reasoning: void 0
2334
+ },
2335
+ raw: void 0
2336
+ };
2337
+ }
2338
+
2299
2339
  // src/utils/map-finish-reason.ts
2300
2340
  function mapToUnified(finishReason) {
2301
2341
  switch (finishReason) {
@@ -2802,7 +2842,8 @@ var OpenRouterChatCompletionBaseResponseSchema = z7.object({
2802
2842
  usage: z7.object({
2803
2843
  prompt_tokens: z7.number(),
2804
2844
  prompt_tokens_details: z7.object({
2805
- cached_tokens: z7.number()
2845
+ cached_tokens: z7.number(),
2846
+ cache_write_tokens: z7.number().nullish()
2806
2847
  }).passthrough().nullish(),
2807
2848
  completion_tokens: z7.number(),
2808
2849
  completion_tokens_details: z7.object({
@@ -3077,7 +3118,7 @@ var OpenRouterChatLanguageModel = class {
3077
3118
  return baseArgs;
3078
3119
  }
3079
3120
  async doGenerate(options) {
3080
- 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;
3121
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
3081
3122
  const providerOptions = options.providerOptions || {};
3082
3123
  const openrouterOptions = providerOptions.openrouter || {};
3083
3124
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -3116,34 +3157,8 @@ var OpenRouterChatLanguageModel = class {
3116
3157
  message: "No choice in response"
3117
3158
  });
3118
3159
  }
3119
- const usageInfo = response.usage ? {
3120
- inputTokens: {
3121
- total: (_a16 = response.usage.prompt_tokens) != null ? _a16 : 0,
3122
- noCache: void 0,
3123
- cacheRead: (_c = (_b16 = response.usage.prompt_tokens_details) == null ? void 0 : _b16.cached_tokens) != null ? _c : void 0,
3124
- cacheWrite: void 0
3125
- },
3126
- outputTokens: {
3127
- total: (_d = response.usage.completion_tokens) != null ? _d : 0,
3128
- text: void 0,
3129
- reasoning: (_f = (_e = response.usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0
3130
- },
3131
- raw: response.usage
3132
- } : {
3133
- inputTokens: {
3134
- total: 0,
3135
- noCache: void 0,
3136
- cacheRead: void 0,
3137
- cacheWrite: void 0
3138
- },
3139
- outputTokens: {
3140
- total: 0,
3141
- text: void 0,
3142
- reasoning: void 0
3143
- },
3144
- raw: void 0
3145
- };
3146
- const reasoningDetails = (_g = choice.message.reasoning_details) != null ? _g : [];
3160
+ const usageInfo = response.usage ? computeTokenUsage(response.usage) : emptyUsage();
3161
+ const reasoningDetails = (_a16 = choice.message.reasoning_details) != null ? _a16 : [];
3147
3162
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
3148
3163
  switch (detail.type) {
3149
3164
  case "reasoning.text" /* Text */: {
@@ -3212,9 +3227,9 @@ var OpenRouterChatLanguageModel = class {
3212
3227
  for (const toolCall of choice.message.tool_calls) {
3213
3228
  content.push({
3214
3229
  type: "tool-call",
3215
- toolCallId: (_h = toolCall.id) != null ? _h : generateId(),
3230
+ toolCallId: (_b16 = toolCall.id) != null ? _b16 : generateId(),
3216
3231
  toolName: toolCall.function.name,
3217
- input: (_i = toolCall.function.arguments) != null ? _i : "{}",
3232
+ input: (_c = toolCall.function.arguments) != null ? _c : "{}",
3218
3233
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
3219
3234
  openrouter: {
3220
3235
  reasoning_details: reasoningDetails
@@ -3241,19 +3256,19 @@ var OpenRouterChatLanguageModel = class {
3241
3256
  sourceType: "url",
3242
3257
  id: annotation.url_citation.url,
3243
3258
  url: annotation.url_citation.url,
3244
- title: (_j = annotation.url_citation.title) != null ? _j : "",
3259
+ title: (_d = annotation.url_citation.title) != null ? _d : "",
3245
3260
  providerMetadata: {
3246
3261
  openrouter: {
3247
- content: (_k = annotation.url_citation.content) != null ? _k : "",
3248
- startIndex: (_l = annotation.url_citation.start_index) != null ? _l : 0,
3249
- endIndex: (_m = annotation.url_citation.end_index) != null ? _m : 0
3262
+ content: (_e = annotation.url_citation.content) != null ? _e : "",
3263
+ startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3264
+ endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3250
3265
  }
3251
3266
  }
3252
3267
  });
3253
3268
  }
3254
3269
  }
3255
3270
  }
3256
- const fileAnnotations = (_n = choice.message.annotations) == null ? void 0 : _n.filter(
3271
+ const fileAnnotations = (_h = choice.message.annotations) == null ? void 0 : _h.filter(
3257
3272
  (a) => a.type === "file"
3258
3273
  );
3259
3274
  const hasToolCalls = choice.message.tool_calls && choice.message.tool_calls.length > 0;
@@ -3261,7 +3276,7 @@ var OpenRouterChatLanguageModel = class {
3261
3276
  (d) => d.type === "reasoning.encrypted" /* Encrypted */ && d.data
3262
3277
  );
3263
3278
  const shouldOverrideFinishReason = hasToolCalls && hasEncryptedReasoning && choice.finish_reason === "stop";
3264
- const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_o = choice.finish_reason) != null ? _o : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3279
+ const effectiveFinishReason = shouldOverrideFinishReason ? createFinishReason("tool-calls", (_i = choice.finish_reason) != null ? _i : void 0) : mapOpenRouterFinishReason(choice.finish_reason);
3265
3280
  return {
3266
3281
  content,
3267
3282
  finishReason: effectiveFinishReason,
@@ -3269,22 +3284,22 @@ var OpenRouterChatLanguageModel = class {
3269
3284
  warnings: [],
3270
3285
  providerMetadata: {
3271
3286
  openrouter: OpenRouterProviderMetadataSchema.parse({
3272
- provider: (_p = response.provider) != null ? _p : "",
3273
- reasoning_details: (_q = choice.message.reasoning_details) != null ? _q : [],
3287
+ provider: (_j = response.provider) != null ? _j : "",
3288
+ reasoning_details: (_k = choice.message.reasoning_details) != null ? _k : [],
3274
3289
  annotations: fileAnnotations && fileAnnotations.length > 0 ? fileAnnotations : void 0,
3275
3290
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
3276
- promptTokens: (_r = usageInfo.inputTokens.total) != null ? _r : 0,
3277
- completionTokens: (_s = usageInfo.outputTokens.total) != null ? _s : 0,
3278
- totalTokens: ((_t = usageInfo.inputTokens.total) != null ? _t : 0) + ((_u = usageInfo.outputTokens.total) != null ? _u : 0)
3279
- }, ((_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 ? {
3291
+ promptTokens: (_l = usageInfo.inputTokens.total) != null ? _l : 0,
3292
+ completionTokens: (_m = usageInfo.outputTokens.total) != null ? _m : 0,
3293
+ totalTokens: ((_n = usageInfo.inputTokens.total) != null ? _n : 0) + ((_o = usageInfo.outputTokens.total) != null ? _o : 0)
3294
+ }, ((_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 ? {
3280
3295
  promptTokensDetails: {
3281
3296
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
3282
3297
  }
3283
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
3298
+ } : {}), ((_t = (_s = response.usage) == null ? void 0 : _s.completion_tokens_details) == null ? void 0 : _t.reasoning_tokens) != null ? {
3284
3299
  completionTokensDetails: {
3285
3300
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
3286
3301
  }
3287
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
3302
+ } : {}), ((_v = (_u = response.usage) == null ? void 0 : _u.cost_details) == null ? void 0 : _v.upstream_inference_cost) != null ? {
3288
3303
  costDetails: {
3289
3304
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
3290
3305
  }
@@ -3355,7 +3370,7 @@ var OpenRouterChatLanguageModel = class {
3355
3370
  stream: response.pipeThrough(
3356
3371
  new TransformStream({
3357
3372
  transform(chunk, controller) {
3358
- var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
3373
+ var _a17, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
3359
3374
  if (options.includeRawChunks) {
3360
3375
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3361
3376
  }
@@ -3387,30 +3402,29 @@ var OpenRouterChatLanguageModel = class {
3387
3402
  });
3388
3403
  }
3389
3404
  if (value.usage != null) {
3390
- usage.inputTokens.total = value.usage.prompt_tokens;
3391
- usage.outputTokens.total = value.usage.completion_tokens;
3405
+ const computed = computeTokenUsage(value.usage);
3406
+ Object.assign(usage.inputTokens, computed.inputTokens);
3407
+ Object.assign(usage.outputTokens, computed.outputTokens);
3392
3408
  rawUsage = value.usage;
3393
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
3409
+ const promptTokens = (_a17 = value.usage.prompt_tokens) != null ? _a17 : 0;
3410
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
3411
+ openrouterUsage.promptTokens = promptTokens;
3394
3412
  if (value.usage.prompt_tokens_details) {
3395
- const cachedInputTokens = (_a17 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a17 : 0;
3396
- usage.inputTokens.cacheRead = cachedInputTokens;
3397
3413
  openrouterUsage.promptTokensDetails = {
3398
- cachedTokens: cachedInputTokens
3414
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
3399
3415
  };
3400
3416
  }
3401
- openrouterUsage.completionTokens = value.usage.completion_tokens;
3417
+ openrouterUsage.completionTokens = completionTokens;
3402
3418
  if (value.usage.completion_tokens_details) {
3403
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
3404
- usage.outputTokens.reasoning = reasoningTokens;
3405
3419
  openrouterUsage.completionTokensDetails = {
3406
- reasoningTokens
3420
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
3407
3421
  };
3408
3422
  }
3409
3423
  if (value.usage.cost != null) {
3410
3424
  openrouterUsage.cost = value.usage.cost;
3411
3425
  }
3412
3426
  openrouterUsage.totalTokens = value.usage.total_tokens;
3413
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
3427
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
3414
3428
  if (upstreamInferenceCost != null) {
3415
3429
  openrouterUsage.costDetails = {
3416
3430
  upstreamInferenceCost
@@ -3530,12 +3544,12 @@ var OpenRouterChatLanguageModel = class {
3530
3544
  sourceType: "url",
3531
3545
  id: annotation.url_citation.url,
3532
3546
  url: annotation.url_citation.url,
3533
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3547
+ title: (_f = annotation.url_citation.title) != null ? _f : "",
3534
3548
  providerMetadata: {
3535
3549
  openrouter: {
3536
- content: (_e = annotation.url_citation.content) != null ? _e : "",
3537
- startIndex: (_f = annotation.url_citation.start_index) != null ? _f : 0,
3538
- endIndex: (_g = annotation.url_citation.end_index) != null ? _g : 0
3550
+ content: (_g = annotation.url_citation.content) != null ? _g : "",
3551
+ startIndex: (_h = annotation.url_citation.start_index) != null ? _h : 0,
3552
+ endIndex: (_i = annotation.url_citation.end_index) != null ? _i : 0
3539
3553
  }
3540
3554
  }
3541
3555
  });
@@ -3551,7 +3565,7 @@ var OpenRouterChatLanguageModel = class {
3551
3565
  }
3552
3566
  if (delta.tool_calls != null) {
3553
3567
  for (const toolCallDelta of delta.tool_calls) {
3554
- const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3568
+ const index = (_j = toolCallDelta.index) != null ? _j : toolCalls.length - 1;
3555
3569
  if (toolCalls[index] == null) {
3556
3570
  if (toolCallDelta.type !== "function") {
3557
3571
  throw new InvalidResponseDataError({
@@ -3565,7 +3579,7 @@ var OpenRouterChatLanguageModel = class {
3565
3579
  message: `Expected 'id' to be a string.`
3566
3580
  });
3567
3581
  }
3568
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3582
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.name) == null) {
3569
3583
  throw new InvalidResponseDataError({
3570
3584
  data: toolCallDelta,
3571
3585
  message: `Expected 'function.name' to be a string.`
@@ -3576,7 +3590,7 @@ var OpenRouterChatLanguageModel = class {
3576
3590
  type: "function",
3577
3591
  function: {
3578
3592
  name: toolCallDelta.function.name,
3579
- arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3593
+ arguments: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3580
3594
  },
3581
3595
  inputStarted: false,
3582
3596
  sent: false
@@ -3588,7 +3602,7 @@ var OpenRouterChatLanguageModel = class {
3588
3602
  message: `Tool call at index ${index} is missing after creation.`
3589
3603
  });
3590
3604
  }
3591
- if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3605
+ if (((_m = toolCall2.function) == null ? void 0 : _m.name) != null && ((_n = toolCall2.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3592
3606
  toolCall2.inputStarted = true;
3593
3607
  controller.enqueue({
3594
3608
  type: "tool-input-start",
@@ -3639,18 +3653,18 @@ var OpenRouterChatLanguageModel = class {
3639
3653
  toolName: toolCall.function.name
3640
3654
  });
3641
3655
  }
3642
- if (((_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null) {
3643
- toolCall.function.arguments += (_o = (_n = toolCallDelta.function) == null ? void 0 : _n.arguments) != null ? _o : "";
3656
+ if (((_o = toolCallDelta.function) == null ? void 0 : _o.arguments) != null) {
3657
+ toolCall.function.arguments += (_q = (_p = toolCallDelta.function) == null ? void 0 : _p.arguments) != null ? _q : "";
3644
3658
  }
3645
3659
  controller.enqueue({
3646
3660
  type: "tool-input-delta",
3647
3661
  id: toolCall.id,
3648
- delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3662
+ delta: (_r = toolCallDelta.function.arguments) != null ? _r : ""
3649
3663
  });
3650
- if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3664
+ if (((_s = toolCall.function) == null ? void 0 : _s.name) != null && ((_t = toolCall.function) == null ? void 0 : _t.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3651
3665
  controller.enqueue({
3652
3666
  type: "tool-call",
3653
- toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3667
+ toolCallId: (_u = toolCall.id) != null ? _u : generateId(),
3654
3668
  toolName: toolCall.function.name,
3655
3669
  input: toolCall.function.arguments,
3656
3670
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3880,7 +3894,8 @@ var OpenRouterCompletionChunkSchema = z8.union([
3880
3894
  usage: z8.object({
3881
3895
  prompt_tokens: z8.number(),
3882
3896
  prompt_tokens_details: z8.object({
3883
- cached_tokens: z8.number()
3897
+ cached_tokens: z8.number(),
3898
+ cache_write_tokens: z8.number().nullish()
3884
3899
  }).passthrough().nullish(),
3885
3900
  completion_tokens: z8.number(),
3886
3901
  completion_tokens_details: z8.object({
@@ -3970,7 +3985,7 @@ var OpenRouterCompletionLanguageModel = class {
3970
3985
  }, this.config.extraBody), this.settings.extraBody);
3971
3986
  }
3972
3987
  async doGenerate(options) {
3973
- 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;
3988
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
3974
3989
  const providerOptions = options.providerOptions || {};
3975
3990
  const openrouterOptions = providerOptions.openrouter || {};
3976
3991
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -4016,37 +4031,24 @@ var OpenRouterCompletionLanguageModel = class {
4016
4031
  }
4017
4032
  ],
4018
4033
  finishReason: mapOpenRouterFinishReason(choice.finish_reason),
4019
- usage: {
4020
- inputTokens: {
4021
- total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
4022
- noCache: void 0,
4023
- cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
4024
- cacheWrite: void 0
4025
- },
4026
- outputTokens: {
4027
- total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : 0,
4028
- text: void 0,
4029
- reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
4030
- },
4031
- raw: (_l = response.usage) != null ? _l : void 0
4032
- },
4034
+ usage: response.usage ? computeTokenUsage(response.usage) : emptyUsage(),
4033
4035
  warnings: [],
4034
4036
  providerMetadata: {
4035
4037
  openrouter: OpenRouterProviderMetadataSchema.parse({
4036
- provider: (_m = response.provider) != null ? _m : "",
4038
+ provider: (_b16 = response.provider) != null ? _b16 : "",
4037
4039
  usage: __spreadValues(__spreadValues(__spreadValues(__spreadValues({
4038
- promptTokens: (_o = (_n = response.usage) == null ? void 0 : _n.prompt_tokens) != null ? _o : 0,
4039
- completionTokens: (_q = (_p = response.usage) == null ? void 0 : _p.completion_tokens) != null ? _q : 0,
4040
- 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)
4041
- }, ((_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 ? {
4040
+ promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
4041
+ completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0,
4042
+ 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)
4043
+ }, ((_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 ? {
4042
4044
  promptTokensDetails: {
4043
4045
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
4044
4046
  }
4045
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
4047
+ } : {}), ((_o = (_n = response.usage) == null ? void 0 : _n.completion_tokens_details) == null ? void 0 : _o.reasoning_tokens) != null ? {
4046
4048
  completionTokensDetails: {
4047
4049
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
4048
4050
  }
4049
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
4051
+ } : {}), ((_q = (_p = response.usage) == null ? void 0 : _p.cost_details) == null ? void 0 : _q.upstream_inference_cost) != null ? {
4050
4052
  costDetails: {
4051
4053
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
4052
4054
  }
@@ -4102,7 +4104,7 @@ var OpenRouterCompletionLanguageModel = class {
4102
4104
  stream: response.pipeThrough(
4103
4105
  new TransformStream({
4104
4106
  transform(chunk, controller) {
4105
- var _a16, _b16, _c;
4107
+ var _a16, _b16, _c, _d, _e;
4106
4108
  if (options.includeRawChunks) {
4107
4109
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4108
4110
  }
@@ -4121,30 +4123,29 @@ var OpenRouterCompletionLanguageModel = class {
4121
4123
  provider = value.provider;
4122
4124
  }
4123
4125
  if (value.usage != null) {
4124
- usage.inputTokens.total = value.usage.prompt_tokens;
4125
- usage.outputTokens.total = value.usage.completion_tokens;
4126
+ const computed = computeTokenUsage(value.usage);
4127
+ Object.assign(usage.inputTokens, computed.inputTokens);
4128
+ Object.assign(usage.outputTokens, computed.outputTokens);
4126
4129
  rawUsage = value.usage;
4127
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
4130
+ const promptTokens = (_a16 = value.usage.prompt_tokens) != null ? _a16 : 0;
4131
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
4132
+ openrouterUsage.promptTokens = promptTokens;
4128
4133
  if (value.usage.prompt_tokens_details) {
4129
- const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
4130
- usage.inputTokens.cacheRead = cachedInputTokens;
4131
4134
  openrouterUsage.promptTokensDetails = {
4132
- cachedTokens: cachedInputTokens
4135
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
4133
4136
  };
4134
4137
  }
4135
- openrouterUsage.completionTokens = value.usage.completion_tokens;
4138
+ openrouterUsage.completionTokens = completionTokens;
4136
4139
  if (value.usage.completion_tokens_details) {
4137
- const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
4138
- usage.outputTokens.reasoning = reasoningTokens;
4139
4140
  openrouterUsage.completionTokensDetails = {
4140
- reasoningTokens
4141
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
4141
4142
  };
4142
4143
  }
4143
4144
  if (value.usage.cost != null) {
4144
4145
  openrouterUsage.cost = value.usage.cost;
4145
4146
  }
4146
4147
  openrouterUsage.totalTokens = value.usage.total_tokens;
4147
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
4148
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
4148
4149
  if (upstreamInferenceCost != null) {
4149
4150
  openrouterUsage.costDetails = {
4150
4151
  upstreamInferenceCost
@@ -4524,7 +4525,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4524
4525
  }
4525
4526
 
4526
4527
  // src/version.ts
4527
- var VERSION2 = false ? "0.0.0-test" : "2.2.2";
4528
+ var VERSION2 = false ? "0.0.0-test" : "2.2.3";
4528
4529
 
4529
4530
  // src/provider.ts
4530
4531
  function createOpenRouter(options = {}) {