mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
fix(agents): clamp Responses cached usage
This commit is contained in:
@@ -140,6 +140,39 @@ describe("openai transport stream", () => {
|
||||
).rejects.toThrow(/did not deliver a first event within 1ms after HTTP streaming headers/);
|
||||
});
|
||||
|
||||
it("clamps Responses cached prompt usage at zero", async () => {
|
||||
const model = createAzureResponsesModel();
|
||||
const output = createResponsesAssistantOutput(model);
|
||||
|
||||
await __testing.processResponsesStream(
|
||||
streamChunks([
|
||||
{
|
||||
type: "response.completed",
|
||||
response: {
|
||||
id: "resp-cache-overflow",
|
||||
status: "completed",
|
||||
usage: {
|
||||
input_tokens: 2,
|
||||
output_tokens: 5,
|
||||
total_tokens: 7,
|
||||
input_tokens_details: { cached_tokens: 4 },
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
output,
|
||||
{ push: vi.fn() },
|
||||
model,
|
||||
);
|
||||
|
||||
expectRecordFields(output.usage, {
|
||||
input: 0,
|
||||
output: 5,
|
||||
cacheRead: 4,
|
||||
totalTokens: 9,
|
||||
});
|
||||
});
|
||||
|
||||
it("summarizes model payload tools with full names when requested", () => {
|
||||
const previous = process.env.OPENCLAW_DEBUG_MODEL_PAYLOAD;
|
||||
process.env.OPENCLAW_DEBUG_MODEL_PAYLOAD = "tools";
|
||||
|
||||
@@ -887,12 +887,15 @@ async function processResponsesStream(
|
||||
| undefined;
|
||||
if (usage) {
|
||||
const cachedTokens = usage.input_tokens_details?.cached_tokens || 0;
|
||||
const inputTokens = usage.input_tokens || 0;
|
||||
const outputTokens = usage.output_tokens || 0;
|
||||
const input = Math.max(0, inputTokens - cachedTokens);
|
||||
output.usage = {
|
||||
input: (usage.input_tokens || 0) - cachedTokens,
|
||||
output: usage.output_tokens || 0,
|
||||
input,
|
||||
output: outputTokens,
|
||||
cacheRead: cachedTokens,
|
||||
cacheWrite: 0,
|
||||
totalTokens: usage.total_tokens || 0,
|
||||
totalTokens: input + outputTokens + cachedTokens,
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user