Skip to content

core: provider API cannot report context occupancy separately from cumulative turn usage #67166

Backlog

Description

@PVLPM

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-60 at 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:

  1. 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.

  2. Point a 1,000,000-token model at it and run one turn.

  3. OpenCode stores the normalized split: tokens.input is 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}}.

  4. 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

  1. An AI SDK provider can report the final request's prompt size separately from the turn's cumulative consumption.
  2. The context meter and estimatePrompt prefer that value when present, and keep using the token sum otherwise.
  3. Session totals keep accumulating the cumulative counters unchanged.
  4. Unknown values remain unknown; no clamping or fabricated limits.

Additional context

Activity

  1. opencodejr commented on Nov 29, 2026

    @opencodejr
    Contributor

    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() in packages/core/src/aisdk.ts maps the input, cache and output counters but never sets contextTokens, even though the internal Usage schema (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:

  2. added
    area/providers
    type/feature
    version/v2
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/providersplatform/windowstype/featureversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions