fix(agents): clamp Responses cached usage · openclaw/openclaw@f964b1c
vincentkoc
·
2026-05-16
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
@@ -140,6 +140,39 @@ describe("openai transport stream", () => {
|
140 | 140 | ).rejects.toThrow(/did not deliver a first event within 1ms after HTTP streaming headers/); |
141 | 141 | }); |
142 | 142 | |
| 143 | +it("clamps Responses cached prompt usage at zero", async () => { |
| 144 | +const model = createAzureResponsesModel(); |
| 145 | +const output = createResponsesAssistantOutput(model); |
| 146 | + |
| 147 | +await __testing.processResponsesStream( |
| 148 | +streamChunks([ |
| 149 | +{ |
| 150 | +type: "response.completed", |
| 151 | +response: { |
| 152 | +id: "resp-cache-overflow", |
| 153 | +status: "completed", |
| 154 | +usage: { |
| 155 | +input_tokens: 2, |
| 156 | +output_tokens: 5, |
| 157 | +total_tokens: 7, |
| 158 | +input_tokens_details: { cached_tokens: 4 }, |
| 159 | +}, |
| 160 | +}, |
| 161 | +}, |
| 162 | +]), |
| 163 | +output, |
| 164 | +{ push: vi.fn() }, |
| 165 | +model, |
| 166 | +); |
| 167 | + |
| 168 | +expectRecordFields(output.usage, { |
| 169 | +input: 0, |
| 170 | +output: 5, |
| 171 | +cacheRead: 4, |
| 172 | +totalTokens: 9, |
| 173 | +}); |
| 174 | +}); |
| 175 | + |
143 | 176 | it("summarizes model payload tools with full names when requested", () => { |
144 | 177 | const previous = process.env.OPENCLAW_DEBUG_MODEL_PAYLOAD; |
145 | 178 | process.env.OPENCLAW_DEBUG_MODEL_PAYLOAD = "tools"; |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -887,12 +887,15 @@ async function processResponsesStream(
|
887 | 887 | | undefined; |
888 | 888 | if (usage) { |
889 | 889 | const cachedTokens = usage.input_tokens_details?.cached_tokens || 0; |
| 890 | +const inputTokens = usage.input_tokens || 0; |
| 891 | +const outputTokens = usage.output_tokens || 0; |
| 892 | +const input = Math.max(0, inputTokens - cachedTokens); |
890 | 893 | output.usage = { |
891 | | -input: (usage.input_tokens || 0) - cachedTokens, |
892 | | -output: usage.output_tokens || 0, |
| 894 | + input, |
| 895 | +output: outputTokens, |
893 | 896 | cacheRead: cachedTokens, |
894 | 897 | cacheWrite: 0, |
895 | | -totalTokens: usage.total_tokens || 0, |
| 898 | +totalTokens: input + outputTokens + cachedTokens, |
896 | 899 | cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
897 | 900 | }; |
898 | 901 | } |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。