fix(stream): map usage to pi-core's cache convention (input excludes cached tokens)

pi-core computes promptTokens = input + cacheRead + cacheWrite (Anthropic
convention: input EXCLUDES cached/written tokens), but qoder follows OpenAI
semantics where prompt_tokens INCLUDES cached_tokens. pi-ai's own OpenAI
provider subtracts them; this provider didn't, which under-reported the
cache rate (45% vs the true ~83%) and tripped pi-core's miss detector on
every cached turn.

- input = max(0, prompt_tokens - cacheRead - cache_write_tokens)
- cacheWrite reads cache_write_tokens, not cacheable_tokens (a capacity
  metric that is 0 even on first-turn writes, not a write count)
This commit is contained in:
liyanan
2026-07-30 12:42:22 +08:00
parent 1b110a6291
commit d4c31fcc4e
+19 -4
View File
@@ -311,13 +311,28 @@ export function streamQoder(
completion_tokens?: number;
total_tokens?: number;
completion_tokens_details?: { reasoning_tokens?: number };
prompt_tokens_details?: { cacheable_tokens?: number; cached_tokens?: number };
prompt_tokens_details?: {
cacheable_tokens?: number;
cached_tokens?: number;
cache_write_tokens?: number;
};
};
output.usage.input = u.prompt_tokens ?? 0;
// pi-core computes `promptTokens = input + cacheRead + cacheWrite`
// (Anthropic convention: `input` EXCLUDES cached/written tokens).
// Qoder follows OpenAI semantics where `prompt_tokens` INCLUDES
// `cached_tokens`, so subtract cacheRead (and cache_write_tokens
// when reported) to match the contract pi-ai's own OpenAI
// provider uses. `cacheable_tokens` is a capacity metric, not a
// write count (it is 0 even on first-turn writes), so it is NOT
// mapped to cacheWrite.
const promptTokens = u.prompt_tokens ?? 0;
const cacheReadTokens = u.prompt_tokens_details?.cached_tokens ?? 0;
const cacheWriteTokens = u.prompt_tokens_details?.cache_write_tokens ?? 0;
output.usage.input = Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens);
output.usage.output = u.completion_tokens ?? 0;
output.usage.totalTokens = u.total_tokens ?? 0;
output.usage.cacheRead = u.prompt_tokens_details?.cached_tokens ?? 0;
output.usage.cacheWrite = u.prompt_tokens_details?.cacheable_tokens ?? 0;
output.usage.cacheRead = cacheReadTokens;
output.usage.cacheWrite = cacheWriteTokens;
}
if (inner.choices && inner.choices.length > 0) {
const choice = inner.choices[0];