feat(ai): show running $ spend — per-run cost + credits left in the response bar - #17
Merged
Merged
Conversation
…esponse bar
M5/bet-#2 ("turn BYO-key into a trust weapon"). The response bar has always had
a slot for credits, but agent.js hands back `credits: null` because nothing ever
told it. The gateway now emits a final `levelcode` SSE frame (thin.ly #367) with
what the turn cost and what's left, in retail micro-$.
- openaiCompat: parse the gateway's namespaced `levelcode` frame into
usage.{cost_micros, credits_remaining_micros}. It carries no `choices`, and no
other provider sends it, so this is inert everywhere else.
- agent.js: accumulate the RUN's cost across turns and keep the latest balance;
report both on agentDone (and in the 🐛 debug line next to cacheRead).
- chat.html: render money, not a count. The bar previously did
`String(credits) + ' credits'`, which would have printed "9876543 credits" for
a micro-$ figure. Now: `· $0.02 · $9.88 left`.
`money()` floors sub-cent turns at "<$0.01" rather than rounding to "$0.00" —
with prompt caching live, sub-cent turns are common and "$0.00" reads as
free/broken.
BYOK runs send neither figure, so the bar is unchanged there — this is a
gateway-only affordance.
All 5 extension test files pass.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Editor half of M5 / top-bet #2 — "turn BYO-key into a trust weapon: real token/cost accounting." Pairs with thin.ly #367 (which emits the data).
Why
The response bar has always had a slot for credits, but
agent.jshands backcredits: null— nothing ever told it. Your own plan's argument: "Cost opacity is the #1 criticism of BOTH Cursor and Windsurf" — and now that prompt caching is live and verified (~87% cache-read discount), the number is genuinely worth showing.What
openaiCompat.js— parse the gateway's namespacedlevelcodeframe intousage.{cost_micros, credits_remaining_micros}. Nochoices, and no other provider sends it → inert everywhere else.agent.js— accumulate the run's cost across turns, keep the latest balance, report both onagentDone(and in the 🐛 debug line next tocacheRead).chat.html— render money, not a count.One bug this avoids
The bar did
String(credits) + ' credit' + (credits === 1 ? '' : 's')— a count. Handing it a micro-$ figure would have printed "9876543 credits". It now renders:And
money()floors sub-cent turns at<$0.01rather than rounding to$0.00— with caching live, sub-cent turns are common and "$0.00" reads as free/broken.BYOK runs send neither figure, so the bar is unchanged there — this is a gateway-only affordance, and the "🖥 Local · Direct · BYO key" story stays literally true.
All 5 extension test files pass.
🤖 Generated with Claude Code