@jambonz/llm 0.6.5 → 0.6.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.cjs CHANGED
@@ -187,12 +187,20 @@ async function* streamFromOpenAI(client, req, options) {
187
187
  throw new Error(`OpenAI-compatible stream returned an error: ${message}`);
188
188
  }
189
189
  if (chunk.usage) {
190
- const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
190
+ const details = chunk.usage.prompt_tokens_details;
191
+ const cachedTokens = details?.cached_tokens;
192
+ const cacheWriteTokens = details?.cache_write_tokens;
191
193
  usage = {
192
- ...chunk.usage.prompt_tokens !== void 0 ? { inputTokens: chunk.usage.prompt_tokens - (cachedTokens ?? 0) } : {},
194
+ ...chunk.usage.prompt_tokens !== void 0 ? {
195
+ inputTokens: Math.max(
196
+ 0,
197
+ chunk.usage.prompt_tokens - (cachedTokens ?? 0) - (cacheWriteTokens ?? 0)
198
+ )
199
+ } : {},
193
200
  ...chunk.usage.completion_tokens !== void 0 ? { outputTokens: chunk.usage.completion_tokens } : {},
194
201
  ...chunk.usage.total_tokens !== void 0 ? { totalTokens: chunk.usage.total_tokens } : {},
195
- ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {}
202
+ ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {},
203
+ ...cacheWriteTokens !== void 0 ? { cacheWriteTokens } : {}
196
204
  };
197
205
  }
198
206
  const choice = chunk.choices?.[0];