@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.
@@ -135,12 +135,20 @@ async function* streamFromOpenAI(client, req, options) {
135
135
  throw new Error(`OpenAI-compatible stream returned an error: ${message}`);
136
136
  }
137
137
  if (chunk.usage) {
138
- const cachedTokens = chunk.usage.prompt_tokens_details?.cached_tokens;
138
+ const details = chunk.usage.prompt_tokens_details;
139
+ const cachedTokens = details?.cached_tokens;
140
+ const cacheWriteTokens = details?.cache_write_tokens;
139
141
  usage = {
140
- ...chunk.usage.prompt_tokens !== void 0 ? { inputTokens: chunk.usage.prompt_tokens - (cachedTokens ?? 0) } : {},
142
+ ...chunk.usage.prompt_tokens !== void 0 ? {
143
+ inputTokens: Math.max(
144
+ 0,
145
+ chunk.usage.prompt_tokens - (cachedTokens ?? 0) - (cacheWriteTokens ?? 0)
146
+ )
147
+ } : {},
141
148
  ...chunk.usage.completion_tokens !== void 0 ? { outputTokens: chunk.usage.completion_tokens } : {},
142
149
  ...chunk.usage.total_tokens !== void 0 ? { totalTokens: chunk.usage.total_tokens } : {},
143
- ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {}
150
+ ...cachedTokens !== void 0 ? { cacheReadTokens: cachedTokens } : {},
151
+ ...cacheWriteTokens !== void 0 ? { cacheWriteTokens } : {}
144
152
  };
145
153
  }
146
154
  const choice = chunk.choices?.[0];