Skip to content

Commit 3d9c91d

Browse files
fix: accumulate tokens across multi-step tool calls instead of overwriting
Multi-step tool-call messages only retained the last step's output, reasoning, and cache.write tokens because processor.ts overwrote assistantMessage.tokens on each finish-step event instead of accumulating additive fields. - Accumulate output, reasoning, cache.write with += across steps - Replace input and cache.read with latest step's values (snapshots) - Derive total from components instead of API totalTokens (which double-counts cached tokens in AI SDK v6) - Add MessageV2.promptSize() and totalSize() helpers for consistent computation across consumers - Use totalSize() for compaction threshold (output/reasoning are part of the next turn's context) - Fix custom provider limit.context defaulting to 0 (now 128,000) and limit.output defaulting to 0 (now 4,096) which broke context % display and disabled auto-compaction - Include cache.write in ACP usage reporting Fixes anomalyco#21913
1 parent 59c0fc2 commit 3d9c91d

8 files changed

Lines changed: 234 additions & 26 deletions

File tree

‎packages/opencode/src/acp/agent.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export namespace ACP {
109109
return
110110
}
111111

112-
const used = msg.tokens.input + (msg.tokens.cache?.read ?? 0)
112+
const used = msg.tokens.input + (msg.tokens.cache?.read ?? 0) + (msg.tokens.cache?.write ?? 0)
113113
const totalCost = assistantMessages.reduce((sum, m) => sum + m.info.cost, 0)
114114

115115
await connection

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { type LanguageModelV3 } from "@ai-sdk/provider"
1313
import { ModelsDev } from "./models"
1414
import { Auth } from "../auth"
1515
import { Env } from "../env"
16+
17+
const DEFAULT_CONTEXT_LIMIT = 128_000
18+
const DEFAULT_OUTPUT_LIMIT = 4_096
1619
import { Instance } from "../project/instance"
1720
import { Flag } from "../flag/flag"
1821
import { iife } from "@/util/iife"
@@ -1170,9 +1173,9 @@ export namespace Provider {
11701173
},
11711174
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
11721175
limit: {
1173-
context: model.limit?.context ?? existingModel?.limit?.context ?? 0,
1176+
context: model.limit?.context ?? existingModel?.limit?.context ?? DEFAULT_CONTEXT_LIMIT,
11741177
input: model.limit?.input ?? existingModel?.limit?.input,
1175-
output: model.limit?.output ?? existingModel?.limit?.output ?? 0,
1178+
output: model.limit?.output ?? existingModel?.limit?.output ?? DEFAULT_OUTPUT_LIMIT,
11761179
},
11771180
headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
11781181
family: model.family ?? existingModel?.family ?? "",

‎packages/opencode/src/session/index.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ export namespace Session {
271271
// tokens to get the non-cached input count for separate cost calculation.
272272
const adjustedInputTokens = safe(inputTokens - cacheReadInputTokens - cacheWriteInputTokens)
273273

274-
const total = input.usage.totalTokens
275-
276274
const tokens = {
277-
total,
278275
input: adjustedInputTokens,
279276
output: safe(outputTokens - reasoningTokens),
280277
reasoning: reasoningTokens,
@@ -300,6 +297,7 @@ export namespace Session {
300297
.add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
301298
.toNumber(),
302299
),
300+
total: tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write,
303301
tokens,
304302
}
305303
}

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,14 @@ export namespace MessageV2 {
452452
})
453453
export type Assistant = z.infer<typeof Assistant>
454454

455+
export function promptSize(tokens: Assistant["tokens"]): number {
456+
return tokens.input + tokens.cache.read + tokens.cache.write
457+
}
458+
459+
export function totalSize(tokens: Assistant["tokens"]): number {
460+
return tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write
461+
}
462+
455463
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
456464
ref: "Message",
457465
})

‎packages/opencode/src/session/overflow.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import type { Config } from "@/config/config"
22
import type { Provider } from "@/provider/provider"
33
import { ProviderTransform } from "@/provider/transform"
4-
import type { MessageV2 } from "./message-v2"
4+
import { MessageV2 } from "./message-v2"
55

66
const COMPACTION_BUFFER = 20_000
77

88
export function isOverflow(input: { cfg: Config.Info; tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
99
if (input.cfg.compaction?.auto === false) return false
1010
const context = input.model.limit.context
11-
if (context === 0) return false
11+
if (!context) return false
1212

13-
const count =
14-
input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write
13+
const count = MessageV2.totalSize(input.tokens)
1514

1615
const reserved =
1716
input.cfg.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model))

‎packages/opencode/src/session/processor.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,17 @@ export namespace SessionProcessor {
362362
})
363363
ctx.assistantMessage.finish = value.finishReason
364364
ctx.assistantMessage.cost += usage.cost
365-
ctx.assistantMessage.tokens = usage.tokens
365+
const prev = ctx.assistantMessage.tokens
366+
ctx.assistantMessage.tokens = {
367+
total: usage.total,
368+
input: usage.tokens.input,
369+
output: (prev?.output ?? 0) + usage.tokens.output,
370+
reasoning: (prev?.reasoning ?? 0) + usage.tokens.reasoning,
371+
cache: {
372+
read: usage.tokens.cache.read,
373+
write: (prev?.cache?.write ?? 0) + usage.tokens.cache.write,
374+
},
375+
}
366376
yield* session.updatePart({
367377
id: PartID.ascending(),
368378
reason: value.finishReason,

‎packages/opencode/test/provider/provider.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,7 @@ test("closest checks multiple query terms in order", async () => {
17091709
})
17101710
})
17111711

1712-
test("model limit defaults to zero when not specified", async () => {
1712+
test("model limit defaults to 128000 context and 4096 output when not specified", async () => {
17131713
await using tmp = await tmpdir({
17141714
init: async (dir) => {
17151715
await Bun.write(
@@ -1740,8 +1740,8 @@ test("model limit defaults to zero when not specified", async () => {
17401740
fn: async () => {
17411741
const providers = await list()
17421742
const model = providers[ProviderID.make("no-limit")].models["model"]
1743-
expect(model.limit.context).toBe(0)
1744-
expect(model.limit.output).toBe(0)
1743+
expect(model.limit.context).toBe(128_000)
1744+
expect(model.limit.output).toBe(4_096)
17451745
},
17461746
})
17471747
})

‎packages/opencode/test/session/compaction.test.ts‎

Lines changed: 202 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { Provider } from "../../src/provider/provider"
2424
import * as SessionProcessorModule from "../../src/session/processor"
2525
import { Snapshot } from "../../src/snapshot"
2626
import { ProviderTest } from "../fake/provider"
27+
import { isOverflow } from "../../src/session/overflow"
2728

2829
Log.init({ print: false })
2930

@@ -375,24 +376,24 @@ describe("session.compaction.isOverflow", () => {
375376
})
376377
})
377378

378-
test("BUG: asymmetry — limit.input model allows 30K more usage before compaction than equivalent model without it", async () => {
379+
test("overflow uses total conversation size (input + output + reasoning + cache)", async () => {
379380
await using tmp = await tmpdir()
380381
await Instance.provide({
381382
directory: tmp.path,
382383
fn: async () => {
383-
// Two models with identical context/output limits, differing only in limit.input
384384
const withInputLimit = createModel({ context: 200_000, input: 200_000, output: 32_000 })
385385
const withoutInputLimit = createModel({ context: 200_000, output: 32_000 })
386386

387-
// 170K total tokens — well above context-output (168K) but below input limit (200K)
388-
const tokens = { input: 166_000, output: 10_000, reasoning: 0, cache: { read: 5_000, write: 0 } }
387+
// Total size = 100K + 70K + 0 + 5K + 0 = 175K
388+
const tokens = { input: 100_000, output: 70_000, reasoning: 0, cache: { read: 5_000, write: 0 } }
389389

390390
const withLimit = await SessionCompaction.isOverflow({ tokens, model: withInputLimit })
391391
const withoutLimit = await SessionCompaction.isOverflow({ tokens, model: withoutInputLimit })
392392

393-
// Both models have identical real capacity — they should agree:
394-
expect(withLimit).toBe(true) // should compact (170K leaves no room for 32K output)
395-
expect(withoutLimit).toBe(true) // correctly compacts (170K > 168K)
393+
// withInputLimit: usable = 200K - 20K = 180K. totalSize=175K < 180K. No overflow.
394+
expect(withLimit).toBe(false)
395+
// withoutInputLimit: usable = 200K - 32K = 168K. totalSize=175K > 168K. Overflow.
396+
expect(withoutLimit).toBe(true)
396397
},
397398
})
398399
})
@@ -1137,7 +1138,7 @@ describe("session.getUsage", () => {
11371138
expect(result.tokens.input).toBe(1000)
11381139
expect(result.tokens.output).toBe(400)
11391140
expect(result.tokens.reasoning).toBe(100)
1140-
expect(result.tokens.total).toBe(1500)
1141+
expect(result.total).toBe(1000 + 400 + 100 + 0 + 0)
11411142
})
11421143

11431144
test("does not double count reasoning tokens in cost", () => {
@@ -1268,8 +1269,7 @@ describe("session.getUsage", () => {
12681269
expect(result.tokens.input).toBe(500)
12691270
expect(result.tokens.cache.read).toBe(200)
12701271
expect(result.tokens.cache.write).toBe(300)
1271-
// total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1272-
expect(result.tokens.total).toBe(1500)
1272+
expect(result.total).toBe(500 + 500 + 0 + 200 + 300)
12731273
return
12741274
}
12751275

@@ -1287,8 +1287,7 @@ describe("session.getUsage", () => {
12871287
expect(result.tokens.input).toBe(500)
12881288
expect(result.tokens.cache.read).toBe(200)
12891289
expect(result.tokens.cache.write).toBe(300)
1290-
// total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1291-
expect(result.tokens.total).toBe(1500)
1290+
expect(result.total).toBe(500 + 500 + 0 + 200 + 300)
12921291
},
12931292
)
12941293

@@ -1321,4 +1320,195 @@ describe("session.getUsage", () => {
13211320
expect(result.tokens.cache.read).toBe(200)
13221321
expect(result.tokens.cache.write).toBe(300)
13231322
})
1323+
1324+
test("total is derived from token components", () => {
1325+
const model = createModel({ context: 100_000, output: 32_000 })
1326+
const result = Session.getUsage({
1327+
model,
1328+
usage: {
1329+
inputTokens: 1000,
1330+
outputTokens: 500,
1331+
totalTokens: 1500,
1332+
},
1333+
})
1334+
expect(result.total).toBe(1000 + 500 + 0 + 0 + 0)
1335+
expect(result.tokens.input).toBe(1000)
1336+
expect(result.tokens.output).toBe(500)
1337+
})
1338+
})
1339+
1340+
describe("MessageV2.token helpers", () => {
1341+
test("promptSize returns input + cache read + cache write", () => {
1342+
const tokens = { input: 5000, output: 2000, reasoning: 500, cache: { read: 3000, write: 1000 } }
1343+
expect(MessageV2.promptSize(tokens)).toBe(5000 + 3000 + 1000)
1344+
})
1345+
1346+
test("promptSize works with zero cache", () => {
1347+
const tokens = { input: 5000, output: 2000, reasoning: 0, cache: { read: 0, write: 0 } }
1348+
expect(MessageV2.promptSize(tokens)).toBe(5000)
1349+
})
1350+
1351+
test("totalSize returns sum of all token fields", () => {
1352+
const tokens = { input: 5000, output: 2000, reasoning: 500, cache: { read: 3000, write: 1000 } }
1353+
expect(MessageV2.totalSize(tokens)).toBe(5000 + 2000 + 500 + 3000 + 1000)
1354+
})
1355+
})
1356+
1357+
describe("isOverflow", () => {
1358+
function makeConfig(opts?: { auto?: boolean; reserved?: number }): Config.Info {
1359+
return {
1360+
compaction: {
1361+
auto: opts?.auto ?? true,
1362+
reserved: opts?.reserved,
1363+
},
1364+
} as Config.Info
1365+
}
1366+
1367+
test("returns false when auto compaction is disabled", () => {
1368+
const model = createModel({ context: 100_000, output: 32_000 })
1369+
const tokens = { input: 90_000, output: 500, reasoning: 0, cache: { read: 0, write: 0 } }
1370+
expect(isOverflow({ cfg: makeConfig({ auto: false }), tokens, model })).toBe(false)
1371+
})
1372+
1373+
test("returns false when context limit is 0", () => {
1374+
const model = {
1375+
...createModel({ context: 100_000, output: 32_000 }),
1376+
limit: { ...createModel({ context: 100_000, output: 32_000 }).limit, context: 0 },
1377+
}
1378+
const tokens = { input: 90_000, output: 500, reasoning: 0, cache: { read: 0, write: 0 } }
1379+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(false)
1380+
})
1381+
1382+
test("uses total size (all token fields) for overflow check", () => {
1383+
const model = createModel({ context: 100_000, output: 32_000 })
1384+
// Total size = 50_000 + 30_000 + 5_000 + 10_000 + 5_000 = 100_000
1385+
// With context=100k and output=32k, usable = 100k - 20k = 80k
1386+
// 100k >= 80k => overflowing
1387+
const tokens = { input: 50_000, output: 30_000, reasoning: 5_000, cache: { read: 10_000, write: 5_000 } }
1388+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(true)
1389+
})
1390+
1391+
test("does not overflow when total size fits in usable context", () => {
1392+
const model = createModel({ context: 100_000, output: 32_000 })
1393+
// Total size = 40_000 + 10_000 + 5_000 + 5_000 + 0 = 60_000
1394+
// usable = 100k - 20k = 80k
1395+
// 60k < 80k => not overflowing
1396+
const tokens = { input: 40_000, output: 10_000, reasoning: 5_000, cache: { read: 5_000, write: 0 } }
1397+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(false)
1398+
})
1399+
1400+
test("overflows when total size exceeds usable context", () => {
1401+
const model = createModel({ context: 100_000, output: 32_000 })
1402+
// Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1403+
// usable = 100k - 20k = 80k
1404+
// 86k >= 80k => overflowing
1405+
const tokens = { input: 70_000, output: 1_000, reasoning: 0, cache: { read: 10_000, write: 5_000 } }
1406+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(true)
1407+
})
1408+
1409+
test("respects custom reserved value", () => {
1410+
const model = createModel({ context: 100_000, output: 32_000 })
1411+
// Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1412+
// reserved=50k, usable=100k-50k=50k
1413+
// 86k >= 50k => overflow
1414+
const tokens = { input: 70_000, output: 1_000, reasoning: 0, cache: { read: 10_000, write: 5_000 } }
1415+
expect(isOverflow({ cfg: makeConfig({ reserved: 50_000 }), tokens, model })).toBe(true)
1416+
})
1417+
})
1418+
1419+
describe("token accumulation across steps", () => {
1420+
test("accumulates output, reasoning, and cache.write across steps; replaces input and cache.read", () => {
1421+
// Step 1: initial request
1422+
const step1Tokens = Session.getUsage({
1423+
model: createModel({ context: 100_000, output: 32_000 }),
1424+
usage: { inputTokens: 10000, outputTokens: 500, totalTokens: 10500 },
1425+
}).tokens
1426+
expect(step1Tokens.input).toBe(10000)
1427+
expect(step1Tokens.output).toBe(500)
1428+
expect(step1Tokens.reasoning).toBe(0)
1429+
expect(step1Tokens.cache.read).toBe(0)
1430+
expect(step1Tokens.cache.write).toBe(0)
1431+
1432+
// After step 1: assistantMessage.tokens = step1Tokens (no prev)
1433+
let accumulated = { ...step1Tokens, cache: { ...step1Tokens.cache } }
1434+
1435+
// Step 2: tool result, conversation has grown
1436+
const step2Tokens = Session.getUsage({
1437+
model: createModel({ context: 100_000, output: 32_000 }),
1438+
usage: {
1439+
inputTokens: 15000,
1440+
outputTokens: 800,
1441+
reasoningTokens: 200,
1442+
cachedInputTokens: 5000,
1443+
totalTokens: 16000,
1444+
},
1445+
}).tokens
1446+
expect(step2Tokens.input).toBe(10000) // 15000 - 5000 (cached)
1447+
expect(step2Tokens.output).toBe(600) // 800 - 200 (reasoning)
1448+
expect(step2Tokens.reasoning).toBe(200)
1449+
expect(step2Tokens.cache.read).toBe(5000)
1450+
expect(step2Tokens.cache.write).toBe(0)
1451+
1452+
// Simulate what processor.ts does: accumulate additive fields, replace snapshot fields
1453+
accumulated = {
1454+
input: step2Tokens.input,
1455+
output: (accumulated.output ?? 0) + step2Tokens.output,
1456+
reasoning: (accumulated.reasoning ?? 0) + step2Tokens.reasoning,
1457+
cache: {
1458+
read: step2Tokens.cache.read,
1459+
write: (accumulated.cache?.write ?? 0) + step2Tokens.cache.write,
1460+
},
1461+
}
1462+
1463+
// After two steps:
1464+
expect(accumulated.input).toBe(10000) // replaced: last step's input
1465+
expect(accumulated.output).toBe(500 + 600) // accumulated: 1100
1466+
expect(accumulated.reasoning).toBe(0 + 200) // accumulated: 200
1467+
expect(accumulated.cache.read).toBe(5000) // replaced: last step's cache read
1468+
expect(accumulated.cache.write).toBe(0 + 0) // accumulated: 0
1469+
1470+
// Step 3: more tool calls, cache hits
1471+
const step3Tokens = Session.getUsage({
1472+
model: createModel({ context: 100_000, output: 32_000 }),
1473+
usage: {
1474+
inputTokens: 20000,
1475+
outputTokens: 1200,
1476+
reasoningTokens: 300,
1477+
cachedInputTokens: 12000,
1478+
totalTokens: 21200,
1479+
},
1480+
metadata: { anthropic: { cacheCreationInputTokens: 500 } },
1481+
}).tokens
1482+
expect(step3Tokens.input).toBe(7500) // 20000 - 12000 - 500
1483+
expect(step3Tokens.output).toBe(900) // 1200 - 300
1484+
expect(step3Tokens.reasoning).toBe(300)
1485+
expect(step3Tokens.cache.read).toBe(12000)
1486+
expect(step3Tokens.cache.write).toBe(500)
1487+
1488+
accumulated = {
1489+
input: step3Tokens.input,
1490+
output: accumulated.output + step3Tokens.output,
1491+
reasoning: accumulated.reasoning + step3Tokens.reasoning,
1492+
cache: {
1493+
read: step3Tokens.cache.read,
1494+
write: accumulated.cache.write + step3Tokens.cache.write,
1495+
},
1496+
}
1497+
1498+
// After three steps:
1499+
expect(accumulated.input).toBe(7500) // replaced: latest step
1500+
expect(accumulated.output).toBe(500 + 600 + 900) // accumulated: 2000
1501+
expect(accumulated.reasoning).toBe(0 + 200 + 300) // accumulated: 500
1502+
expect(accumulated.cache.read).toBe(12000) // replaced: latest step
1503+
expect(accumulated.cache.write).toBe(0 + 0 + 500) // accumulated: 500
1504+
1505+
// promptSize = input tokens sent to the LLM (current prompt footprint)
1506+
const promptSize = MessageV2.promptSize(accumulated)
1507+
expect(promptSize).toBe(7500 + 12000 + 500) // 20_000
1508+
1509+
// totalSize = full conversation size (used for context overflow and display %)
1510+
// includes output because the next turn's prompt will include this response
1511+
const totalSize = MessageV2.totalSize(accumulated)
1512+
expect(totalSize).toBe(7500 + 2000 + 500 + 12000 + 500) // 22_500
1513+
})
13241514
})

0 commit comments

Comments
 (0)