Repository navigation
Description
Description
OpenCode derives context occupancy from the same per-message tokens object it uses for cumulative accounting:
- the context meter sums
input + output + reasoning + cache.read + cache.write(packages/tui/src/util/session.ts:46-60at v2.4.2;packages/client/src/solid/usage.ts:8-21); - prompt estimation for compaction reads the same object (
packages/core/src/session/compaction.ts:1178-1202, used at:315); - session totals accumulate the same fields (
packages/core/src/session/usage.ts:11-20,43-64).
A provider that reports cumulative counters for a turn with several internal model requests is correct for consumption totals but wrong for current context occupancy. The AI SDK provider interface has no second value: usage() maps six counters and drops Usage.contextTokens, which exists in packages/ai/src/schema/events.ts:64-73 but is populated only by native protocol mappers. On dev (2026-11-29) no session, meter or compaction code reads it, and packages/core/src/aisdk.ts:879-893 still cannot set it.
OpenCode's native Claude Agent provider shows the current compromise: it reports the final model call's input and accumulates output, with the comment "Context occupancy is the last API call's input, not the sum of repeated contexts" (packages/core/src/plugin/provider/claude-agent/language-model/usage.ts:44-55). The meter is approximately right, but accumulated input and cache consumption cannot be represented alongside it.
Plugins
A local plugin that exposes an external ACP agent as a provider. No core modifications; the provider reports through the public AI SDK interface.
OpenCode version
2.4.2 (fc31a485317970bcf6be7b431a9830aaef875731). Also checked against dev on 2026-11-29.
Steps to reproduce
Deterministic, no model:
-
Register a provider whose one stream ends with a valid AI SDK usage object, cached input included in total input:
{ inputTokens: { total: 9_474_472, noCache: 157_736, cacheRead: 9_316_736, cacheWrite: 0 }, outputTokens: { total: 50_177 }, }
This is a constructed fixture derived from the normalized totals of a reported session, not a recording of that session's final context. An external runtime that re-sends conversation history for several internal requests can accumulate cached reads like this; not every ACP integration does.
-
Point a 1,000,000-token model at it and run one turn.
-
OpenCode stores the normalized split:
tokens.inputis the non-cached part (packages/core/src/session/usage.ts:11-20), so the assistant message reads{"input":157736,"output":50177,"reasoning":0,"cache":{"read":9316736,"write":0}}. -
The meter sums those fields:
157736 + 50177 + 9316736 = 9,524,649, reported as 100% of a 1,000,000 window.
This demonstrates the arithmetic, not the original session's context: the reported session's final occupancy was never recorded, and the fixture is not a measurement of it.
Screenshot and/or share link
None. The failure is in token normalization and estimation, before any useful session output.
Operating System
Windows 11 (reported); the arithmetic is platform-independent.
Terminal
N/A (OpenCode Desktop).
Expected behavior
- An AI SDK provider can report the final request's prompt size separately from the turn's cumulative consumption.
- The context meter and
estimatePromptprefer that value when present, and keep using the token sum otherwise. - Session totals keep accumulating the cumulative counters unchanged.
- Unknown values remain unknown; no clamping or fabricated limits.
Additional context
- The same number feeds
estimatePrompt, so cumulative consumption can cross the compaction ceiling earlier than the real prompt would. That is a code-derived consequence; I did not run end-to-end compaction against this provider. - Open PR fix(ai): preserve context size across Anthropic fallback #60807 adds
contextTokensto assistant messages and makes prompt estimation prefer it for the Anthropic fallback case. It does not expose the value to AI SDK providers, andcontextUsagestill sumstokens. - Closed issues Session token usage grows unbounded via cache.read and causes unrecoverable context-window errors #30649 (v1 session totals) and Anthropic server-side fallback: summed iteration usage makes the next step see about twice the real prompt size #60792 (Anthropic fallback summing) are related but different. ACP: report the context window on session start and usage after each model call #66734 covers ACP client-side reporting, and [FEATURE]: Plugin API: read the session context size estimate #57216 covers reading a context estimate from the plugin API.
Activity
opencodejr commentedon Nov 29, 2026
Thanks for the detailed write-up and the deterministic fixture.
What you're asking for: an AI SDK provider (such as your plugin wrapping an ACP agent) should be able to report the final request's prompt size separately from the turn's cumulative usage. The context meter and compaction's prompt estimate would use that value when it is present, and session totals would stay cumulative. I've filed this as a feature request rather than a bug, because the meter does what it was built to do. What's missing is a way for a provider to report the second number. The maintainers will consider it.
The code matches what you describe.
usage()inpackages/core/src/aisdk.tsmaps the input, cache and output counters but never setscontextTokens, even though the internalUsageschema (packages/ai/src/schema/events.ts) has that field. The Claude Agent provider works around this by reporting only the last API call's input.Related:
contextTokensto assistant messages and makes prompt estimation prefer it, for the Anthropic fallback case onlydocs(upstream): record the published upstream reports and their outcomes