TSA-495: Include rate-limited and retry charges in document_cost - #538
Merged
Merged
Conversation
Collaborator
|
good work! |
RomanNabukhotniidev
approved these changes
Sep 29, 2026
MatiStb
self-requested a review
September 30, 2026 10:03
Dekshad
self-requested a review
September 30, 2026 13:59
Dekshad
approved these changes
Sep 30, 2026
Dekshad
left a comment
Collaborator
There was a problem hiding this comment.
LGTM!
Tested locally, works as expected.
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.
Closes #495
Summary
document_costonly summed thetranscriptiontable, but the worker chargesdocument.spent_usdin two places where no transcription row is written:deferRateLimitedPage): tokens from the first callare charged, the page is deferred, the charge lives only in
page_eventtranscribe_rate_limitedhandleFailedTranscriptionwith retry): the charge livesonly in
page_eventtranscribe_failedwithtranscription_id = NULLBudget enforcement was correct, but the cost view understated spending — or
returned no row at all for a document stopped by its budget on such calls.
Changes
document_cost: sumstranscriptionrows pluspage_eventcharges (transcribe_rate_limited/transcribe_failed) that haveno transcription row and
costUsd > 0. Events linked to a transcription row areskipped, so nothing is counted twice. Columns are unchanged;
calls,avg_latency_msandcache_hitsstill describe transcription rows only.down()restores the previous view.transcribe.ts: thetranscribe_rate_limitedevent now also storesinputTokens/outputTokens, same astranscribe_failedalready does.docs/schema/schema.sqlanddocs/04-database.mdupdated.The retryable-failure path is not in the original issue, but it is the same
root cause in the same view, so it is fixed here too.
How to test
npm run migrate:dev -w apps/backendrate-limited events ($0.000144 each) and a document with a retry charge
($0.003) + a final failure with a transcription row ($0.004) + a success ($0.01).
document.spent_usdwithdocument_cost.total_cost_usd.Before:
After:
Notes
add cost but 0 tokens.
calls = 0andavg_latency_ms = null.