Skip to content

feat(server): report OpenCode token usage for the context window meter - #7616

Closed
brdcastro wants to merge 4 commits into
pingdotgg:mainfrom
brdcastro:opencode-token-usage
Closed

brdcastro wants to merge 4 commits into
pingdotgg:mainfrom
brdcastro:opencode-token-usage

Conversation

@brdcastro

@brdcastro brdcastro commented Aug 20, 2026 •

Copy link
Copy Markdown

OpenCode sessions never report token usage, so the context window meter (used/max + percentage) that the composer shows for Claude and Codex sessions stays hidden for OpenCode threads.

The adapter now normalizes the token usage from each completed assistant message (message.updated) into a thread.token-usage.updated event, and the existing ingestion pipeline and meter handle the rest.

  • Usage is counted the same way OpenCode's own compaction check counts it (total when reported, otherwise input + output + cached input/write), capped at the model's context window when known.
  • The model's context window comes from client.provider.list(), resolved lazily on the first completed message and cached for the session.
  • One usage event per message id; assistant messages without token data (older OpenCode versions) are ignored without affecting the event pump.

Generated with Qwen3.8-27B (LM Studio) via opencode.


Note

Low Risk
Additive adapter behavior on the existing event path; failures on provider.list only omit max/cap, and malformed token shapes are handled defensively.

Overview
OpenCode threads can now drive the composer context window meter by emitting thread.token-usage.updated when assistant messages finish, matching what Claude and Codex adapters already do.

On completed assistant message.updated events (time.completed set), the adapter maps OpenCode’s tokens payload into a ThreadTokenUsageSnapshot via normalizeOpenCodeTokenUsage, using the same total formula as OpenCode’s compaction check (total when present, else input + output + cache read/write), caps usedTokens at the model limit when known, and sets compactsAutomatically: true. provider.list is called once per session (best-effort) to cache each model’s context window; emissions are one per message id, partial in-flight updates are ignored, and message.removed clears the dedupe set. Missing or zero token data is skipped without breaking the event pump.

Reviewed by Cursor Bugbot for commit 7592599. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Emit thread.token-usage.updated for completed OpenCode assistant messages

  • Adds normalizeOpenCodeTokenUsage and openCodeMessageTokensTotal helpers in OpenCodeAdapter.ts to compute token totals and build a ThreadTokenUsageSnapshot from AssistantMessage.tokens
  • On assistant message completion, the event pump resolves the model context window via client.provider.list (cached per session in modelContextWindowById), normalizes usage, and emits a thread.token-usage.updated event once per message using tokenUsageEmittedMessageIds
  • In-progress updates are ignored; message.removed clears the emission guard for that message
  • Risk: resolveModelContextWindow calls client.provider.list once per session on first need; if that call fails or returns unexpected data, maxTokens will be absent from the emitted snapshot

Macroscope summarized 7592599.

@coderabbitai

coderabbitai Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fdc42761-35c7-4d65-8f3d-be3d91204e2d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:L 100-499 changed lines (additions + deletions). labels Aug 20, 2026
Comment thread apps/server/src/provider/Layers/OpenCodeAdapter.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4fc60ac. Configure here.

Comment thread apps/server/src/provider/Layers/OpenCodeAdapter.ts
@macroscopeapp

macroscopeapp Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — This PR introduces a new feature for token usage reporting, adding new runtime behavior (event emission), session state tracking, and provider API integration. New capabilities introducing user-facing behavior warrant human review.

You can add or adjust custom eligibility rules. Learn more.

@t3dotgg

t3dotgg commented Aug 28, 2026

Copy link
Copy Markdown
Member

Note

🤖 GPT-5.6 Sol responding on behalf of Theo

We're closing this PR as we clean up the T3 Code backlog. Thank you for taking the time to put this together.

We are keeping OPEN #7072 as the review path for OpenCode context-window usage. The focused adapter test here remains useful reference.

If you believe we closed this in error, please reopen the PR and leave a comment explaining what we missed.

@t3dotgg t3dotgg closed this Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants