fix(stream): correct cached input accounting
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# Changelog
|
||||
|
||||
## 0.4.4 - 2026-08-03
|
||||
|
||||
- Fix cached input tokens being counted twice.
|
||||
|
||||
## 0.4.3 - 2026-08-02
|
||||
|
||||
- Allow pi to start when model discovery is unavailable. The provider now caches the last successfully fetched model catalog so previously discovered Command Code models remain selectable offline; a first offline start without a cache keeps Command Code unavailable until `/reload` succeeds.
|
||||
|
||||
+7
-3
@@ -388,10 +388,14 @@ export function createStreamCommandCode(deps: CoreDependencies) {
|
||||
const usage = commandCodeUsage(event)
|
||||
if (usage) {
|
||||
const details = commandCodeInputTokenDetails(usage)
|
||||
output.usage.input = numberValue(usage.inputTokens) ?? 0
|
||||
const totalInput = numberValue(usage.inputTokens) ?? 0
|
||||
const input = numberValue(details?.noCacheTokens)
|
||||
const cacheRead = numberValue(details?.cacheReadTokens) ?? 0
|
||||
const cacheWrite = numberValue(details?.cacheWriteTokens) ?? 0
|
||||
output.usage.input = input ?? Math.max(0, totalInput - cacheRead - cacheWrite)
|
||||
output.usage.output = numberValue(usage.outputTokens) ?? 0
|
||||
output.usage.cacheRead = numberValue(details?.cacheReadTokens) ?? 0
|
||||
output.usage.cacheWrite = numberValue(details?.cacheWriteTokens) ?? 0
|
||||
output.usage.cacheRead = cacheRead
|
||||
output.usage.cacheWrite = cacheWrite
|
||||
output.usage.totalTokens =
|
||||
output.usage.input +
|
||||
output.usage.output +
|
||||
|
||||
@@ -102,9 +102,9 @@ describe("streamCommandCode — successful streams", () => {
|
||||
type: "finish",
|
||||
finishReason: "stop",
|
||||
totalUsage: {
|
||||
inputTokens: 5,
|
||||
outputTokens: 2,
|
||||
inputTokenDetails: { cacheReadTokens: 3, cacheWriteTokens: 1 },
|
||||
inputTokens: 3124,
|
||||
outputTokens: 15,
|
||||
inputTokenDetails: { noCacheTokens: 52, cacheReadTokens: 3072 },
|
||||
},
|
||||
}),
|
||||
],
|
||||
@@ -134,7 +134,10 @@ describe("streamCommandCode — successful streams", () => {
|
||||
done.message.content[0]?.type === "text" ? done.message.content[0].text : "",
|
||||
"Hello",
|
||||
)
|
||||
assert.equal(done.message.usage.totalTokens, 11)
|
||||
assert.equal(done.message.usage.input, 52)
|
||||
assert.equal(done.message.usage.cacheRead, 3072)
|
||||
assert.equal(done.message.usage.cacheWrite, 0)
|
||||
assert.equal(done.message.usage.totalTokens, 3139)
|
||||
assert.equal(calculatedUsages.length, 1)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user