Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions services/auto-routing-benchmark/src/scoreboard/sweep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ describe('readSweepCompletion', () => {
});
});

it.each([
[
'the provider-billed upstream cost for BYOK',
{ cost: 0.0004, is_byok: true, cost_details: { upstream_inference_cost: 0.008 } },
0.008,
],
[
'the router cost without BYOK',
{ cost: 0.012, is_byok: false, cost_details: { upstream_inference_cost: 0.009 } },
0.012,
],
['an unknown cost when BYOK omits the upstream cost', { cost: 0.0004, is_byok: true }, null],
])('reports %s', async (_label, usage, expected) => {
const response = new Response(
`data: {"choices":[{"delta":{"content":"answer"}}]}\n\ndata: ${JSON.stringify({ choices: [], usage })}\n\ndata: [DONE]\n\n`
);
expect((await readSweepCompletion(response, 100, () => 175)).costUsd).toBe(expected);
});

it('accepts explicit null error fields in answer and usage chunks', async () => {
const response = new Response(
[
Expand Down
13 changes: 12 additions & 1 deletion services/auto-routing-benchmark/src/scoreboard/sweep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const streamChunkSchema = z.object({
usage: z
.object({
cost: z.number().nonnegative().nullish(),
is_byok: z.boolean().nullish(),
cost_details: z
.object({ upstream_inference_cost: z.number().nonnegative().nullish() })
.nullish(),
prompt_tokens: z.number().int().nonnegative().nullish(),
completion_tokens: z.number().int().nonnegative().nullish(),
})
Expand Down Expand Up @@ -83,7 +87,14 @@ export async function readSweepCompletion(
}
output += delta.content ?? '';
}
if (chunk.usage?.cost != null) costUsd = chunk.usage.cost;
if (chunk.usage) {
// With BYOK, `cost` is only OpenRouter's fee; the provider bills the upstream inference cost.
const billed =
chunk.usage.is_byok === true
? chunk.usage.cost_details?.upstream_inference_cost
: chunk.usage.cost;
if (billed != null) costUsd = billed;
}
if (chunk.usage?.prompt_tokens != null || chunk.usage?.completion_tokens != null) {
tokenUsage = {
promptTokens: chunk.usage.prompt_tokens ?? tokenUsage?.promptTokens ?? null,
Expand Down