Skip to content

Commit 0fbcc96

Browse files
PierrunoYTclaude
andcommitted
test(openai): pin the cached_tokens upper bound when it equals prompt_tokens
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 78dd5c4 commit 0fbcc96

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/api/providers/__tests__/openai-usage-tracking.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,34 @@ describe("OpenAiHandler with usage tracking fix", () => {
385385
},
386386
])
387387
})
388+
389+
// Only OpenAI's nested counter must be a subset of prompt_tokens; this pins the inclusive boundary.
390+
it.each([
391+
["below the prompt size", 99, 99],
392+
["equal to the prompt size", 100, 100],
393+
["above the prompt size", 101, undefined],
394+
])(
395+
"accepts a nested cached_tokens count %s with no cache_read_input_tokens",
396+
async (_name, cached, expected) => {
397+
const cacheHandler = new OpenAiHandler({ ...mockOptions, openAiStreamingEnabled })
398+
const usage = {
399+
prompt_tokens: 100,
400+
completion_tokens: 5,
401+
prompt_tokens_details: { cached_tokens: cached },
402+
}
403+
mockCreate.mockResolvedValueOnce(
404+
openAiStreamingEnabled
405+
? asyncStreamFrom([{ choices: [], usage }])
406+
: { choices: [{ message: { content: "Response" } }], usage },
407+
)
408+
409+
const chunks = await collectStream(cacheHandler.createMessage("system prompt", []))
410+
411+
expect(chunks.filter((chunk) => chunk.type === "usage")).toEqual([
412+
{ type: "usage", inputTokens: 100, outputTokens: 5, cacheReadTokens: expected },
413+
])
414+
},
415+
)
388416
})
389417

390418
it.each([null, undefined, {}, { prompt_tokens_details: null }])(

0 commit comments

Comments
 (0)