fix(http): emit copilot_usage.total_nano_aiu for exact opencode cost - #96
Open
David Koleczek (DavidKoleczek) wants to merge 1 commit into
Open
fix(http): emit copilot_usage.total_nano_aiu for exact opencode cost#96David Koleczek (DavidKoleczek) wants to merge 1 commit into
David Koleczek (DavidKoleczek) wants to merge 1 commit into
Conversation
Collaborator
Author
|
Companion PR (required together): microsoft/amplifier-app-opencode#15 |
opencode re-derives session cost from token counts times a static per-model rate table and ignores the exact usage.cost_usd already sent on the wire. With Anthropic prompt caching this overstates cost: cache-read tokens are folded into input at the full input rate and cache-creation tokens are dropped (recorded as 0). Emit a top-level copilot_usage.total_nano_aiu = round(cost_usd * 1e11) as a sibling of usage on the terminal stop / tool_calls chunk. opencode reads this off the raw SSE chunk and overrides its token-times-rate estimate with total_nano_aiu / 1e11, so the displayed cost equals cost_usd exactly. The field is additive and ignored by clients that do not read it. This is only consumed when opencode enables raw-chunk passthrough, which requires the provider id to contain "github-copilot" -- the companion change in amplifier-app-opencode. Both are required together. Refs: microsoft-amplifier/amplifier-support#352 🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier) Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
David Koleczek (DavidKoleczek)
force-pushed
the
fix/opencode-exact-cost
branch
from
July 20, 2026 19:25
664ea09 to
951475f
Compare
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.
Bug
When opencode uses amplifier-agent as an OpenAI-compatible provider, the session cost opencode displays is overstated. amplifier-agent's own
cost_usdis Anthropic-exact, but opencode never uses it: it re-derives cost from token counts times a static per-model rate table. With Anthropic prompt caching this is wrong in two ways -- cache-read tokens get folded into the input bucket and billed at the full input rate, and cache-creation tokens are dropped (recorded as 0). The net effect is a systematic overcharge.Reported in microsoft-amplifier/amplifier-support#352.
Fix
Emit a top-level
copilot_usage: {total_nano_aiu: round(cost_usd * 1e11)}as a sibling ofusageon the terminalstop/tool_callsSSE chunk (_wire.py), derived from the per-turnusage_costthe route already accumulates (chat_completions.py).opencode reads
copilot_usage.total_nano_aiuoff the raw SSE chunk and overrides its token-times-rate estimate withtotal_nano_aiu / 1e11. Because that value is derived fromcost_usd, opencode's displayed cost becomes exactlycost_usd. The field is additive; clients that do not read it ignore it.This is only consumed when opencode enables raw-chunk passthrough (
includeRawChunks), which it does only when the provider id contains the substringgithub-copilot. That is the companion change in amplifier-app-opencode (rename the provider id togithub-copilot-amplifier). Both changes are required together; neither has any effect alone.Verification
Verified end-to-end in an isolated environment (opencode 1.17.15, amplifier-agent) with a 3-turn, cache-heavy session on claude-haiku-4-5:
$0.04905320vs amplifier-agentcost_usd$0.03107695(+57.8%).$0.03055695== amplifier-agentcost_usd$0.03055695(penny-exact, gap eliminated).The session completed with no 4xx, confirming amplifier-agent tolerates the Copilot request headers opencode injects for
github-copilot-substring providers.ruff checkandruff format --checkpass on the changed files.