fix: report real token usage and estimate model costs#20
Merged
ephraimduncan merged 5 commits intomainfrom Mar 22, 2026
Merged
Conversation
The proxy hardcoded usage: { prompt_tokens: 0, completion_tokens: 0,
total_tokens: 0 } in both streaming and non-streaming responses, causing
OpenCode's sidebar to always show 0 tokens.
Cursor provides token data via two channels that were being ignored:
- TokenDeltaUpdate (incremental output token counts in InteractionUpdate)
- ConversationTokenDetails (total context usage in checkpoint updates)
Changes:
- Handle tokenDelta case in handleInteractionUpdate, accumulating output
token counts in StreamState
- Extract tokenDetails.usedTokens from ConversationStateStructure on
checkpoint updates
- Emit a final usage-only SSE chunk (empty choices, per OpenAI spec)
before [DONE] in streaming responses
- Return real usage from collectFullResponse for non-streaming responses
- Add stream_options to ChatCompletionRequest interface
Cursor is subscription-based so per-token cost was hardcoded to 0, making the sidebar always show $0.00 spent. Map model IDs to their upstream provider's per-token pricing (Claude, GPT, Gemini, Grok) so users see approximate market value of their usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes token usage always showing
0and cost always showing$0.00in OpenCode.Changes
Real token usage from gRPC stream (
src/proxy.ts)outputTokensfrom Cursor'stokenDeltamessages andtotalTokensfrom checkpointtokenDetailsusage(prompt/completion/total) per OpenAI spec for streaming responsesCollectedResponsetypestream_optionsto the request interfaceMarket-rate cost estimation (
src/index.ts)cost: { input: 0, output: 0, ... }withestimateModelCost(model.id)