fix(usage): count speech and transcription once after the tip merge

The tip already writes a usage_history row for every speech and transcription call
(recordMediaRequest), and the per-key USD limits count that row. Tagging those calls with
the modal ledger tier as well would count each call twice. Only rerank, which writes no
usage_history row, keeps the modal tier. The tests now check the speech ledger row keeps
its own tier and that a transcription with no reported duration adds no spend.
This commit is contained in:
serenity
2026-11-17 16:58:19 +03:30
parent 4693693230
commit 5db9866657
3 changed files with 18 additions and 13 deletions

View File

@@ -1 +1 @@
- **fix(usage):** text-to-speech, speech-to-text and rerank spend now counts toward a key's daily, weekly and 5-hour USD limits. Transcription is priced per second from the duration the provider reports. ([#19754](https://github.com/cheaper-inference/OmniRoute/pull/19754))
- **fix(usage):** rerank spend now counts toward a key's daily, weekly and 5-hour USD limits. Speech and transcription already count through their usage history rows and are counted once. ([#19754](https://github.com/cheaper-inference/OmniRoute/pull/19754))

View File

@@ -221,15 +221,14 @@ export function aggregateLedger(
export const SEARCH_LEDGER_SERVICE_TIER = "search";
/**
* `service_tier` marker on speech, transcription and rerank ledger rows. Those calls write no
* usage_history row, so per-key USD limits read their spend from the ledger. Image calls are not
* tagged: the ones that report usage already have a usage_history row, and counting both would
* charge them twice.
* `service_tier` marker on rerank ledger rows. Rerank writes no usage_history row, so per-key USD
* limits read its spend from the ledger. Speech, transcription and image calls are not tagged: they
* already have a usage_history row, and counting both would charge them twice.
*/
export const MODAL_LEDGER_SERVICE_TIER = "modal";
/**
* Total web-search and modal (speech, transcription, rerank) spend for an api key since an ISO
* Total web-search and modal (rerank) spend for an api key since an ISO
* timestamp, failed calls included (a provider that reports a cost on an error status still
* charged).
*/

View File

@@ -1,8 +1,9 @@
// Per-key USD limits (daily / weekly / 5 h) re-price usage_history and add the
// search ledger tier. Speech, transcription and rerank calls write no
// usage_history row, so their spend never moved a limit. They now write a
// request_cost_ledger row tagged with the "modal" tier, and the limit readers
// add that tier next to "search".
// search ledger tier. Rerank calls write no usage_history row, so their spend
// never moved a limit. They now write a request_cost_ledger row tagged with the
// "modal" tier, and the limit readers add that tier next to "search". Speech and
// transcription already write a usage_history row; their ledger row keeps its
// own tier, so each call is counted exactly once.
//
// Every route test drives the public handler with a real key and a mocked
// upstream, then reads the result through getApiKeyUsageLimitStatus, the code
@@ -155,7 +156,11 @@ test("a $2 TTS call flips weeklyExceeded on a $1 weekly limit, and the next call
const status = await statusOf(key.key);
assert.ok(Math.abs(status.weeklySpentUsd - 2) < 1e-9, `weekly spend ${status.weeklySpentUsd}`);
assert.equal(status.weeklyExceeded, true);
assert.deepEqual(ledgerTiers(key.id), ["modal"]);
assert.deepEqual(
ledgerTiers(key.id),
["standard"],
"speech counts from usage_history, not twice"
);
const blocked = await speak();
assert.equal(blocked.status, 429);
@@ -254,7 +259,7 @@ test("a transcription call priced per second flips weeklyExceeded on a $1 weekly
assert.equal(status.weeklyExceeded, true);
});
test("a transcription whose upstream reports no duration charges nothing", async () => {
test("a transcription whose upstream reports no duration adds no spend", async () => {
saveSyncedPricing({
openai: { "whisper-1": { input: 0, output: 0, input_cost_per_second: 0.02 } },
});
@@ -276,7 +281,8 @@ test("a transcription whose upstream reports no duration charges nothing", async
await new Promise((resolve) => setTimeout(resolve, 100));
assert.deepEqual(ledgerTiers(key.id), []);
assert.equal((await statusOf(key.key)).weeklyExceeded, false);
// The call is unpriced, so `weeklyExceeded` follows the unpriced-usage policy (fail closed by default).
assert.equal((await statusOf(key.key)).weeklySpentUsd, 0);
});
test("modal ledger spend counts in the daily, weekly and 5h windows; the 5h reset follows its timestamp", async () => {