agy-cli 0.1.22: thinking is not counted twice in billed output (#1023)

ACP's outputTokens already includes the thinking tokens, so `completion_tokens`
is outputTokens again, not output + thinking. The totalTokens shape check
compares against input + output (and input + cache + output), without
thinking.

Evidence (klarkxy, agy 1.5.4): for single-turn Flash and Carbon prompts,
ACP's totalTokens equals input + output exactly (2322 + 4728 = 7050,
2316 + 9390 = 11706). Output minus thinking is 67 and 68 tokens, which
matches the visible replies. The store's gen_metadata output (field 3)
equals ACP's outputTokens, with thinking (field 9) below it. 0.1.19 sent
9389 and 18712, double-counting.

Run at a0898318 merged onto origin/main (1199adda), on mica with a temp
HOME: bun test packages/agy-cli gave 14 pass, 0 fail, and scripts/check.mjs
exited 0. CI's test and on-npm jobs pass. Earlier, at d18f2346, putting
`output + thinking` back by hand turned five tests red.
Not tried: a live agy session here.

Co-authored-by: klarkxy <klarkxy@users.noreply.github.com>
This commit is contained in:
Klarkxy
2026-11-12 10:52:21 +08:00
committed by GitHub
parent 92c20ffef6
commit b829a5541e
4 changed files with 123 additions and 8 deletions
+11 -3
View File
@@ -4231,6 +4231,14 @@ function tokenCount(value) {
// 2133 input + 4899 cache read + 6 output = 7038 total). Some earlier builds
// folded cached reads into inputTokens. totalTokens tells the two shapes apart,
// so both report the same uncached input and cache numbers.
//
// outputTokens already includes thoughtTokens: a flash reply of one visible
// token reported outputTokens 140 with thoughtTokens 139. Adding them again
// billed thinking twice. A report with thoughtTokens above outputTokens still
// bills at least the thinking. Because outputTokens already carries the
// thinking, totalTokens has no separate thinking term, so the shape check
// compares against input + output only. Adding thinking to both sides would
// tip a small cache read (below 2x thinking) into the cache-inclusive branch.
function promptUsage(usage) {
if (!usage || typeof usage !== "object") usage = {}
const rawInput = tokenCount(usage.inputTokens)
@@ -4240,8 +4248,8 @@ function promptUsage(usage) {
const thinking = tokenCount(usage.thoughtTokens)
const total = tokenCount(usage.totalTokens)
const inclusive = total > 0
? Math.abs(total - (rawInput + output + thinking)) <
Math.abs(total - (rawInput + cacheRead + cacheWrite + output + thinking))
? Math.abs(total - (rawInput + output)) <
Math.abs(total - (rawInput + cacheRead + cacheWrite + output))
: cacheRead <= rawInput
// Prompt input excludes cache creation, which is reported separately.
const promptInput = inclusive ? rawInput : rawInput + cacheRead
@@ -4250,7 +4258,7 @@ function promptUsage(usage) {
input: promptInput - read,
read,
write: cacheWrite,
output: output + thinking,
output: Math.max(output, thinking),
thinking,
promptInput,
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-agy-cli-auth",
"version": "0.1.21",
"version": "0.1.22",
"description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI",
"type": "module",
"main": "./opencode.mjs",
+1 -1
View File
@@ -186,7 +186,7 @@ function finish(reply, extra = {}) {
stopReason: "end_turn",
usage: {
inputTokens: extra.exclusive ? input : input + cached,
outputTokens: reply.length,
outputTokens: reply.length + thought,
cachedReadTokens: cached,
cachedWriteTokens: 0,
thoughtTokens: thought,
+110 -3
View File
@@ -132,7 +132,7 @@ test("单步回合精确透传 ACP usage 并原样保留原始对象", async ()
const { responses } = await runTurn(kit, baseBody())
assert.equal(responses.length, 1)
assert.deepEqual(plain(responses[0].usage), {
input_tokens: 600, output_tokens: 35, cache_read_input_tokens: 400, cache_creation_input_tokens: 50,
input_tokens: 600, output_tokens: 30, cache_read_input_tokens: 400, cache_creation_input_tokens: 50,
output_tokens_details: { thinking_tokens: 5 },
})
const start = responses[0].events.find((item) => item.event === "message_start")
@@ -143,6 +143,86 @@ test("单步回合精确透传 ACP usage 并原样保留原始对象", async ()
})
})
test("ACP outputTokens 已含 thoughtTokens:单步不把思考记两次(flash 实测形状)", async () => {
await withKit({}, async (kit) => {
// Shape seen live: one visible token, 139 thinking. candidatesTokenCount
// = thoughtsTokenCount + 1 for a 1-byte visible reply.
const raw = { inputTokens: 12007, cachedReadTokens: 0, cachedWriteTokens: 0, outputTokens: 140, thoughtTokens: 139 }
kit.prompts.push(async (agent) => {
await agent.thought("long hidden reasoning")
await agent.text("4")
return { stopReason: "end_turn", usage: raw }
})
const { responses } = await runTurn(kit, baseBody())
assert.equal(responses.length, 1)
assert.equal(responses[0].usage.output_tokens, 140)
assert.equal(responses[0].usage.output_tokens_details.thinking_tokens, 139)
})
})
test("小读缓存 + 重思考:totalTokens 形状判断不把思考加在两边(12007/100/140/139)", async () => {
// Exclusive input, inclusive output: totalTokens = input + cache read + output.
// With thinking added to both sides of the shape check, a cache read below
// 2x thinking picked the cache-inclusive branch and billed input 11907.
for (const cachedReadTokens of [100, 3000]) {
await withKit({}, async (kit) => {
const raw = {
inputTokens: 12_007, cachedReadTokens, cachedWriteTokens: 0,
outputTokens: 140, thoughtTokens: 139, totalTokens: 12_007 + cachedReadTokens + 140,
}
kit.prompts.push(async (agent) => {
await agent.thought("long hidden reasoning")
await agent.text("4")
return { stopReason: "end_turn", usage: raw }
})
const { responses } = await runTurn(kit, baseBody({ tools: [] }))
assert.equal(responses.length, 1)
const usage = plain(responses[0].usage)
assert.deepEqual(usage, {
input_tokens: 12_007,
cache_read_input_tokens: cachedReadTokens,
cache_creation_input_tokens: 0,
output_tokens: 140,
output_tokens_details: { thinking_tokens: 139 },
}, `cachedReadTokens ${cachedReadTokens}`)
assert.equal(usage.input_tokens + usage.cache_read_input_tokens, 12_007 + cachedReadTokens)
})
}
})
test("thoughtTokens 大于 outputTokens 时至少计思考数,thinking 不超过 output", async () => {
await withKit({}, async (kit) => {
kit.prompts.push(async (agent) => {
await agent.text("x")
return { stopReason: "end_turn", usage: { inputTokens: 100, outputTokens: 3, thoughtTokens: 9 } }
})
const { responses } = await runTurn(kit, baseBody())
assertCleanUsage(responses[0].usage, "thinking>output")
assert.equal(responses[0].usage.output_tokens, 9)
})
})
test("多步工具回合:各步 output 之和不超过 ACP 累计 outputTokens(含思考)", async () => {
await withKit({}, async (kit) => {
const raw = { inputTokens: 40_000, cachedReadTokens: 30_000, cachedWriteTokens: 0, outputTokens: 5_000, thoughtTokens: 4_800 }
kit.prompts.push(async (agent) => {
await agent.thought("plan")
await agent.text("Reading.")
await agent.tools([["Read", { path: "/a" }]])
await agent.thought("more")
await agent.tools([["Read", { path: "/b" }]])
await agent.text("Done.")
return { stopReason: "end_turn", usage: raw }
})
const { responses } = await runTurn(kit, baseBody())
assert.ok(responses.length >= 3)
for (const parsed of responses) assertCleanUsage(parsed.usage, "multi-step")
const total = responses.reduce((sum, parsed) => sum + parsed.usage.output_tokens, 0)
assert.ok(total <= raw.outputTokens, `各步 output 之和 ${total} 超过 ACP 累计 ${raw.outputTokens}`)
assert.ok(total >= raw.outputTokens - 50, `各步 output 之和 ${total} 明显少于 ACP 累计`)
})
})
// ------------------------------------------------------------------ 25 步固定案例
test("25 步/74 工具/累计 4,775,934:各步窗口有界、原始累计保留、求和不守恒反例", async () => {
@@ -1012,9 +1092,10 @@ test("agy 1.5.1+ 独立 inputTokens(未含读缓存)按 totalTokens 完整
inputTokens: 2133,
cachedReadTokens: 28_610,
cachedWriteTokens: 120,
outputTokens: 45,
// outputTokens already includes thoughtTokens, same as a live ACP report.
outputTokens: 60,
thoughtTokens: 15,
totalTokens: 2133 + 28_610 + 120 + 45 + 15,
totalTokens: 2133 + 28_610 + 120 + 60,
}
kit.prompts.push(async (agent) => {
await agent.text("answer with high cache hit")
@@ -1029,6 +1110,32 @@ test("agy 1.5.1+ 独立 inputTokens(未含读缓存)按 totalTokens 完整
output_tokens_details: { thinking_tokens: 15 },
})
})
// Same 1.5.1+ shape with a cache read well below 2x thinking, so the shape
// check cannot lean on a large cache read to pick the right branch.
await withKit({}, async (kit) => {
const raw = {
inputTokens: 2133,
cachedReadTokens: 300,
cachedWriteTokens: 0,
outputTokens: 2060,
thoughtTokens: 2000,
totalTokens: 2133 + 300 + 2060,
}
kit.prompts.push(async (agent) => {
await agent.thought("heavy reasoning")
await agent.text("answer with small cache hit")
return { stopReason: "end_turn", usage: raw }
})
const { responses } = await runTurn(kit, baseBody({ tools: [] }))
assert.deepEqual(plain(responses[0].usage), {
input_tokens: 2133,
cache_read_input_tokens: 300,
cache_creation_input_tokens: 0,
output_tokens: 2060,
output_tokens_details: { thinking_tokens: 2000 },
})
})
})
const localRequire = createRequire(import.meta.url)