@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.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
@@ -3563,12 +3577,12 @@ var OpenRouterChatLanguageModel = class {
3563
3577
  sourceType: "url",
3564
3578
  id: annotation.url_citation.url,
3565
3579
  url: annotation.url_citation.url,
3566
- title: (_d = annotation.url_citation.title) != null ? _d : "",
3580
+ title: (_f = annotation.url_citation.title) != null ? _f : "",
3567
3581
  providerMetadata: {
3568
3582
  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
3583
+ content: (_g = annotation.url_citation.content) != null ? _g : "",
3584
+ startIndex: (_h = annotation.url_citation.start_index) != null ? _h : 0,
3585
+ endIndex: (_i = annotation.url_citation.end_index) != null ? _i : 0
3572
3586
  }
3573
3587
  }
3574
3588
  });
@@ -3584,7 +3598,7 @@ var OpenRouterChatLanguageModel = class {
3584
3598
  }
3585
3599
  if (delta.tool_calls != null) {
3586
3600
  for (const toolCallDelta of delta.tool_calls) {
3587
- const index = (_h = toolCallDelta.index) != null ? _h : toolCalls.length - 1;
3601
+ const index = (_j = toolCallDelta.index) != null ? _j : toolCalls.length - 1;
3588
3602
  if (toolCalls[index] == null) {
3589
3603
  if (toolCallDelta.type !== "function") {
3590
3604
  throw new InvalidResponseDataError({
@@ -3598,7 +3612,7 @@ var OpenRouterChatLanguageModel = class {
3598
3612
  message: `Expected 'id' to be a string.`
3599
3613
  });
3600
3614
  }
3601
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.name) == null) {
3615
+ if (((_k = toolCallDelta.function) == null ? void 0 : _k.name) == null) {
3602
3616
  throw new InvalidResponseDataError({
3603
3617
  data: toolCallDelta,
3604
3618
  message: `Expected 'function.name' to be a string.`
@@ -3609,7 +3623,7 @@ var OpenRouterChatLanguageModel = class {
3609
3623
  type: "function",
3610
3624
  function: {
3611
3625
  name: toolCallDelta.function.name,
3612
- arguments: (_j = toolCallDelta.function.arguments) != null ? _j : ""
3626
+ arguments: (_l = toolCallDelta.function.arguments) != null ? _l : ""
3613
3627
  },
3614
3628
  inputStarted: false,
3615
3629
  sent: false
@@ -3621,7 +3635,7 @@ var OpenRouterChatLanguageModel = class {
3621
3635
  message: `Tool call at index ${index} is missing after creation.`
3622
3636
  });
3623
3637
  }
3624
- if (((_k = toolCall2.function) == null ? void 0 : _k.name) != null && ((_l = toolCall2.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3638
+ if (((_m = toolCall2.function) == null ? void 0 : _m.name) != null && ((_n = toolCall2.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall2.function.arguments)) {
3625
3639
  toolCall2.inputStarted = true;
3626
3640
  controller.enqueue({
3627
3641
  type: "tool-input-start",
@@ -3672,18 +3686,18 @@ var OpenRouterChatLanguageModel = class {
3672
3686
  toolName: toolCall.function.name
3673
3687
  });
3674
3688
  }
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 : "";
3689
+ if (((_o = toolCallDelta.function) == null ? void 0 : _o.arguments) != null) {
3690
+ toolCall.function.arguments += (_q = (_p = toolCallDelta.function) == null ? void 0 : _p.arguments) != null ? _q : "";
3677
3691
  }
3678
3692
  controller.enqueue({
3679
3693
  type: "tool-input-delta",
3680
3694
  id: toolCall.id,
3681
- delta: (_p = toolCallDelta.function.arguments) != null ? _p : ""
3695
+ delta: (_r = toolCallDelta.function.arguments) != null ? _r : ""
3682
3696
  });
3683
- if (((_q = toolCall.function) == null ? void 0 : _q.name) != null && ((_r = toolCall.function) == null ? void 0 : _r.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3697
+ if (((_s = toolCall.function) == null ? void 0 : _s.name) != null && ((_t = toolCall.function) == null ? void 0 : _t.arguments) != null && isParsableJson(toolCall.function.arguments)) {
3684
3698
  controller.enqueue({
3685
3699
  type: "tool-call",
3686
- toolCallId: (_s = toolCall.id) != null ? _s : generateId(),
3700
+ toolCallId: (_u = toolCall.id) != null ? _u : generateId(),
3687
3701
  toolName: toolCall.function.name,
3688
3702
  input: toolCall.function.arguments,
3689
3703
  providerMetadata: !reasoningDetailsAttachedToToolCall ? {
@@ -3913,7 +3927,8 @@ var OpenRouterCompletionChunkSchema = import_v47.z.union([
3913
3927
  usage: import_v47.z.object({
3914
3928
  prompt_tokens: import_v47.z.number(),
3915
3929
  prompt_tokens_details: import_v47.z.object({
3916
- cached_tokens: import_v47.z.number()
3930
+ cached_tokens: import_v47.z.number(),
3931
+ cache_write_tokens: import_v47.z.number().nullish()
3917
3932
  }).passthrough().nullish(),
3918
3933
  completion_tokens: import_v47.z.number(),
3919
3934
  completion_tokens_details: import_v47.z.object({
@@ -4003,7 +4018,7 @@ var OpenRouterCompletionLanguageModel = class {
4003
4018
  }, this.config.extraBody), this.settings.extraBody);
4004
4019
  }
4005
4020
  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;
4021
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
4007
4022
  const providerOptions = options.providerOptions || {};
4008
4023
  const openrouterOptions = providerOptions.openrouter || {};
4009
4024
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), openrouterOptions);
@@ -4049,37 +4064,24 @@ var OpenRouterCompletionLanguageModel = class {
4049
4064
  }
4050
4065
  ],
4051
4066
  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
- },
4067
+ usage: response.usage ? computeTokenUsage(response.usage) : emptyUsage(),
4066
4068
  warnings: [],
4067
4069
  providerMetadata: {
4068
4070
  openrouter: OpenRouterProviderMetadataSchema.parse({
4069
- provider: (_m = response.provider) != null ? _m : "",
4071
+ provider: (_b16 = response.provider) != null ? _b16 : "",
4070
4072
  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 ? {
4073
+ promptTokens: (_d = (_c = response.usage) == null ? void 0 : _c.prompt_tokens) != null ? _d : 0,
4074
+ completionTokens: (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens) != null ? _f : 0,
4075
+ 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)
4076
+ }, ((_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
4077
  promptTokensDetails: {
4076
4078
  cachedTokens: response.usage.prompt_tokens_details.cached_tokens
4077
4079
  }
4078
- } : {}), ((_z = (_y = response.usage) == null ? void 0 : _y.completion_tokens_details) == null ? void 0 : _z.reasoning_tokens) != null ? {
4080
+ } : {}), ((_o = (_n = response.usage) == null ? void 0 : _n.completion_tokens_details) == null ? void 0 : _o.reasoning_tokens) != null ? {
4079
4081
  completionTokensDetails: {
4080
4082
  reasoningTokens: response.usage.completion_tokens_details.reasoning_tokens
4081
4083
  }
4082
- } : {}), ((_B = (_A = response.usage) == null ? void 0 : _A.cost_details) == null ? void 0 : _B.upstream_inference_cost) != null ? {
4084
+ } : {}), ((_q = (_p = response.usage) == null ? void 0 : _p.cost_details) == null ? void 0 : _q.upstream_inference_cost) != null ? {
4083
4085
  costDetails: {
4084
4086
  upstreamInferenceCost: response.usage.cost_details.upstream_inference_cost
4085
4087
  }
@@ -4135,7 +4137,7 @@ var OpenRouterCompletionLanguageModel = class {
4135
4137
  stream: response.pipeThrough(
4136
4138
  new TransformStream({
4137
4139
  transform(chunk, controller) {
4138
- var _a16, _b16, _c;
4140
+ var _a16, _b16, _c, _d, _e;
4139
4141
  if (options.includeRawChunks) {
4140
4142
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4141
4143
  }
@@ -4154,30 +4156,29 @@ var OpenRouterCompletionLanguageModel = class {
4154
4156
  provider = value.provider;
4155
4157
  }
4156
4158
  if (value.usage != null) {
4157
- usage.inputTokens.total = value.usage.prompt_tokens;
4158
- usage.outputTokens.total = value.usage.completion_tokens;
4159
+ const computed = computeTokenUsage(value.usage);
4160
+ Object.assign(usage.inputTokens, computed.inputTokens);
4161
+ Object.assign(usage.outputTokens, computed.outputTokens);
4159
4162
  rawUsage = value.usage;
4160
- openrouterUsage.promptTokens = value.usage.prompt_tokens;
4163
+ const promptTokens = (_a16 = value.usage.prompt_tokens) != null ? _a16 : 0;
4164
+ const completionTokens = (_b16 = value.usage.completion_tokens) != null ? _b16 : 0;
4165
+ openrouterUsage.promptTokens = promptTokens;
4161
4166
  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
4167
  openrouterUsage.promptTokensDetails = {
4165
- cachedTokens: cachedInputTokens
4168
+ cachedTokens: (_c = value.usage.prompt_tokens_details.cached_tokens) != null ? _c : 0
4166
4169
  };
4167
4170
  }
4168
- openrouterUsage.completionTokens = value.usage.completion_tokens;
4171
+ openrouterUsage.completionTokens = completionTokens;
4169
4172
  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
4173
  openrouterUsage.completionTokensDetails = {
4173
- reasoningTokens
4174
+ reasoningTokens: (_d = value.usage.completion_tokens_details.reasoning_tokens) != null ? _d : 0
4174
4175
  };
4175
4176
  }
4176
4177
  if (value.usage.cost != null) {
4177
4178
  openrouterUsage.cost = value.usage.cost;
4178
4179
  }
4179
4180
  openrouterUsage.totalTokens = value.usage.total_tokens;
4180
- const upstreamInferenceCost = (_c = value.usage.cost_details) == null ? void 0 : _c.upstream_inference_cost;
4181
+ const upstreamInferenceCost = (_e = value.usage.cost_details) == null ? void 0 : _e.upstream_inference_cost;
4181
4182
  if (upstreamInferenceCost != null) {
4182
4183
  openrouterUsage.costDetails = {
4183
4184
  upstreamInferenceCost
@@ -4557,7 +4558,7 @@ function withUserAgentSuffix2(headers, ...userAgentSuffixParts) {
4557
4558
  }
4558
4559
 
4559
4560
  // src/version.ts
4560
- var VERSION2 = false ? "0.0.0-test" : "2.2.2";
4561
+ var VERSION2 = false ? "0.0.0-test" : "2.2.3";
4561
4562
 
4562
4563
  // src/provider.ts
4563
4564
  function createOpenRouter(options = {}) {