Repository navigation
fix: book proxy savings only for accepted generations and label them as estimates - #32
Open
1aifanatic wants to merge 1 commit into
Open
1aifanatic wants to merge 1 commit into
1aifanatic wants to merge 1 commit into
Conversation
…as estimates - Book savings/requests only for /v1/messages, never count_tokens, and only after the upstream returned 2xx (a rejected or failed request realised no saving). Stats writes are wrapped so they can't fail a request. - addSaving() takes the signed net-dollar estimate and accumulates it as estimatedNetUsd, so folds that cost more than they saved now offset favourable ones instead of being dropped. - Status line reads "↓ ~X tokens saved (est.)" plus "est. net $…", and the README documents the estimation assumptions (char-based tokens, fixed cache-read and ScaleDown rates). Refs scaledown-team#18 Co-Authored-By: Claude Opus 5.5 <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.
Refs #18. This covers the proxy accounting and labelling parts. What's left is listed below.
Problem
server.tssent both/v1/messagesand/v1/messages/count_tokensthroughaddSaving/addRequest, and it did so before the upstream call. Count-only preflights and failed or rejected requests therefore raised the "saved" total.stats.addSavingdropped non-positive values and ignored the transform'snetSavingsUsd, so a fold that cost more than it saved never reduced the total.↓ X saved, which reads like measured billing.Changes
/v1/messages, and only when the upstream response is 2xx.count_tokensis never booked. The stats write sits in its owntryso it can't break a request.addSaving(sessionId, tokens, netUsd?)keeps the estimated token reduction (totalSaved, positive only) separate from a newestimatedNetUsd, which accumulates with its sign.↓ ~X tokens saved (est.)and, when present,est. net $…(negative values included).Tests
src/proxy/server-accounting.test.ts(stats module mocked):count_tokenscalls book nothing;All four fail against the current
server.tsand pass with this change.src/stats.test.ts: the signed accumulation stays separate from the token total, and non-finite or empty inputs are ignored.Both files mock
os.homedir(). The existing proxy tests setprocess.env.HOMEinstead, which jest's ESM sandbox doesn't pass toos.homedir(), so those tests touch the real~/.scaledown.Not in this PR (remaining for #18)
count_tokens. Acount_tokenscall can still trigger a ScaleDown fold. It is no longer booked as a saving.server.ts(session ids). The conflict is small and confined to theaddSaving/addRequestlines.🤖 Generated with Claude Code