@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.
@@ -174,12 +174,20 @@ async function* streamFromOpenAI(client, req, options) {
174
174
  throw new Error(`OpenAI-compatible stream returned an error: ${message}`);
175
175
  }
176
176
  if (chunk.usage) {
177
- const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
177
+ const details = chunk.usage.prompt_tokens_details;
178
+ const cachedTokens = details?.cached_tokens;
179
+ const cacheWriteTokens = details?.cache_write_tokens;
178
180
  usage = {
179
- ...chunk.usage.prompt_tokens !== void 0 ? { inputTokens: chunk.usage.prompt_tokens - (cachedTokens ?? 0) } : {},
181
+ ...chunk.usage.prompt_tokens !== void 0 ? {
182
+ inputTokens: Math.max(
183
+ 0,
184
+ chunk.usage.prompt_tokens - (cachedTokens ?? 0) - (cacheWriteTokens ?? 0)
185
+ )
186
+ } : {},
180
187
  ...chunk.usage.completion_tokens !== void 0 ? { outputTokens: chunk.usage.completion_tokens } : {},
181
188
  ...chunk.usage.total_tokens !== void 0 ? { totalTokens: chunk.usage.total_tokens } : {},
182
- ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {}
189
+ ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {},
190
+ ...cacheWriteTokens !== void 0 ? { cacheWriteTokens } : {}
183
191
  };
184
192
  }
185
193
  const choice = chunk.choices?.[0];