@axlsdk/axl 0.7.5 → 0.7.6

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.d.cts CHANGED
@@ -108,6 +108,8 @@ type StreamChunk = {
108
108
  reasoning_tokens?: number;
109
109
  cached_tokens?: number;
110
110
  };
111
+ /** Estimated cost in USD for this call, computed the same way as ProviderResponse.cost. */
112
+ cost?: number;
111
113
  /** Provider-specific opaque metadata (e.g. raw Gemini parts with thought signatures). */
112
114
  providerMetadata?: Record<string, unknown>;
113
115
  };
package/dist/index.d.ts CHANGED
@@ -108,6 +108,8 @@ type StreamChunk = {
108
108
  reasoning_tokens?: number;
109
109
  cached_tokens?: number;
110
110
  };
111
+ /** Estimated cost in USD for this call, computed the same way as ProviderResponse.cost. */
112
+ cost?: number;
111
113
  /** Provider-specific opaque metadata (e.g. raw Gemini parts with thought signatures). */
112
114
  providerMetadata?: Record<string, unknown>;
113
115
  };
package/dist/index.js CHANGED
@@ -165,29 +165,32 @@ async function fetchWithRetry(input, init, maxRetries = MAX_RETRIES) {
165
165
 
166
166
  // src/providers/openai.ts
167
167
  var OPENAI_PRICING = {
168
- "gpt-4o": [25e-7, 1e-5],
169
- "gpt-4o-mini": [15e-8, 6e-7],
170
- "gpt-4-turbo": [1e-5, 3e-5],
171
- "gpt-4": [3e-5, 6e-5],
172
- "gpt-3.5-turbo": [5e-7, 15e-7],
173
- "gpt-5": [125e-8, 1e-5],
174
- "gpt-5-mini": [25e-8, 2e-6],
175
- "gpt-5-nano": [5e-8, 4e-7],
176
- "gpt-5.1": [125e-8, 1e-5],
177
- "gpt-5.2": [175e-8, 14e-6],
178
- "gpt-5.3": [175e-8, 14e-6],
179
- "gpt-5.4": [25e-7, 15e-6],
180
- "gpt-5.4-pro": [3e-5, 18e-5],
181
- o1: [15e-6, 6e-5],
182
- "o1-mini": [3e-6, 12e-6],
183
- "o1-pro": [15e-5, 6e-4],
184
- o3: [1e-5, 4e-5],
185
- "o3-mini": [11e-7, 44e-7],
186
- "o3-pro": [2e-5, 8e-5],
187
- "o4-mini": [11e-7, 44e-7],
188
- "gpt-4.1": [2e-6, 8e-6],
189
- "gpt-4.1-mini": [4e-7, 16e-7],
190
- "gpt-4.1-nano": [1e-7, 4e-7]
168
+ // gpt-4o era — cache reads at 50% of input rate
169
+ "gpt-4o": [25e-7, 1e-5, 0.5],
170
+ "gpt-4o-mini": [15e-8, 6e-7, 0.5],
171
+ "gpt-4-turbo": [1e-5, 3e-5, 0.5],
172
+ "gpt-4": [3e-5, 6e-5, 0.5],
173
+ "gpt-3.5-turbo": [5e-7, 15e-7, 0.5],
174
+ o1: [15e-6, 6e-5, 0.5],
175
+ "o1-mini": [3e-6, 12e-6, 0.5],
176
+ "o1-pro": [15e-5, 6e-4, 0.5],
177
+ // gpt-4.1 / o3 / o4 era — cache reads at 25% of input rate
178
+ "gpt-4.1": [2e-6, 8e-6, 0.25],
179
+ "gpt-4.1-mini": [4e-7, 16e-7, 0.25],
180
+ "gpt-4.1-nano": [1e-7, 4e-7, 0.25],
181
+ o3: [1e-5, 4e-5, 0.25],
182
+ "o3-mini": [11e-7, 44e-7, 0.25],
183
+ "o3-pro": [2e-5, 8e-5, 0.25],
184
+ "o4-mini": [11e-7, 44e-7, 0.25],
185
+ // gpt-5 era — cache reads at 10% of input rate
186
+ "gpt-5": [125e-8, 1e-5, 0.1],
187
+ "gpt-5-mini": [25e-8, 2e-6, 0.1],
188
+ "gpt-5-nano": [5e-8, 4e-7, 0.1],
189
+ "gpt-5.1": [125e-8, 1e-5, 0.1],
190
+ "gpt-5.2": [175e-8, 14e-6, 0.1],
191
+ "gpt-5.3": [175e-8, 14e-6, 0.1],
192
+ "gpt-5.4": [25e-7, 15e-6, 0.1],
193
+ "gpt-5.4-pro": [3e-5, 18e-5, 0.1]
191
194
  };
192
195
  var PRICING_KEYS_BY_LENGTH = Object.keys(OPENAI_PRICING).sort((a, b) => b.length - a.length);
193
196
  function estimateOpenAICost(model, promptTokens, completionTokens, cachedTokens) {
@@ -201,9 +204,9 @@ function estimateOpenAICost(model, promptTokens, completionTokens, cachedTokens)
201
204
  }
202
205
  }
203
206
  if (!pricing) return 0;
204
- const [inputRate, outputRate] = pricing;
207
+ const [inputRate, outputRate, cacheMultiplier] = pricing;
205
208
  const cached = cachedTokens ?? 0;
206
- const inputCost = (promptTokens - cached) * inputRate + cached * inputRate * 0.5;
209
+ const inputCost = (promptTokens - cached) * inputRate + cached * inputRate * cacheMultiplier;
207
210
  return inputCost + completionTokens * outputRate;
208
211
  }
209
212
  function isOSeriesModel(model) {
@@ -313,7 +316,7 @@ var OpenAIProvider = class {
313
316
  if (!res.body) {
314
317
  throw new Error("OpenAI stream response has no body");
315
318
  }
316
- yield* this.parseSSEStream(res.body);
319
+ yield* this.parseSSEStream(res.body, options.model);
317
320
  }
318
321
  // ---------------------------------------------------------------------------
319
322
  // Internal helpers
@@ -387,7 +390,7 @@ var OpenAIProvider = class {
387
390
  if (msg.tool_call_id) out.tool_call_id = msg.tool_call_id;
388
391
  return out;
389
392
  }
390
- async *parseSSEStream(body) {
393
+ async *parseSSEStream(body, model) {
391
394
  const reader = body.getReader();
392
395
  const decoder = new TextDecoder();
393
396
  let buffer = "";
@@ -404,7 +407,16 @@ var OpenAIProvider = class {
404
407
  const trimmed = line.trim();
405
408
  if (!trimmed || trimmed.startsWith(":")) continue;
406
409
  if (trimmed === "data: [DONE]") {
407
- yield { type: "done", usage: usageData };
410
+ yield {
411
+ type: "done",
412
+ usage: usageData,
413
+ cost: usageData ? estimateOpenAICost(
414
+ model,
415
+ usageData.prompt_tokens,
416
+ usageData.completion_tokens,
417
+ usageData.cached_tokens
418
+ ) : void 0
419
+ };
408
420
  return;
409
421
  }
410
422
  if (trimmed.startsWith("data: ")) {
@@ -447,7 +459,16 @@ var OpenAIProvider = class {
447
459
  }
448
460
  }
449
461
  }
450
- yield { type: "done", usage: usageData };
462
+ yield {
463
+ type: "done",
464
+ usage: usageData,
465
+ cost: usageData ? estimateOpenAICost(
466
+ model,
467
+ usageData.prompt_tokens,
468
+ usageData.completion_tokens,
469
+ usageData.cached_tokens
470
+ ) : void 0
471
+ };
451
472
  } finally {
452
473
  reader.releaseLock();
453
474
  }
@@ -705,6 +726,7 @@ var OpenAIResponsesProvider = class {
705
726
  const decoder = new TextDecoder();
706
727
  let buffer = "";
707
728
  const callIdMap = /* @__PURE__ */ new Map();
729
+ let eventType = "";
708
730
  try {
709
731
  while (true) {
710
732
  const { done, value } = await reader.read();
@@ -712,7 +734,6 @@ var OpenAIResponsesProvider = class {
712
734
  buffer += decoder.decode(value, { stream: true });
713
735
  const lines = buffer.split("\n");
714
736
  buffer = lines.pop() ?? "";
715
- let eventType = "";
716
737
  for (const line of lines) {
717
738
  const trimmed = line.trim();
718
739
  if (!trimmed || trimmed.startsWith(":")) continue;
@@ -780,7 +801,17 @@ var OpenAIResponsesProvider = class {
780
801
  } : void 0;
781
802
  const reasoningItems = response?.output?.filter((item) => item.type === "reasoning") ?? [];
782
803
  const providerMetadata = reasoningItems.length > 0 ? { openaiReasoningItems: reasoningItems } : void 0;
783
- return { type: "done", usage, providerMetadata };
804
+ return {
805
+ type: "done",
806
+ usage,
807
+ cost: usage ? estimateOpenAICost(
808
+ model,
809
+ usage.prompt_tokens,
810
+ usage.completion_tokens,
811
+ usage.cached_tokens
812
+ ) : void 0,
813
+ providerMetadata
814
+ };
784
815
  }
785
816
  case "response.failed": {
786
817
  const errorMsg = data.response?.error?.message ?? data.response?.status_details?.error?.message ?? "Unknown error";
@@ -913,7 +944,7 @@ var AnthropicProvider = class {
913
944
  if (!res.body) {
914
945
  throw new Error("Anthropic stream response has no body");
915
946
  }
916
- yield* this.parseSSEStream(res.body);
947
+ yield* this.parseSSEStream(res.body, options.model);
917
948
  }
918
949
  // ---------------------------------------------------------------------------
919
950
  // Internal: request building
@@ -1156,13 +1187,14 @@ ${jsonInstruction}` : jsonInstruction;
1156
1187
  // ---------------------------------------------------------------------------
1157
1188
  // Internal: SSE stream parsing
1158
1189
  // ---------------------------------------------------------------------------
1159
- async *parseSSEStream(body) {
1190
+ async *parseSSEStream(body, model) {
1160
1191
  const reader = body.getReader();
1161
1192
  const decoder = new TextDecoder();
1162
1193
  let buffer = "";
1163
1194
  let currentToolId = "";
1164
1195
  let currentToolName = "";
1165
1196
  let usage;
1197
+ let cacheWrite = 0;
1166
1198
  try {
1167
1199
  while (true) {
1168
1200
  const { done, value } = await reader.read();
@@ -1219,7 +1251,7 @@ ${jsonInstruction}` : jsonInstruction;
1219
1251
  case "message_start": {
1220
1252
  if (event.message?.usage) {
1221
1253
  const cacheRead = event.message.usage.cache_read_input_tokens ?? 0;
1222
- const cacheWrite = event.message.usage.cache_creation_input_tokens ?? 0;
1254
+ cacheWrite = event.message.usage.cache_creation_input_tokens ?? 0;
1223
1255
  const inputTokens = (event.message.usage.input_tokens ?? 0) + cacheRead + cacheWrite;
1224
1256
  usage = {
1225
1257
  prompt_tokens: inputTokens,
@@ -1250,13 +1282,33 @@ ${jsonInstruction}` : jsonInstruction;
1250
1282
  if (usage) {
1251
1283
  usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
1252
1284
  }
1253
- yield { type: "done", usage };
1285
+ yield {
1286
+ type: "done",
1287
+ usage,
1288
+ cost: usage ? estimateAnthropicCost(
1289
+ model,
1290
+ usage.prompt_tokens,
1291
+ usage.completion_tokens,
1292
+ usage.cached_tokens,
1293
+ cacheWrite
1294
+ ) : void 0
1295
+ };
1254
1296
  return;
1255
1297
  }
1256
1298
  }
1257
1299
  }
1258
1300
  }
1259
- yield { type: "done", usage };
1301
+ yield {
1302
+ type: "done",
1303
+ usage,
1304
+ cost: usage ? estimateAnthropicCost(
1305
+ model,
1306
+ usage.prompt_tokens,
1307
+ usage.completion_tokens,
1308
+ usage.cached_tokens,
1309
+ cacheWrite
1310
+ ) : void 0
1311
+ };
1260
1312
  } finally {
1261
1313
  reader.releaseLock();
1262
1314
  }
@@ -1383,7 +1435,7 @@ var GeminiProvider = class {
1383
1435
  if (!res.body) {
1384
1436
  throw new Error("Gemini stream response has no body");
1385
1437
  }
1386
- yield* this.parseSSEStream(res.body);
1438
+ yield* this.parseSSEStream(res.body, options.model);
1387
1439
  }
1388
1440
  // ---------------------------------------------------------------------------
1389
1441
  // Internal: request building
@@ -1664,7 +1716,7 @@ var GeminiProvider = class {
1664
1716
  // ---------------------------------------------------------------------------
1665
1717
  // Internal: SSE stream parsing
1666
1718
  // ---------------------------------------------------------------------------
1667
- async *parseSSEStream(body) {
1719
+ async *parseSSEStream(body, model) {
1668
1720
  const reader = body.getReader();
1669
1721
  const decoder = new TextDecoder();
1670
1722
  let buffer = "";
@@ -1720,7 +1772,17 @@ var GeminiProvider = class {
1720
1772
  }
1721
1773
  }
1722
1774
  const providerMetadata = accumulatedParts.length > 0 ? { geminiParts: accumulatedParts } : void 0;
1723
- yield { type: "done", usage, providerMetadata };
1775
+ yield {
1776
+ type: "done",
1777
+ usage,
1778
+ cost: usage ? estimateGeminiCost(
1779
+ model,
1780
+ usage.prompt_tokens,
1781
+ usage.completion_tokens,
1782
+ usage.cached_tokens
1783
+ ) : void 0,
1784
+ providerMetadata
1785
+ };
1724
1786
  } finally {
1725
1787
  reader.releaseLock();
1726
1788
  }
@@ -2430,7 +2492,8 @@ Please fix and try again.`;
2430
2492
  response = {
2431
2493
  content: content2,
2432
2494
  tool_calls: void 0,
2433
- usage: chunk.usage
2495
+ usage: chunk.usage,
2496
+ cost: chunk.cost
2434
2497
  };
2435
2498
  }
2436
2499
  }