Skip to content

Commit 5b4643d

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 180ded6 commit 5b4643d

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
@@ -108,7 +108,7 @@ export namespace ACP {
108108
return
109109
}
110110

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

114114
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"
@@ -1158,9 +1161,9 @@ export namespace Provider {
11581161
},
11591162
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
11601163
limit: {
1161-
context: model.limit?.context ?? existingModel?.limit?.context ?? 0,
1164+
context: model.limit?.context ?? existingModel?.limit?.context ?? DEFAULT_CONTEXT_LIMIT,
11621165
input: model.limit?.input ?? existingModel?.limit?.input,
1163-
output: model.limit?.output ?? existingModel?.limit?.output ?? 0,
1166+
output: model.limit?.output ?? existingModel?.limit?.output ?? DEFAULT_OUTPUT_LIMIT,
11641167
},
11651168
headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
11661169
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
@@ -269,10 +269,7 @@ export namespace Session {
269269
// tokens to get the non-cached input count for separate cost calculation.
270270
const adjustedInputTokens = safe(inputTokens - cacheReadInputTokens - cacheWriteInputTokens)
271271

272-
const total = input.usage.totalTokens
273-
274272
const tokens = {
275-
total,
276273
input: adjustedInputTokens,
277274
output: outputTokens - reasoningTokens,
278275
reasoning: reasoningTokens,
@@ -298,6 +295,7 @@ export namespace Session {
298295
.add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
299296
.toNumber(),
300297
),
298+
total: tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write,
301299
tokens,
302300
}
303301
}

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

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

452+
export function promptSize(tokens: Assistant["tokens"]): number {
453+
return tokens.input + tokens.cache.read + tokens.cache.write
454+
}
455+
456+
export function totalSize(tokens: Assistant["tokens"]): number {
457+
return tokens.input + tokens.output + tokens.reasoning + tokens.cache.read + tokens.cache.write
458+
}
459+
452460
export const Info = z.discriminatedUnion("role", [User, Assistant]).meta({
453461
ref: "Message",
454462
})

‎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
@@ -358,7 +358,17 @@ export namespace SessionProcessor {
358358
})
359359
ctx.assistantMessage.finish = value.finishReason
360360
ctx.assistantMessage.cost += usage.cost
361-
ctx.assistantMessage.tokens = usage.tokens
361+
const prev = ctx.assistantMessage.tokens
362+
ctx.assistantMessage.tokens = {
363+
total: usage.total,
364+
input: usage.tokens.input,
365+
output: (prev?.output ?? 0) + usage.tokens.output,
366+
reasoning: (prev?.reasoning ?? 0) + usage.tokens.reasoning,
367+
cache: {
368+
read: usage.tokens.cache.read,
369+
write: (prev?.cache?.write ?? 0) + usage.tokens.cache.write,
370+
},
371+
}
362372
yield* session.updatePart({
363373
id: PartID.ascending(),
364374
reason: value.finishReason,

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

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

1673-
test("model limit defaults to zero when not specified", async () => {
1673+
test("model limit defaults to 128000 context and 4096 output when not specified", async () => {
16741674
await using tmp = await tmpdir({
16751675
init: async (dir) => {
16761676
await Bun.write(
@@ -1701,8 +1701,8 @@ test("model limit defaults to zero when not specified", async () => {
17011701
fn: async () => {
17021702
const providers = await Provider.list()
17031703
const model = providers[ProviderID.make("no-limit")].models["model"]
1704-
expect(model.limit.context).toBe(0)
1705-
expect(model.limit.output).toBe(0)
1704+
expect(model.limit.context).toBe(128_000)
1705+
expect(model.limit.output).toBe(4_096)
17061706
},
17071707
})
17081708
})

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

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

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

@@ -365,24 +366,24 @@ describe("session.compaction.isOverflow", () => {
365366
})
366367
})
367368

368-
test("BUG: asymmetry — limit.input model allows 30K more usage before compaction than equivalent model without it", async () => {
369+
test("overflow uses total conversation size (input + output + reasoning + cache)", async () => {
369370
await using tmp = await tmpdir()
370371
await Instance.provide({
371372
directory: tmp.path,
372373
fn: async () => {
373-
// Two models with identical context/output limits, differing only in limit.input
374374
const withInputLimit = createModel({ context: 200_000, input: 200_000, output: 32_000 })
375375
const withoutInputLimit = createModel({ context: 200_000, output: 32_000 })
376376

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

380380
const withLimit = await SessionCompaction.isOverflow({ tokens, model: withInputLimit })
381381
const withoutLimit = await SessionCompaction.isOverflow({ tokens, model: withoutInputLimit })
382382

383-
// Both models have identical real capacity — they should agree:
384-
expect(withLimit).toBe(true) // should compact (170K leaves no room for 32K output)
385-
expect(withoutLimit).toBe(true) // correctly compacts (170K > 168K)
383+
// withInputLimit: usable = 200K - 20K = 180K. totalSize=175K < 180K. No overflow.
384+
expect(withLimit).toBe(false)
385+
// withoutInputLimit: usable = 200K - 32K = 168K. totalSize=175K > 168K. Overflow.
386+
expect(withoutLimit).toBe(true)
386387
},
387388
})
388389
})
@@ -1085,7 +1086,7 @@ describe("session.getUsage", () => {
10851086
expect(result.tokens.input).toBe(1000)
10861087
expect(result.tokens.output).toBe(400)
10871088
expect(result.tokens.reasoning).toBe(100)
1088-
expect(result.tokens.total).toBe(1500)
1089+
expect(result.total).toBe(1000 + 400 + 100 + 0 + 0)
10891090
})
10901091

10911092
test("does not double count reasoning tokens in cost", () => {
@@ -1182,8 +1183,7 @@ describe("session.getUsage", () => {
11821183
expect(result.tokens.input).toBe(500)
11831184
expect(result.tokens.cache.read).toBe(200)
11841185
expect(result.tokens.cache.write).toBe(300)
1185-
// total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1186-
expect(result.tokens.total).toBe(1500)
1186+
expect(result.total).toBe(500 + 500 + 0 + 200 + 300)
11871187
return
11881188
}
11891189

@@ -1201,8 +1201,7 @@ describe("session.getUsage", () => {
12011201
expect(result.tokens.input).toBe(500)
12021202
expect(result.tokens.cache.read).toBe(200)
12031203
expect(result.tokens.cache.write).toBe(300)
1204-
// total = adjusted (500) + output (500) + cacheRead (200) + cacheWrite (300)
1205-
expect(result.tokens.total).toBe(1500)
1204+
expect(result.total).toBe(500 + 500 + 0 + 200 + 300)
12061205
},
12071206
)
12081207

@@ -1227,4 +1226,195 @@ describe("session.getUsage", () => {
12271226
expect(result.tokens.cache.read).toBe(200)
12281227
expect(result.tokens.cache.write).toBe(300)
12291228
})
1229+
1230+
test("total is derived from token components", () => {
1231+
const model = createModel({ context: 100_000, output: 32_000 })
1232+
const result = Session.getUsage({
1233+
model,
1234+
usage: {
1235+
inputTokens: 1000,
1236+
outputTokens: 500,
1237+
totalTokens: 1500,
1238+
},
1239+
})
1240+
expect(result.total).toBe(1000 + 500 + 0 + 0 + 0)
1241+
expect(result.tokens.input).toBe(1000)
1242+
expect(result.tokens.output).toBe(500)
1243+
})
1244+
})
1245+
1246+
describe("MessageV2.token helpers", () => {
1247+
test("promptSize returns input + cache read + cache write", () => {
1248+
const tokens = { input: 5000, output: 2000, reasoning: 500, cache: { read: 3000, write: 1000 } }
1249+
expect(MessageV2.promptSize(tokens)).toBe(5000 + 3000 + 1000)
1250+
})
1251+
1252+
test("promptSize works with zero cache", () => {
1253+
const tokens = { input: 5000, output: 2000, reasoning: 0, cache: { read: 0, write: 0 } }
1254+
expect(MessageV2.promptSize(tokens)).toBe(5000)
1255+
})
1256+
1257+
test("totalSize returns sum of all token fields", () => {
1258+
const tokens = { input: 5000, output: 2000, reasoning: 500, cache: { read: 3000, write: 1000 } }
1259+
expect(MessageV2.totalSize(tokens)).toBe(5000 + 2000 + 500 + 3000 + 1000)
1260+
})
1261+
})
1262+
1263+
describe("isOverflow", () => {
1264+
function makeConfig(opts?: { auto?: boolean; reserved?: number }): Config.Info {
1265+
return {
1266+
compaction: {
1267+
auto: opts?.auto ?? true,
1268+
reserved: opts?.reserved,
1269+
},
1270+
} as Config.Info
1271+
}
1272+
1273+
test("returns false when auto compaction is disabled", () => {
1274+
const model = createModel({ context: 100_000, output: 32_000 })
1275+
const tokens = { input: 90_000, output: 500, reasoning: 0, cache: { read: 0, write: 0 } }
1276+
expect(isOverflow({ cfg: makeConfig({ auto: false }), tokens, model })).toBe(false)
1277+
})
1278+
1279+
test("returns false when context limit is 0", () => {
1280+
const model = {
1281+
...createModel({ context: 100_000, output: 32_000 }),
1282+
limit: { ...createModel({ context: 100_000, output: 32_000 }).limit, context: 0 },
1283+
}
1284+
const tokens = { input: 90_000, output: 500, reasoning: 0, cache: { read: 0, write: 0 } }
1285+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(false)
1286+
})
1287+
1288+
test("uses total size (all token fields) for overflow check", () => {
1289+
const model = createModel({ context: 100_000, output: 32_000 })
1290+
// Total size = 50_000 + 30_000 + 5_000 + 10_000 + 5_000 = 100_000
1291+
// With context=100k and output=32k, usable = 100k - 20k = 80k
1292+
// 100k >= 80k => overflowing
1293+
const tokens = { input: 50_000, output: 30_000, reasoning: 5_000, cache: { read: 10_000, write: 5_000 } }
1294+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(true)
1295+
})
1296+
1297+
test("does not overflow when total size fits in usable context", () => {
1298+
const model = createModel({ context: 100_000, output: 32_000 })
1299+
// Total size = 40_000 + 10_000 + 5_000 + 5_000 + 0 = 60_000
1300+
// usable = 100k - 20k = 80k
1301+
// 60k < 80k => not overflowing
1302+
const tokens = { input: 40_000, output: 10_000, reasoning: 5_000, cache: { read: 5_000, write: 0 } }
1303+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(false)
1304+
})
1305+
1306+
test("overflows when total size exceeds usable context", () => {
1307+
const model = createModel({ context: 100_000, output: 32_000 })
1308+
// Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1309+
// usable = 100k - 20k = 80k
1310+
// 86k >= 80k => overflowing
1311+
const tokens = { input: 70_000, output: 1_000, reasoning: 0, cache: { read: 10_000, write: 5_000 } }
1312+
expect(isOverflow({ cfg: makeConfig(), tokens, model })).toBe(true)
1313+
})
1314+
1315+
test("respects custom reserved value", () => {
1316+
const model = createModel({ context: 100_000, output: 32_000 })
1317+
// Total size = 70_000 + 1_000 + 0 + 10_000 + 5_000 = 86_000
1318+
// reserved=50k, usable=100k-50k=50k
1319+
// 86k >= 50k => overflow
1320+
const tokens = { input: 70_000, output: 1_000, reasoning: 0, cache: { read: 10_000, write: 5_000 } }
1321+
expect(isOverflow({ cfg: makeConfig({ reserved: 50_000 }), tokens, model })).toBe(true)
1322+
})
1323+
})
1324+
1325+
describe("token accumulation across steps", () => {
1326+
test("accumulates output, reasoning, and cache.write across steps; replaces input and cache.read", () => {
1327+
// Step 1: initial request
1328+
const step1Tokens = Session.getUsage({
1329+
model: createModel({ context: 100_000, output: 32_000 }),
1330+
usage: { inputTokens: 10000, outputTokens: 500, totalTokens: 10500 },
1331+
}).tokens
1332+
expect(step1Tokens.input).toBe(10000)
1333+
expect(step1Tokens.output).toBe(500)
1334+
expect(step1Tokens.reasoning).toBe(0)
1335+
expect(step1Tokens.cache.read).toBe(0)
1336+
expect(step1Tokens.cache.write).toBe(0)
1337+
1338+
// After step 1: assistantMessage.tokens = step1Tokens (no prev)
1339+
let accumulated = { ...step1Tokens, cache: { ...step1Tokens.cache } }
1340+
1341+
// Step 2: tool result, conversation has grown
1342+
const step2Tokens = Session.getUsage({
1343+
model: createModel({ context: 100_000, output: 32_000 }),
1344+
usage: {
1345+
inputTokens: 15000,
1346+
outputTokens: 800,
1347+
reasoningTokens: 200,
1348+
cachedInputTokens: 5000,
1349+
totalTokens: 16000,
1350+
},
1351+
}).tokens
1352+
expect(step2Tokens.input).toBe(10000) // 15000 - 5000 (cached)
1353+
expect(step2Tokens.output).toBe(600) // 800 - 200 (reasoning)
1354+
expect(step2Tokens.reasoning).toBe(200)
1355+
expect(step2Tokens.cache.read).toBe(5000)
1356+
expect(step2Tokens.cache.write).toBe(0)
1357+
1358+
// Simulate what processor.ts does: accumulate additive fields, replace snapshot fields
1359+
accumulated = {
1360+
input: step2Tokens.input,
1361+
output: (accumulated.output ?? 0) + step2Tokens.output,
1362+
reasoning: (accumulated.reasoning ?? 0) + step2Tokens.reasoning,
1363+
cache: {
1364+
read: step2Tokens.cache.read,
1365+
write: (accumulated.cache?.write ?? 0) + step2Tokens.cache.write,
1366+
},
1367+
}
1368+
1369+
// After two steps:
1370+
expect(accumulated.input).toBe(10000) // replaced: last step's input
1371+
expect(accumulated.output).toBe(500 + 600) // accumulated: 1100
1372+
expect(accumulated.reasoning).toBe(0 + 200) // accumulated: 200
1373+
expect(accumulated.cache.read).toBe(5000) // replaced: last step's cache read
1374+
expect(accumulated.cache.write).toBe(0 + 0) // accumulated: 0
1375+
1376+
// Step 3: more tool calls, cache hits
1377+
const step3Tokens = Session.getUsage({
1378+
model: createModel({ context: 100_000, output: 32_000 }),
1379+
usage: {
1380+
inputTokens: 20000,
1381+
outputTokens: 1200,
1382+
reasoningTokens: 300,
1383+
cachedInputTokens: 12000,
1384+
totalTokens: 21200,
1385+
},
1386+
metadata: { anthropic: { cacheCreationInputTokens: 500 } },
1387+
}).tokens
1388+
expect(step3Tokens.input).toBe(7500) // 20000 - 12000 - 500
1389+
expect(step3Tokens.output).toBe(900) // 1200 - 300
1390+
expect(step3Tokens.reasoning).toBe(300)
1391+
expect(step3Tokens.cache.read).toBe(12000)
1392+
expect(step3Tokens.cache.write).toBe(500)
1393+
1394+
accumulated = {
1395+
input: step3Tokens.input,
1396+
output: accumulated.output + step3Tokens.output,
1397+
reasoning: accumulated.reasoning + step3Tokens.reasoning,
1398+
cache: {
1399+
read: step3Tokens.cache.read,
1400+
write: accumulated.cache.write + step3Tokens.cache.write,
1401+
},
1402+
}
1403+
1404+
// After three steps:
1405+
expect(accumulated.input).toBe(7500) // replaced: latest step
1406+
expect(accumulated.output).toBe(500 + 600 + 900) // accumulated: 2000
1407+
expect(accumulated.reasoning).toBe(0 + 200 + 300) // accumulated: 500
1408+
expect(accumulated.cache.read).toBe(12000) // replaced: latest step
1409+
expect(accumulated.cache.write).toBe(0 + 0 + 500) // accumulated: 500
1410+
1411+
// promptSize = input tokens sent to the LLM (current prompt footprint)
1412+
const promptSize = MessageV2.promptSize(accumulated)
1413+
expect(promptSize).toBe(7500 + 12000 + 500) // 20_000
1414+
1415+
// totalSize = full conversation size (used for context overflow and display %)
1416+
// includes output because the next turn's prompt will include this response
1417+
const totalSize = MessageV2.totalSize(accumulated)
1418+
expect(totalSize).toBe(7500 + 2000 + 500 + 12000 + 500) // 22_500
1419+
})
12301420
})

0 commit comments

Comments
 (0)