Skip to content

fix(http): emit copilot_usage.total_nano_aiu for exact opencode cost - #96

Open
David Koleczek (DavidKoleczek) wants to merge 1 commit into
mainfrom
fix/opencode-exact-cost
Open

fix(http): emit copilot_usage.total_nano_aiu for exact opencode cost#96
David Koleczek (DavidKoleczek) wants to merge 1 commit into
mainfrom
fix/opencode-exact-cost

Conversation

@DavidKoleczek

Copy link
Copy Markdown
Collaborator

Bug

When opencode uses amplifier-agent as an OpenAI-compatible provider, the session cost opencode displays is overstated. amplifier-agent's own cost_usd is 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 of usage on the terminal stop / tool_calls SSE chunk (_wire.py), derived from the per-turn usage_cost the route already accumulates (chat_completions.py).

opencode reads copilot_usage.total_nano_aiu off the raw SSE chunk and overrides its token-times-rate estimate with total_nano_aiu / 1e11. Because that value is derived from cost_usd, opencode's displayed cost becomes exactly cost_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 substring github-copilot. That is the companion change in amplifier-app-opencode (rename the provider id to github-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:

  • Before: opencode recorded $0.04905320 vs amplifier-agent cost_usd $0.03107695 (+57.8%).
  • After: opencode recorded $0.03055695 == amplifier-agent cost_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 check and ruff format --check pass on the changed files.

@DavidKoleczek

Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant