From d4c31fcc4e3f12ec0dd416148f3fdb5bfb9a47e7 Mon Sep 17 00:00:00 2001 From: liyanan Date: Thu, 30 Jul 2026 12:42:22 +0800 Subject: [PATCH] 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) --- src/stream.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/stream.ts b/src/stream.ts index 9c42e5e..360b366 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -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];