Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(llma): store list price when openrouter is running a promotion (#74972)

Brandond081f54dd0fe
diff --git a/.github/workflows/update-ai-costs.yml b/.github/workflows/update-ai-costs.yml
index 8307609fa815..c1da7390102b 100644
--- a/.github/workflows/update-ai-costs.yml
+++ b/.github/workflows/update-ai-costs.yml
@@ -39,6 +39,11 @@ jobs:
# --ignore-scripts already skips husky's prepare hook (no need for HUSKY=0)
install-command: pnpm install --frozen-lockfile --ignore-scripts
+ # Single definition for the writer and the PR body; a job-level env
+ # block cannot reference the `runner` context.
+ - name: Set discount summary path
+ run: echo "DISCOUNT_SUMMARY_PATH=$RUNNER_TEMP/discount-summary.md" >> "$GITHUB_ENV"
+
- name: Update AI costs
run: |
cd nodejs
@@ -126,15 +131,8 @@ jobs:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PREVIOUS_PR: ${{ steps.previous-pr.outputs.number }}
run: |
- existing_pr=$(gh pr list --head "chore/llma-update-ai-costs" --json number --jq '.[0].number' 2>/dev/null || true)
- if [ -n "$existing_pr" ]; then
- echo "PR #$existing_pr already exists — updated by commit"
- exit 0
- fi
-
- new_pr_url=$(gh pr create \
- --title "chore(llma): Update LLM costs" \
- --body "$(cat <<'BODY'
+ body_file="$RUNNER_TEMP/pr-body.md"
+ cat > "$body_file" <<'BODY'
This is an automated PR to update LLM pricing models.
## Reviewer checklist
@@ -146,7 +144,36 @@ jobs:
- [Google AI (Gemini) Pricing](https://ai.google.dev/pricing)
- [Mistral AI Pricing](https://mistral.ai/pricing#api-pricing)
BODY
- )" \
+
+ # A missing summary annotates and continues: the commit is already
+ # pushed, so the price book shipping outweighs a complete PR body.
+ if [ -f "$DISCOUNT_SUMMARY_PATH" ]; then
+ printf '\n' >> "$body_file"
+ cat "$DISCOUNT_SUMMARY_PATH" >> "$body_file"
+ else
+ echo "::warning::No discount summary at $DISCOUNT_SUMMARY_PATH; PR body omits the promotions section"
+ fi
+
+ # GitHub rejects a body over 65536 characters. Cut on a line
+ # boundary so a multi-byte model id cannot split into invalid UTF-8.
+ if [ "$(wc -c < "$body_file")" -gt 60000 ]; then
+ awk 'BEGIN{n=0} {n+=length($0)+1; if (n>60000) exit; print}' "$body_file" > "$body_file.trunc"
+ printf '\n_Body truncated._\n' >> "$body_file.trunc"
+ mv "$body_file.trunc" "$body_file"
+ fi
+
+ # The branch's open PR already shows the new diff; its body still
+ # describes the previous run.
+ existing_pr=$(gh pr list --head "chore/llma-update-ai-costs" --json number --jq '.[0].number' 2>/dev/null || true)
+ if [ -n "$existing_pr" ]; then
+ echo "PR #$existing_pr already exists, updating its body"
+ gh pr edit "$existing_pr" --body-file "$body_file"
+ exit 0
+ fi
+
+ new_pr_url=$(gh pr create \
+ --title "chore(llma): Update LLM costs" \
+ --body-file "$body_file" \
--base master \
--head chore/llma-update-ai-costs \
--label "automated" \
diff --git a/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.test.ts b/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.test.ts
new file mode 100644
index 000000000000..9741bebd4053
--- /dev/null
+++ b/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.test.ts
@@ -0,0 +1,965 @@
+import fs from 'fs'
+import path from 'path'
+
+import { parseJSON } from '~/common/utils/json-parse'
+
+import {
+ type BuiltModelRow,
+ DISCOUNT_REPORT_ROW_LIMIT,
+ DISCOUNT_SUMMARY_ENV,
+ type DiscountReportEntry,
+ type EndpointCandidate,
+ type RunTotals,
+ UNCHECKED_WARN_FRACTION,
+ accumulateModelRow,
+ buildDefaultCost,
+ buildModelCost,
+ buildModelRow,
+ collectModelRows,
+ confirmDiscountAgainstSiblings,
+ fetchOpenRouterCosts,
+ finalizeTotals,
+ foldModelIntoTotals,
+ parseDiscountRate,
+ readEndpointsFromOpenRouter,
+ renderDiscountReport,
+ sanitizeReportCell,
+ withoutDiscount,
+ writeOutputs,
+} from './update-ai-costs'
+
+/** Build a cost the way the fetch loop does, so tests exercise the real de-discount. */
+const cost = (promptPrice: string, discount = 0, completion = promptPrice): ReturnType<typeof buildModelCost> => {
+ const built = buildModelCost({ prompt: promptPrice, completion, discount })
+ if (!built) {
+ throw new Error(`fixture priced at ${promptPrice} failed to build`)
+ }
+ return built
+}
+
+const candidate = (key: string, promptPrice: string, discount: number): EndpointCandidate => ({
+ key,
+ cost: cost(promptPrice, discount)!,
+ discount,
+})
+
+/** An endpoints-payload entry shaped the way OpenRouter serves it. */
+const endpoint = (tag: string, promptPrice: string, discount = 0): Record<string, unknown> => ({
+ tag,
+ provider_name: tag,
+ pricing: { prompt: promptPrice, completion: promptPrice, discount },
+})
+
+describe('parseDiscountRate()', () => {
+ it.each<{ description: string; discount: unknown; expected: number }>([
+ { description: 'returns 0 when the field is absent', discount: undefined, expected: 0 },
+ { description: 'returns 0 for null', discount: null, expected: 0 },
+ { description: 'returns 0 for an explicit zero', discount: 0, expected: 0 },
+ { description: 'reads a fractional rate', discount: 0.35, expected: 0.35 },
+ { description: 'reads a rate served as a string', discount: '0.5', expected: 0.5 },
+ { description: 'reads a negative rate as a markup', discount: -0.1, expected: -0.1 },
+ { description: 'rejects a rate of exactly 1 (division by zero)', discount: 1, expected: 0 },
+ { description: 'rejects a rate above 1 (would flip the sign)', discount: 1.5, expected: 0 },
+ ])('$description', ({ discount, expected }) => {
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(parseDiscountRate({ discount })).toBe(expected)
+ })
+
+ it('recovers list from a markup, which is a negative rate', () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(parseDiscountRate({ discount: -0.1 }, 'x/y')).toBe(-0.1)
+ expect(warn).not.toHaveBeenCalled()
+ expect(buildModelCost({ prompt: '0.0000011', completion: '0.0000011', discount: -0.1 })!.prompt_token).toBe(
+ 0.000001
+ )
+ })
+
+ it.each<{ description: string; discount: unknown }>([
+ { description: 'an object', discount: {} },
+ { description: 'an array', discount: ['0.5'] },
+ { description: 'a boolean', discount: true },
+ { description: 'an empty string', discount: '' },
+ { description: 'a whitespace string', discount: ' ' },
+ { description: 'an unparseable string', discount: 'abc' },
+ ])('warns on a present but unusable rate: $description', ({ discount }) => {
+ // Read as "no promotion", these store the promo price as list.
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(parseDiscountRate({ discount }, 'x/y')).toBe(0)
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('x/y'))
+ })
+
+ it('stays silent when there is no rate at all', () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ parseDiscountRate({}, 'x/y')
+ expect(warn).not.toHaveBeenCalled()
+ })
+
+ it('warns exactly once on an unparseable string rate', () => {
+ // The decimal parser logs its own failure; one bad field, one line.
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(parseDiscountRate({ discount: 'abc' }, 'x/y')).toBe(0)
+ expect(warn).toHaveBeenCalledTimes(1)
+ })
+
+ it('stays silent on an unparseable rate when warnings are suppressed', () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ parseDiscountRate({ discount: 'abc' }, 'x/y', false)
+ expect(warn).not.toHaveBeenCalled()
+ })
+
+ it('warns naming the model when the rate is out of range', () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ parseDiscountRate({ discount: 2 }, 'openai/gpt-5.6-luna (openai)')
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('openai/gpt-5.6-luna (openai)'))
+ })
+})
+
+describe('withoutDiscount()', () => {
+ it('returns undefined for absent pricing', () => {
+ expect(withoutDiscount(undefined)).toBeUndefined()
+ })
+
+ it('drops the rate and keeps every other field', () => {
+ expect(withoutDiscount({ prompt: '1', completion: '2', discount: 0.5 })).toEqual({
+ prompt: '1',
+ completion: '2',
+ })
+ })
+
+ it('keeps a cost at the served price even when a rate is present', () => {
+ // Without the strip the carve-out rests on the list payload never gaining a rate.
+ const served = { prompt: '0.0000005', completion: '0.000003', discount: 0.5 }
+ expect(buildModelCost(withoutDiscount(served))!.prompt_token).toBe(0.0000005)
+ expect(buildModelCost(served)!.prompt_token).toBe(0.000001)
+ })
+})
+
+describe('buildDefaultCost()', () => {
+ it('keeps the served price when the list payload carries a rate', () => {
+ expect(buildDefaultCost({ prompt: '0.0000005', completion: '0.000003', discount: 0.5 })!.prompt_token).toBe(
+ 0.0000005
+ )
+ })
+
+ it('behaves like the plain builder when there is no rate', () => {
+ expect(buildDefaultCost({ prompt: '0.000001', completion: '0.000006' })).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ })
+ })
+})
+
+describe('buildModelCost() discount handling', () => {
+ it('returns null when pricing is absent', () => {
+ expect(buildModelCost(undefined)).toBeNull()
+ })
+
+ it('leaves an undiscounted endpoint untouched', () => {
+ expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', discount: 0 })).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ })
+ })
+
+ it('leaves pricing untouched when no discount field is present at all', () => {
+ expect(buildModelCost({ prompt: '0.000001', completion: '0.000006' })).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ })
+ })
+
+ it('divides a promo price back up to list', () => {
+ // Must land exactly on the undiscounted azure sibling.
+ expect(buildModelCost({ prompt: '0.0000005', completion: '0.000003', discount: 0.5 })).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ })
+ })
+
+ it('applies the rate uniformly to every field, flat fees included', () => {
+ // web_search is a per-call fee and is discounted at the same ratio.
+ expect(
+ buildModelCost({
+ prompt: '0.0000005',
+ completion: '0.000003',
+ input_cache_read: '0.00000005',
+ web_search: '0.005',
+ discount: 0.5,
+ })
+ ).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ cache_read_token: 0.0000001,
+ web_search: 0.01,
+ })
+ })
+
+ it('does not adjust prices when the rate is out of range', () => {
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', discount: 1 })).toEqual({
+ prompt_token: 0.000001,
+ completion_token: 0.000006,
+ })
+ })
+
+ it('omits optional fields that are absent or zero, as before', () => {
+ expect(buildModelCost({ prompt: '0.000001', completion: '0.000006', web_search: '0', discount: 0.5 })).toEqual({
+ prompt_token: 0.000002,
+ completion_token: 0.000012,
+ })
+ })
+})
+
+describe('buildModelCost() recovers prices the cost book previously recorded', () => {
+ // Expectations are the list prices the cost book itself held before each
+ // promotion began, so a wrong divisor fails on values this suite did not pick.
+ it.each<{ model: string; served: string; discount: number; list: number }>([
+ { model: 'qwen/qwen-plus prompt', served: '0.000000169', discount: 0.35, list: 0.00000026 },
+ { model: 'qwen/qwen-plus completion', served: '0.000000507', discount: 0.35, list: 0.00000078 },
+ { model: 'qwen/qwen3-max prompt', served: '0.000000507', discount: 0.35, list: 0.00000078 },
+ { model: 'qwen/qwen3-max completion', served: '0.000002535', discount: 0.35, list: 0.0000039 },
+ { model: 'qwen/qwen3-14b prompt', served: '0.000000147875', discount: 0.35, list: 0.0000002275 },
+ { model: 'qwen/qwen3-235b-a22b prompt', served: '0.00000029575', discount: 0.35, list: 0.000000455 },
+ { model: 'qwen/qwen3-coder-flash prompt', served: '0.00000012675', discount: 0.35, list: 0.000000195 },
+ { model: 'qwen/qwen3.6-max-preview prompt', served: '0.000000832', discount: 0.2, list: 0.00000104 },
+ { model: 'qwen/qwen3.5-plus-20260420 prompt', served: '0.000000225', discount: 0.25, list: 0.0000003 },
+ { model: 'qwen/qwen3.6-flash prompt', served: '0.000000140625', discount: 0.25, list: 0.0000001875 },
+ { model: 'openai/gpt-5.6-luna prompt', served: '0.0000005', discount: 0.5, list: 0.000001 },
+ { model: 'openai/gpt-5.6-terra prompt', served: '0.00000125', discount: 0.5, list: 0.0000025 },
+ ])('$model', ({ served, discount, list }) => {
+ expect(cost(served, discount)!.prompt_token).toBe(list)
+ })
+})
+
+describe('buildModelRow()', () => {
+ const listPricing = { prompt: '0.0000005', completion: '0.0000005' }
+ const listCost = cost('0.0000005')!
+
+ it('reports nothing was checked when there are no endpoints', () => {
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [])
+ expect(built!.checked).toBe(false)
+ expect(built!.discount).toBeUndefined()
+ expect(built!.cost).toEqual({ default: listCost })
+ })
+
+ it('reports nothing was checked when every endpoint fails to parse', () => {
+ // A non-empty payload can still yield no usable pricing.
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
+ { tag: 'openai', pricing: { prompt: 'x' } },
+ {},
+ ])
+ expect(built!.checked).toBe(false)
+ expect(Object.keys(built!.cost)).toStrictEqual(['default'])
+ })
+
+ it('stores each endpoint at its de-discounted list price', () => {
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
+ endpoint('openai', '0.0000005', 0.5),
+ endpoint('azure', '0.000001'),
+ ])
+ expect(built!.checked).toBe(true)
+ expect(built!.cost.openai.prompt_token).toBe(0.000001)
+ expect(built!.cost.azure.prompt_token).toBe(0.000001)
+ })
+
+ it('leaves `default` exactly as OpenRouter served it', () => {
+ // provider-matching.ts resolves `$ai_provider: openrouter` onto `default`,
+ // and OpenRouter does bill the promo rate, so it must not move.
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('openai', '0.0000005', 0.5)])
+ expect(built!.cost.default).toEqual(listCost)
+ expect(built!.cost.default.prompt_token).toBe(0.0000005)
+ })
+
+ it('reports the discounted endpoints and the confirmation verdict', () => {
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [
+ endpoint('openai', '0.0000005', 0.5),
+ endpoint('azure', '0.000001'),
+ ])
+ expect(built!.discount).toEqual({
+ model: 'openai/gpt-5.6-luna',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'confirmed' as const }],
+ })
+ })
+
+ it('reports no discount entry when every endpoint is at list price', () => {
+ const built = buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('azure', '0.000001')])
+ expect(built!.checked).toBe(true)
+ expect(built!.discount).toBeUndefined()
+ })
+
+ it('never lets an endpoint claim the `default` key', () => {
+ // Otherwise it overwrites the served price with a de-discounted one.
+ const built = buildModelRow('evil/model', listPricing, [endpoint('default', '0.0000009', 0.5)])
+ expect(built!.cost.default).toEqual(listCost)
+ expect(built!.cost['provider-default'].prompt_token).toBe(0.0000018)
+ })
+
+ it('reports not-checkable when no undiscounted sibling exists', () => {
+ // Every Qwen model: Alibaba is the only route, so nothing corroborates it.
+ const built = buildModelRow('qwen/qwen-plus', listPricing, [endpoint('alibaba-fp8', '0.000000169', 0.35)])
+ expect(built!.discount?.endpoints[0].confirmation).toBe('not-checkable')
+ })
+
+ it('warns once, naming the model, on an out-of-range rate', () => {
+ // Parsed twice per endpoint; a second warn would be unattributable.
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ buildModelRow('openai/gpt-5.6-luna', listPricing, [endpoint('openai', '0.000001', 1.5)])
+ expect(warn).toHaveBeenCalledTimes(1)
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('openai/gpt-5.6-luna'))
+ })
+
+ it('returns null when the model has no usable list pricing', () => {
+ expect(buildModelRow('x/y', { prompt: 'nonsense' }, [])).toBeNull()
+ })
+
+ it('keeps `default` at the served price even if the list payload gains a rate', () => {
+ const built = buildModelRow('x/y', { ...listPricing, discount: 0.5 }, [endpoint('openai', '0.0000005', 0.5)])
+ expect(built!.cost.default.prompt_token).toBe(0.0000005)
+ expect(built!.cost.openai.prompt_token).toBe(0.000001)
+ })
+
+ it('confines a hostile provider name to a safe key', () => {
+ // Interpolated into generated TypeScript, so an apostrophe must not reach it.
+ const built = buildModelRow('evil/model', listPricing, [
+ { tag: '!!!', provider_name: "o'brien <script>", pricing: { prompt: '0.000001', completion: '0.000001' } },
+ ])
+ const key = Object.keys(built!.cost).find((k) => k !== 'default')!
+ expect(key).toBe('provider-o-brien-script')
+ expect(key).toMatch(/^[a-z0-9-]+$/)
+ })
+})
+
+describe('confirmDiscountAgainstSiblings()', () => {
+ it('is not checkable for an undiscounted route', () => {
+ const azure = candidate('azure', '0.000001', 0)
+ expect(confirmDiscountAgainstSiblings(azure, [azure])).toBe('not-checkable')
+ })
+
+ it('is not checkable without an undiscounted sibling', () => {
+ // Every Qwen model: Alibaba is the only first-party route.
+ const alibaba = candidate('alibaba-fp8', '0.000000169', 0.35)
+ expect(confirmDiscountAgainstSiblings(alibaba, [alibaba])).toBe('not-checkable')
+ })
+
+ it('confirms when the de-discounted price lands on an undiscounted sibling', () => {
+ const openai = candidate('openai', '0.0000005', 0.5)
+ expect(confirmDiscountAgainstSiblings(openai, [openai, candidate('azure', '0.000001', 0)])).toBe('confirmed')
+ })
+
+ it('reports unconfirmed when the recovered price matches no sibling', () => {
+ const openai = candidate('openai', '0.0000005', 0.5)
+ expect(confirmDiscountAgainstSiblings(openai, [openai, candidate('azure', '0.000009', 0)])).toBe('unconfirmed')
+ })
+
+ it('judges each promoted route on its own', () => {
+ const flex = candidate('openai-flex', '0.00000025', 0.5)
+ const openai = candidate('openai', '0.0000005', 0.5)
+ const all = [flex, openai, candidate('azure', '0.000001', 0)]
+ expect(confirmDiscountAgainstSiblings(openai, all)).toBe('confirmed')
+ expect(confirmDiscountAgainstSiblings(flex, all)).toBe('unconfirmed')
+ })
+
+ it('compares on the prompt price, not the completion price', () => {
+ // Every other fixture sets completion equal to prompt.
+ const discounted: EndpointCandidate = {
+ key: 'openai',
+ cost: buildModelCost({ prompt: '0.0000005', completion: '0.000009', discount: 0.5 })!,
+ discount: 0.5,
+ }
+ const sibling: EndpointCandidate = {
+ key: 'azure',
+ cost: buildModelCost({ prompt: '0.000001', completion: '0.000002' })!,
+ discount: 0,
+ }
+ expect(confirmDiscountAgainstSiblings(discounted, [discounted, sibling])).toBe('confirmed')
+ })
+})
+
+describe('sanitizeReportCell()', () => {
+ it.each<{ description: string; input: string; expected: string }>([
+ {
+ description: 'passes an ordinary model id through',
+ input: 'openai/gpt-5.6-luna',
+ expected: 'openai/gpt-5.6-luna',
+ },
+ { description: 'folds newlines so a row cannot be split', input: 'a\nb\r\nc', expected: 'a b c' },
+ { description: 'strips pipes so a row cannot gain columns', input: 'a|b', expected: 'ab' },
+ {
+ description: 'strips angle brackets and everything else outside the allowlist',
+ input: '<img src=x>',
+ expected: 'img srcx',
+ },
+ { description: 'strips backticks so code spans cannot escape', input: 'a`b', expected: 'ab' },
+ {
+ description: 'strips image syntax so a remote image cannot load in the PR body',
+ input: '![pricing](https://example.com/pixel)',
+ expected: 'pricinghttps://example.com/pixel',
+ },
+ {
+ description: 'strips link syntax so no clickable target survives',
+ input: '[click](https://evil.example)',
+ expected: 'clickhttps://evil.example',
+ },
+ ])('$description', ({ input, expected }) => {
+ expect(sanitizeReportCell(input)).toBe(expected)
+ })
+
+ it('bounds the cell length', () => {
+ expect(sanitizeReportCell('x'.repeat(500))).toHaveLength(120)
+ })
+})
+
+describe('renderDiscountReport()', () => {
+ const entry = (over: Partial<DiscountReportEntry> = {}): DiscountReportEntry => ({
+ model: 'openai/gpt-5.6-luna',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'confirmed' as const }],
+ ...over,
+ })
+
+ it('says so plainly when nothing is discounted', () => {
+ expect(renderDiscountReport([])).toContain('No discounted endpoints found')
+ })
+
+ it('names each discounted model, endpoint and rate', () => {
+ const report = renderDiscountReport([entry()])
+ expect(report).toContain('openai/gpt-5.6-luna')
+ expect(report).toContain('`openai`')
+ expect(report).toContain('50%')
+ })
+
+ it('counts endpoints and models, and reports the confirmed ratio', () => {
+ // Numerator and denominator must differ, or a checkable-count reads the same.
+ const report = renderDiscountReport([
+ entry(),
+ entry({
+ model: 'aaa/unconfirmed',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'unconfirmed' as const }],
+ }),
+ entry({
+ model: 'qwen/qwen-plus',
+ endpoints: [
+ { key: 'alibaba-fp8', discount: 0.35, confirmation: 'not-checkable' as const },
+ { key: 'streamlake', discount: 0.4, confirmation: 'not-checkable' as const },
+ ],
+ }),
+ ])
+ expect(report).toContain('**4 endpoint(s)**')
+ expect(report).toContain('**3 model(s)**')
+ expect(report).toContain('1/2 checkable')
+ })
+
+ it('puts the confirmation verdict on the row it belongs to', () => {
+ const report = renderDiscountReport([
+ entry({ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'unconfirmed' as const }] }),
+ ])
+ expect(report).toContain('| unconfirmed |')
+ })
+
+ it('blanks the model on a continuation row and keeps that route verdict', () => {
+ const report = renderDiscountReport([
+ entry({
+ endpoints: [
+ { key: 'openai', discount: 0.5, confirmation: 'confirmed' as const },
+ { key: 'openai-flex', discount: 0.5, confirmation: 'confirmed' as const },
+ ],
+ }),
+ ])
+ const rows = report.split('\n').filter((line) => line.includes('50%'))
+ expect(rows[0]).toContain('openai/gpt-5.6-luna')
+ expect(rows[1]).not.toContain('openai/gpt-5.6-luna')
+ expect(rows[1]).toContain('confirmed')
+ })
+
+ it('reports models it could not check rather than claiming a clean run', () => {
+ // A run whose endpoint calls all failed must not read as "no promotions".
+ expect(renderDiscountReport([], 17)).toContain('17 model(s) could not be checked')
+ expect(renderDiscountReport([entry()], 17)).toContain('17 model(s) could not be checked')
+ })
+
+ it('says nothing about unchecked models when every model was checked', () => {
+ expect(renderDiscountReport([entry()], 0)).not.toContain('could not be checked')
+ })
+
+ const manyEntries = (count: number, endpointsEach = 1): DiscountReportEntry[] =>
+ Array.from({ length: count }, (_, index) =>
+ entry({
+ model: `vendor/model-${String(index).padStart(4, '0')}`,
+ endpoints: Array.from({ length: endpointsEach }, (_, e) => ({
+ key: `k${e}`,
+ discount: 0.5,
+ confirmation: 'confirmed' as const,
+ })),
+ })
+ )
+
+ it('pins the row limit to a literal, so the cap cannot drift with the code', () => {
+ expect(DISCOUNT_REPORT_ROW_LIMIT).toBe(200)
+ })
+
+ it('lists everything and says nothing about omissions at exactly the limit', () => {
+ const report = renderDiscountReport(manyEntries(DISCOUNT_REPORT_ROW_LIMIT))
+ expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(DISCOUNT_REPORT_ROW_LIMIT)
+ expect(report).not.toContain('more discounted model(s)')
+ })
+
+ it('omits exactly one model at one over the limit', () => {
+ const report = renderDiscountReport(manyEntries(DISCOUNT_REPORT_ROW_LIMIT + 1))
+ expect(report).toContain('and 1 more discounted model(s) not listed')
+ })
+
+ it('caps on rendered rows, not models, since a model can carry many endpoints', () => {
+ // The widest model carries 32 endpoints, so a model cap under-counts rows.
+ const report = renderDiscountReport(manyEntries(20, 32))
+ expect(report.split('\n').filter((line) => line.includes('50%')).length).toBeLessThanOrEqual(
+ DISCOUNT_REPORT_ROW_LIMIT
+ )
+ expect(report).toContain('more discounted model(s) not listed')
+ })
+
+ it('escapes a hostile model id so it cannot break the table', () => {
+ const report = renderDiscountReport([entry({ model: 'evil/x|y\n| pwned | 1 | 2 |' })])
+ expect(report).not.toContain('pwned | 1 | 2 |')
+ expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(1)
+ })
+
+ it('escapes a hostile endpoint key so it cannot break the table', () => {
+ // Sanitized separately from the model id, so it needs its own input.
+ const report = renderDiscountReport([
+ entry({
+ endpoints: [{ key: 'a|b\n| pwned | 9 | 9 |', discount: 0.5, confirmation: 'confirmed' as const }],
+ }),
+ ])
+ expect(report).not.toContain('pwned | 9 | 9 |')
+ expect(report.split('\n').filter((line) => line.includes('50%'))).toHaveLength(1)
+ })
+
+ it('sorts models so the report is stable across runs', () => {
+ const report = renderDiscountReport([entry({ model: 'zzz/model' }), entry({ model: 'aaa/model' })])
+ expect(report.indexOf('aaa/model')).toBeLessThan(report.indexOf('zzz/model'))
+ })
+})
+
+describe('accumulateModelRow()', () => {
+ const totals = (): RunTotals => ({ models: [], discounts: [], uncheckedModels: 0 })
+ const built = (over: Partial<BuiltModelRow> = {}): BuiltModelRow => ({
+ cost: { default: cost('0.000001')! },
+ checked: true,
+ ...over,
+ })
+
+ it('collects the row into the price book', () => {
+ const acc = accumulateModelRow(built(), 'openai/gpt-5.6-luna', totals())
+ expect(acc.models).toStrictEqual([{ model: 'openai/gpt-5.6-luna', cost: { default: cost('0.000001') } }])
+ })
+
+ it('collects a discount entry when the row reports one', () => {
+ const discount = {
+ model: 'm',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'confirmed' as const }],
+ }
+ expect(accumulateModelRow(built({ discount }), 'm', totals()).discounts).toStrictEqual([discount])
+ })
+
+ it('counts a row that could not be checked', () => {
+ // The wire from `checked` to the summary, not just its two ends.
+ expect(accumulateModelRow(built({ checked: false }), 'm', totals()).uncheckedModels).toBe(1)
+ })
+
+ it('accumulates across successive calls', () => {
+ // Every field must accumulate the same way, or one silently stays behind.
+ let acc = totals()
+ acc = accumulateModelRow(built({ checked: false }), 'a', acc)
+ acc = accumulateModelRow(built({ checked: false }), 'b', acc)
+ expect(acc.models).toHaveLength(2)
+ expect(acc.uncheckedModels).toBe(2)
+ })
+
+ it('leaves the totals it was handed untouched', () => {
+ const base = totals()
+ const discount: DiscountReportEntry = {
+ model: 'a',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'confirmed' }],
+ }
+ accumulateModelRow(built({ checked: false, discount }), 'a', base)
+ expect(base.models).toHaveLength(0)
+ expect(base.discounts).toHaveLength(0)
+ expect(base.uncheckedModels).toBe(0)
+ })
+
+ it('does not count a row that was checked', () => {
+ expect(accumulateModelRow(built({ checked: true }), 'm', totals()).uncheckedModels).toBe(0)
+ })
+})
+
+describe('foldModelIntoTotals()', () => {
+ const listPricing = { prompt: '0.0000005', completion: '0.0000005' }
+ const start = (): RunTotals => ({ models: [], discounts: [], uncheckedModels: 0 })
+
+ it('folds a priced model into the totals', () => {
+ const out = foldModelIntoTotals('a/b', listPricing, [], start())
+ expect(out.models).toHaveLength(1)
+ expect(out.uncheckedModels).toBe(1)
+ })
+
+ it('skips a model whose list pricing will not parse', () => {
+ // Without the skip the price book gains a row whose default is null.
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ const out = foldModelIntoTotals('a/b', { prompt: 'nonsense' }, [], start())
+ expect(out.models).toHaveLength(0)
+ })
+
+ it('leaves the totals it was handed untouched', () => {
+ const base = start()
+ foldModelIntoTotals('a/b', listPricing, [], base)
+ expect(base.models).toHaveLength(0)
+ expect(base.uncheckedModels).toBe(0)
+ })
+
+ it('keeps what was already collected when it skips', () => {
+ // From empty, "added nothing" and "discarded everything" look the same.
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ const seeded = foldModelIntoTotals('good/one', listPricing, [endpoint('openai', '0.000001', 0.5)], start())
+ expect(seeded.models).toHaveLength(1)
+ const after = foldModelIntoTotals('bad/one', { prompt: 'nonsense' }, [], seeded)
+ expect(after.models).toHaveLength(1)
+ expect(after.discounts).toHaveLength(1)
+ expect(after.uncheckedModels).toBe(seeded.uncheckedModels)
+ })
+
+ it('files the row and the discount under the model id it was given', () => {
+ const out = foldModelIntoTotals('a/b', listPricing, [endpoint('openai', '0.000001', 0.5)], start())
+ expect(out.models[0].model).toBe('a/b')
+ expect(out.discounts[0].model).toBe('a/b')
+ })
+
+ it('warns naming the model it skipped', () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ foldModelIntoTotals('a/b', undefined, [], start())
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('Skipping'), 'a/b')
+ })
+})
+
+describe('finalizeTotals()', () => {
+ const row = (model: string) => ({ model, cost: { default: cost('0.000001')! } })
+
+ it('orders models by id so output does not follow response order', () => {
+ const out = finalizeTotals({ models: [row('z/z'), row('a/a')], discounts: [], uncheckedModels: 0 })
+ expect(out.models.map((m) => m.model)).toStrictEqual(['a/a', 'z/z'])
+ })
+
+ it('leaves the totals it was handed untouched', () => {
+ // Same contract accumulateModelRow carries.
+ const base: RunTotals = { models: [row('z/z'), row('a/a')], discounts: [], uncheckedModels: 0 }
+ finalizeTotals(base)
+ expect(base.models.map((m) => m.model)).toStrictEqual(['z/z', 'a/a'])
+ })
+
+ it('carries the other totals through unchanged', () => {
+ const out = finalizeTotals({ models: [], discounts: [], uncheckedModels: 4 })
+ expect(out.uncheckedModels).toBe(4)
+ })
+})
+
+describe('collectModelRows()', () => {
+ const priced = (id: string) => ({ id, pricing: { prompt: '0.0000005', completion: '0.0000005' } })
+ const noEndpoints = (): Promise<unknown[]> => Promise.resolve([])
+
+ it('orders the price book by model id', () => {
+ // Response order rewrites the whole file each run and opens a PR on noise.
+ return collectModelRows([priced('z/z'), priced('a/a')], noEndpoints).then((totals) => {
+ expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a', 'z/z'])
+ })
+ })
+
+ it('skips an entry with no id and keeps going', async () => {
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ const totals = await collectModelRows([{}, priced('a/a')], noEndpoints)
+ expect(totals.models).toHaveLength(1)
+ expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a'])
+ })
+
+ it('skips an entry with no id even when its pricing is valid', async () => {
+ // An id-less entry with parseable pricing is the only input that tells the
+ // id guard apart from the pricing guard below it.
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ const totals = await collectModelRows(
+ [{ pricing: { prompt: '0.000001', completion: '0.000001' } }, priced('a/a')],
+ noEndpoints
+ )
+ expect(totals.models.map((m) => m.model)).toStrictEqual(['a/a'])
+ })
+
+ it('reports the unchecked count with its denominator', async () => {
+ // Assert the whole phrase: the per-model progress line also carries an
+ // "n/m" and would satisfy a bare substring check.
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ jest.spyOn(console, 'log').mockImplementation(() => {})
+ await collectModelRows([priced('a/a'), priced('b/b')], noEndpoints)
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('2/2 model(s) had no usable endpoint pricing'))
+ })
+
+ it('warns rather than logs once too much of the catalogue is unchecked', async () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ jest.spyOn(console, 'log').mockImplementation(() => {})
+ await collectModelRows([priced('a/a')], noEndpoints)
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('degraded'))
+ })
+
+ it('stays at log level when the unchecked share is within the steady state', async () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ const log = jest.spyOn(console, 'log').mockImplementation(() => {})
+ // One unchecked model in a catalogue large enough to stay under the share.
+ const models = [priced('a/a'), ...Array.from({ length: 20 }, (_, i) => priced(`m/${i}`))]
+ await collectModelRows(models, (id) => Promise.resolve(id === 'a/a' ? [] : [endpoint('openai', '0.000001')]))
+ expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('degraded'))
+ expect(log).toHaveBeenCalledWith(expect.stringContaining('1/21 model(s) had no usable endpoint pricing'))
+ })
+
+ it('pins the unchecked warn share to a literal', () => {
+ expect(UNCHECKED_WARN_FRACTION).toBe(0.1)
+ })
+
+ it('counts models whose endpoints came back empty', async () => {
+ const totals = await collectModelRows([priced('a/a'), priced('b/b')], noEndpoints)
+ expect(totals.uncheckedModels).toBe(2)
+ })
+
+ it('collects discounts from the endpoints it is handed', async () => {
+ const totals = await collectModelRows([priced('a/a')], () =>
+ Promise.resolve([endpoint('openai', '0.0000005', 0.5), endpoint('azure', '0.000001')])
+ )
+ expect(totals.discounts).toHaveLength(1)
+ expect(totals.discounts[0].endpoints[0].confirmation).toBe('confirmed')
+ expect(totals.models[0].cost.openai.prompt_token).toBe(0.000001)
+ })
+
+ it('reads endpoints once per model, by id', async () => {
+ const seen: string[] = []
+ await collectModelRows([priced('a/a'), priced('b/b')], (id) => {
+ seen.push(id)
+ return Promise.resolve([])
+ })
+ expect(seen).toStrictEqual(['a/a', 'b/b'])
+ })
+})
+
+describe('readEndpointsFromOpenRouter()', () => {
+ const mockFetch = (impl: () => unknown): jest.SpyInstance =>
+ jest.spyOn(global, 'fetch' as never).mockImplementation(impl as never)
+
+ it('returns the endpoints the payload carries', async () => {
+ mockFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ data: { endpoints: [1, 2] } }) }))
+ await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([1, 2])
+ })
+
+ it('degrades to no endpoints on a non-ok response, and says so', async () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ mockFetch(() => Promise.resolve({ ok: false, status: 429, statusText: 'Too Many Requests' }))
+ await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([])
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('a/b'))
+ })
+
+ it('degrades to no endpoints when the request throws, and says so', async () => {
+ const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
+ mockFetch(() => Promise.reject(new Error('socket hang up')))
+ await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([])
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('Error fetching'), 'a/b', expect.anything())
+ })
+
+ it('degrades to no endpoints when the payload has no endpoints key', async () => {
+ mockFetch(() => Promise.resolve({ ok: true, json: () => Promise.resolve({}) }))
+ await expect(readEndpointsFromOpenRouter('a/b')).resolves.toStrictEqual([])
+ })
+
+ it('encodes each path segment of the model id', async () => {
+ let requested = ''
+ mockFetch(((url: string) => {
+ requested = url
+ return Promise.resolve({ ok: true, json: () => Promise.resolve({ data: { endpoints: [] } }) })
+ }) as never)
+ await readEndpointsFromOpenRouter('anthropic/claude-sonnet-5:batch')
+ expect(requested).toContain('anthropic/claude-sonnet-5%3Abatch')
+ })
+})
+
+describe('fetchOpenRouterCosts()', () => {
+ const listPayload = {
+ data: [{ id: 'a/b', pricing: { prompt: '0.0000005', completion: '0.0000005' } }],
+ }
+ const endpointsPayload = { data: { endpoints: [endpoint('openai', '0.0000005', 0.5)] } }
+
+ const mockFetch = (): jest.SpyInstance =>
+ jest.spyOn(global, 'fetch' as never).mockImplementation(((url: string) =>
+ Promise.resolve({
+ ok: true,
+ json: () => Promise.resolve(url.includes('/endpoints') ? endpointsPayload : listPayload),
+ })) as never)
+
+ it('reads per-endpoint pricing, not just the list payload', async () => {
+ // Pins that production hands the loop the real reader, not a fake.
+ jest.spyOn(console, 'log').mockImplementation(() => {})
+ mockFetch()
+ const totals = await fetchOpenRouterCosts()
+ expect(totals.models[0].cost.openai.prompt_token).toBe(0.000001)
+ expect(totals.uncheckedModels).toBe(0)
+ })
+
+ it('reads the models out of the payload envelope', async () => {
+ jest.spyOn(console, 'log').mockImplementation(() => {})
+ mockFetch()
+ const totals = await fetchOpenRouterCosts()
+ expect(totals.models.map((m) => m.model)).toStrictEqual(['a/b'])
+ })
+
+ it('throws when the models list cannot be fetched', async () => {
+ jest.spyOn(global, 'fetch' as never).mockImplementation((() =>
+ Promise.resolve({ ok: false, status: 503, statusText: 'Service Unavailable' })) as never)
+ await expect(fetchOpenRouterCosts()).rejects.toThrow('Failed to fetch OpenRouter models')
+ })
+
+ it('throws when the models list is not JSON', async () => {
+ jest.spyOn(global, 'fetch' as never).mockImplementation((() =>
+ Promise.resolve({ ok: true, json: () => Promise.reject(new Error('bad json')) })) as never)
+ await expect(fetchOpenRouterCosts()).rejects.toThrow('Failed to parse OpenRouter API response')
+ })
+})
+
+describe('writeOutputs()', () => {
+ const summaryPath = '/tmp/does-not-matter-discount-summary.md'
+
+ afterEach(() => {
+ delete process.env[DISCOUNT_SUMMARY_ENV]
+ })
+
+ /** Record both halves of every write, so content is pinned and not just the path. */
+ const captureWrites = (): Array<[string, string]> => {
+ const writes: Array<[string, string]> = []
+ jest.spyOn(fs, 'writeFileSync').mockImplementation(((file: string, body: string) => {
+ writes.push([String(file), String(body)])
+ }) as never)
+ return writes
+ }
+
+ it('writes the price book before the summary', () => {
+ // Ordering is the protection: the summary only decorates a PR body, so it
+ // must never be the write that discards a completed run.
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ const writes = captureWrites()
+
+ writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)
+
+ const priceBook = writes.findIndex(([f]) => f.endsWith('llm-costs.json'))
+ const summary = writes.findIndex(([f]) => f === summaryPath)
+ // Both must actually happen; a missing price-book write would otherwise
+ // satisfy a bare index comparison at -1.
+ expect(priceBook).toBeGreaterThanOrEqual(0)
+ expect(summary).toBeGreaterThanOrEqual(0)
+ expect(priceBook).toBeLessThan(summary)
+ })
+
+ it('writes the rows it was handed into the price book', () => {
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ const writes = captureWrites()
+ const rows = [{ model: 'm', cost: { default: cost('0.000001')! } }]
+
+ writeOutputs(rows, [], 0)
+
+ const [, body] = writes.find(([f]) => f.endsWith('llm-costs.json'))!
+ expect(parseJSON(body)).toEqual(rows)
+ })
+
+ it('carries the unchecked count into the summary it writes', () => {
+ // The count is produced five hops upstream; this is the hop that hands it
+ // to the report, and without it the body reads as a verified clean run.
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ const writes = captureWrites()
+
+ writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 17)
+
+ const [, body] = writes.find(([f]) => f === summaryPath)!
+ expect(body).toContain('17 model(s) could not be checked')
+ })
+
+ it('renders the promotions it was handed into the summary', () => {
+ // Every other call passes an empty list, which pins nothing.
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ const writes = captureWrites()
+ const promo: DiscountReportEntry = {
+ model: 'openai/gpt-5.6-luna',
+ endpoints: [{ key: 'openai', discount: 0.5, confirmation: 'confirmed' }],
+ }
+
+ writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [promo], 0)
+
+ const [, body] = writes.find(([f]) => f === summaryPath)!
+ expect(body).toContain('openai/gpt-5.6-luna')
+ expect(body).not.toContain('No discounted endpoints found')
+ })
+
+ it('generates the provider union alongside the price book', () => {
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ const writes = captureWrites()
+
+ writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)
+
+ expect(writes.some(([f]) => f.endsWith('canonical-providers.ts'))).toBe(true)
+ })
+
+ it('does not throw when the summary write fails', () => {
+ process.env[DISCOUNT_SUMMARY_ENV] = summaryPath
+ jest.spyOn(console, 'warn').mockImplementation(() => {})
+ jest.spyOn(fs, 'writeFileSync').mockImplementation(((file: string) => {
+ if (String(file) === summaryPath) {
+ throw new Error('scratch disk full')
+ }
+ }) as never)
+
+ expect(() => writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)).not.toThrow()
+ })
+
+ it('writes no summary when the env var is unset', () => {
+ const writes = captureWrites()
+
+ writeOutputs([{ model: 'm', cost: { default: cost('0.000001')! } }], [], 0)
+
+ expect(writes.some(([f]) => f === summaryPath)).toBe(false)
+ })
+})
+
+describe('workflow contract', () => {
+ it('sets the same env var the script reads', () => {
+ // Two hand-maintained strings in different languages. Nothing else binds
+ // them, and a rename drops the promotions section from the PR body.
+ const workflow = fs.readFileSync(
+ path.join(__dirname, '../../../../../../../.github/workflows/update-ai-costs.yml'),
+ 'utf8'
+ )
+ expect(workflow).toContain(`${DISCOUNT_SUMMARY_ENV}=`) // the step that sets it
+ // The exact guard and the exact read, so renaming either one fails here.
+ expect(workflow).toContain(`[ -f "$${DISCOUNT_SUMMARY_ENV}" ]`)
+ expect(workflow).toContain(`cat "$${DISCOUNT_SUMMARY_ENV}" >> "$body_file"`)
+ // The sink: the assembled body must be what reaches GitHub on both paths.
+ expect(workflow.match(/--body-file "\$body_file"/g) ?? []).toHaveLength(2)
+ })
+})
+
+describe('module import safety', () => {
+ it('does not fetch or write generated files when imported', () => {
+ // Without the guard, importing hits the live API and rewrites the committed
+ // price book. Spies go in before the module loads so the assertion catches it.
+ const fetchSpy = jest
+ .spyOn(global, 'fetch' as never)
+ .mockImplementation((() =>
+ Promise.resolve({ ok: true, json: () => Promise.resolve({ data: [] }) })) as never)
+ const writeSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
+ const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockImplementation(() => undefined)
+
+ jest.isolateModules(() => {
+ require('./update-ai-costs')
+ })
+
+ expect(fetchSpy).not.toHaveBeenCalled()
+ expect(writeSpy).not.toHaveBeenCalled()
+ expect(mkdirSpy).not.toHaveBeenCalled()
+ })
+})
diff --git a/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.ts b/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.ts
index 63e6031896ab..34470565ba5b 100644
--- a/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.ts
+++ b/nodejs/src/ingestion/pipelines/ai/costs/scripts/update-ai-costs.ts
@@ -2,6 +2,8 @@ import fs from 'fs'
import bigDecimal from 'js-big-decimal'
import path from 'path'
+import { normalizeProviderKey } from '~/ingestion/pipelines/ai/costs/provider-matching'
+
interface ModelCost {
prompt_token: number
completion_token: number
@@ -55,7 +57,47 @@ const parsePricingNumber = (value: unknown): number | undefined => {
}
}
-const buildModelCost = (pricing: Record<string, unknown> | undefined): ModelCost | null => {
+/** A provider key means what that provider charges a direct caller, so whatever
+ * OpenRouter applied is divided back out. A negative `discount_to_user` is a
+ * markup and the same division recovers list; only a rate at or above 1 is
+ * invalid, which OpenRouter itself falls back to no rate. */
+export const parseDiscountRate = (pricing: Record<string, unknown>, context?: string, warn = true): number => {
+ const raw = pricing.discount
+ if (raw === undefined || raw === null) {
+ return 0
+ }
+
+ // Classify before parsing: Number() never throws, so an unusable rate cannot
+ // draw a second parse-failure log from parsePricingNumber for the same field.
+ const asNumber =
+ typeof raw === 'number' ? raw : typeof raw === 'string' && raw.trim() !== '' ? Number(raw) : Number.NaN
+ if (!Number.isFinite(asNumber) || asNumber >= 1) {
+ if (warn) {
+ console.warn(`Ignoring unusable discount ${String(raw)} for ${context ?? 'unknown model'}`)
+ }
+ return 0
+ }
+
+ // parsePricingNumber clamps negatives to 0, which would erase a markup.
+ return asNumber < 0 ? asNumber : (parsePricingNumber(raw) ?? 0)
+}
+
+/** Keeps a cost at the served price by hiding the rate from the shared builder. */
+export const withoutDiscount = (pricing: Record<string, unknown> | undefined): Record<string, unknown> | undefined => {
+ if (!pricing) {
+ return undefined
+ }
+ const { discount: _rate, ...rest } = pricing
+ return rest
+}
+
+/** `default` must stay at the served price whatever the list payload carries. */
+export const buildDefaultCost = (
+ modelPricing: Record<string, unknown> | undefined,
+ context?: string
+): ModelCost | null => buildModelCost(withoutDiscount(modelPricing), context)
+
+export const buildModelCost = (pricing: Record<string, unknown> | undefined, context?: string): ModelCost | null => {
if (!pricing) {
return null
}
@@ -67,9 +109,15 @@ const buildModelCost = (pricing: Record<string, unknown> | undefined): ModelCost
return null
}
+ // The rate applies to every field, flat fees included: a 50%-off route serves
+ // web_search at 0.005 against 0.01 on its undiscounted sibling.
+ const discount = parseDiscountRate(pricing, context)
+ const toListPrice = (value: number): number =>
+ discount === 0 ? value : parseFloat((value / (1 - discount)).toPrecision(10))
+
const cost: ModelCost = {
- prompt_token: promptToken,
- completion_token: completionToken,
+ prompt_token: toListPrice(promptToken),
+ completion_token: toListPrice(completionToken),
}
const optionalPricingFields: Array<[keyof ModelCost, string]> = [
@@ -88,39 +136,269 @@ const buildModelCost = (pricing: Record<string, unknown> | undefined): ModelCost
for (const [targetField, sourceField] of optionalPricingFields) {
const parsedValue = parsePricingNumber(pricing[sourceField])
if (parsedValue !== undefined && parsedValue !== 0) {
- cost[targetField] = parsedValue
+ cost[targetField] = toListPrice(parsedValue)
}
}
return cost
}
-const normalizeProviderKey = (endpoint: { tag?: string; provider_name?: string; name?: string }): string => {
- const rawKey = endpoint.tag || endpoint.provider_name || endpoint.name || 'unknown'
- return rawKey
- .toLowerCase()
- .replace(/[^a-z0-9]+/g, '-')
- .replace(/^-+|-+$/g, '')
+export interface EndpointCandidate {
+ key: string
+ cost: ModelCost
+ discount: number
}
-const fetchOpenRouterCosts = async (): Promise<ModelRow[]> => {
- // eslint-disable-next-line no-restricted-globals
- const res = await fetch('https://openrouter.ai/api/v1/models', {})
- if (!res.ok) {
- throw new Error(`Failed to fetch OpenRouter models: ${res.status} ${res.statusText}`)
+/*
+ * `default` keeps the promotion. `PROVIDER_ALIASES` maps `$ai_provider:
+ * 'openrouter'` onto it, and for those callers the promo price is what they were
+ * billed, so raising it to list over-reports them by 1/(1 - rate). Splitting the
+ * two readers needs an `openrouter` key in `CanonicalProvider` first.
+ */
+
+export type DiscountConfirmation = 'confirmed' | 'unconfirmed' | 'not-checkable'
+
+/** Corroborates one route's de-discount against an undiscounted sibling. Per
+ * route, or a verdict rolled up across a model's routes gets displayed against
+ * one it never checked. Evidence only: two hosts legitimately price the same
+ * model differently, so a mismatch fires on half of all checkable routes. */
+export const confirmDiscountAgainstSiblings = (
+ candidate: EndpointCandidate,
+ candidates: EndpointCandidate[]
+): DiscountConfirmation => {
+ const undiscounted = candidates.filter((other) => other.discount === 0)
+ if (candidate.discount === 0 || undiscounted.length === 0) {
+ return 'not-checkable'
}
- let data
+ // Both sides already went through the same rounding, so exact equality holds.
+ return undiscounted.some((other) => other.cost.prompt_token === candidate.cost.prompt_token)
+ ? 'confirmed'
+ : 'unconfirmed'
+}
+
+export interface DiscountReportEntry {
+ model: string
+ endpoints: Array<{ key: string; discount: number; confirmation: DiscountConfirmation }>
+}
+
+/** Share of the catalogue that may go unchecked before the run says so loudly. */
+export const UNCHECKED_WARN_FRACTION = 0.1
+
+/** Table rows rendered before the remainder collapses into a count line. */
+export const DISCOUNT_REPORT_ROW_LIMIT = 200
+
+/** Confines a third-party string to inert text in one table cell. An allowlist,
+ * because a denylist has to anticipate every markdown construct that renders. */
+export const sanitizeReportCell = (value: string): string =>
+ value
+ .replace(/[\r\n]+/g, ' ')
+ .replace(/[^A-Za-z0-9._:/ -]+/g, '')
+ .trim()
+ .slice(0, 120)
+
+/** Renders promotions as line items in the generated PR. `uncheckedModels` is
+ * reported too, so a bare "no discounts" cannot read as a verified negative. */
+export const renderDiscountReport = (entries: DiscountReportEntry[], uncheckedModels = 0): string => {
+ const unchecked =
+ uncheckedModels > 0
+ ? `\n${uncheckedModels} model(s) could not be checked (endpoint pricing unavailable); their prices may still carry a promotion.\n`
+ : ''
+
+ if (entries.length === 0) {
+ return `## Discounts\n\nNo discounted endpoints found in this run.\n${unchecked}`
+ }
+
+ const rows = [...entries].sort((a, b) => a.model.localeCompare(b.model))
+ const verdicts = rows.map((row) => row.endpoints.map((e) => e.confirmation))
+ const confirmed = verdicts.filter((v) => v.includes('confirmed')).length
+ const checkable = verdicts.filter((v) => v.some((c) => c !== 'not-checkable')).length
+ const endpointCount = rows.reduce((total, row) => total + row.endpoints.length, 0)
+
+ const lines = [
+ '## Discounts',
+ '',
+ `OpenRouter is running a promotion on **${endpointCount} endpoint(s)** across **${rows.length} model(s)**.`,
+ 'Per-provider keys below are stored at list rate, with the promotion divided back',
+ 'out, so a provider key keeps meaning what that provider charges a direct caller.',
+ 'The `default` key is left as OpenRouter serves it.',
+ '',
+ `Independently confirmed against an undiscounted sibling route: ${confirmed}/${checkable} checkable model(s).`,
+ unchecked,
+ '| Model | Endpoint | Rate | Confirmed |',
+ '| --- | --- | --- | --- |',
+ ]
+
+ // Bounded on emitted rows, not models: the widest model carries 32 endpoints,
+ // so a model cap would not bound the body GitHub has to accept.
+ let emitted = 0
+ let omittedModels = 0
+ for (const row of rows) {
+ if (emitted + row.endpoints.length > DISCOUNT_REPORT_ROW_LIMIT) {
+ omittedModels += 1
+ continue
+ }
+ for (const [index, endpoint] of row.endpoints.entries()) {
+ const model = index === 0 ? sanitizeReportCell(row.model) : ''
+ const confirmation = endpoint.confirmation
+ lines.push(
+ `| ${model} | \`${sanitizeReportCell(endpoint.key)}\` | ${Math.round(endpoint.discount * 100)}% | ${confirmation} |`
+ )
+ }
+ emitted += row.endpoints.length
+ }
+ if (omittedModels > 0) {
+ lines.push('', `...and ${omittedModels} more discounted model(s) not listed.`)
+ }
+
+ lines.push('')
+ return lines.join('\n')
+}
+
+/** Shared with provider-matching: this writes the keys that module reads. */
+const endpointProviderKey = (endpoint: { tag?: string; provider_name?: string; name?: string }): string =>
+ normalizeProviderKey(endpoint.tag || endpoint.provider_name || endpoint.name || 'unknown')
+
+export interface BuiltModelRow {
+ cost: Record<string, ModelCost>
+ discount?: DiscountReportEntry
+ /** False when no endpoint yielded usable pricing, so nothing was checked for promotions. */
+ checked: boolean
+}
+
+/** Turns one model's payloads into the row that gets written, plus what the
+ * discount report says about it. */
+export const buildModelRow = (
+ modelId: string,
+ modelPricing: Record<string, unknown> | undefined,
+ endpoints: unknown[]
+): BuiltModelRow | null => {
+ const defaultCost = buildDefaultCost(modelPricing, modelId)
+ if (!defaultCost) {
+ return null
+ }
+ const cost: Record<string, ModelCost> = { default: defaultCost }
+ const candidates: EndpointCandidate[] = []
+
+ for (const raw of endpoints) {
+ const endpoint = (raw ?? {}) as { tag?: string; provider_name?: string; pricing?: Record<string, unknown> }
+ const context = `${modelId} (${endpoint.tag ?? '?'})`
+ const endpointCost = buildModelCost(endpoint.pricing, context)
+ if (!endpointCost) {
+ continue
+ }
+
+ const providerKey = endpointProviderKey(endpoint)
+ // Normalized too: every key here is interpolated into canonical-providers.ts
+ // as a string literal, so it must be [a-z0-9-] by construction.
+ const safeProviderKey =
+ providerKey && providerKey !== 'default'
+ ? providerKey
+ : `provider-${normalizeProviderKey(endpoint.provider_name ?? 'unknown') || 'unknown'}`
+
+ cost[safeProviderKey] = endpointCost
+ candidates.push({
+ key: safeProviderKey,
+ cost: endpointCost,
+ discount: parseDiscountRate(endpoint.pricing ?? {}, context, false),
+ })
+ }
+
+ // Keyed on parsed candidates rather than the raw endpoint count: a payload
+ // whose entries all fail to parse yielded nothing to check either.
+ if (candidates.length === 0) {
+ return { cost, checked: false }
+ }
+
+ const discounted = candidates.filter((candidate) => candidate.discount > 0)
+ return {
+ cost,
+ checked: true,
+ discount:
+ discounted.length > 0
+ ? {
+ model: modelId,
+ endpoints: discounted.map((candidate) => ({
+ key: candidate.key,
+ discount: candidate.discount,
+ confirmation: confirmDiscountAgainstSiblings(candidate, candidates),
+ })),
+ }
+ : undefined,
+ }
+}
+
+export interface RunTotals {
+ models: ModelRow[]
+ discounts: DiscountReportEntry[]
+ uncheckedModels: number
+}
+
+/** Folds one built row into the run totals; split out so a test can cover the
+ * wire between what `buildModelRow` reports and what the summary claims. */
+export const accumulateModelRow = (built: BuiltModelRow, modelId: string, totals: RunTotals): RunTotals => ({
+ models: [...totals.models, { model: modelId, cost: built.cost }],
+ discounts: built.discount ? [...totals.discounts, built.discount] : totals.discounts,
+ uncheckedModels: totals.uncheckedModels + (built.checked ? 0 : 1),
+})
+
+/** Skips a model whose list pricing will not parse, keeping what came before. */
+export const foldModelIntoTotals = (
+ modelId: string,
+ modelPricing: Record<string, unknown> | undefined,
+ endpoints: unknown[],
+ totals: RunTotals
+): RunTotals => {
+ const built = buildModelRow(modelId, modelPricing, endpoints)
+ if (!built) {
+ console.warn('Skipping model without valid pricing:', modelId)
+ return totals
+ }
+ return accumulateModelRow(built, modelId, totals)
+}
+
+/** Orders by model id: inheriting response order rewrites the whole file each run. */
+export const finalizeTotals = (totals: RunTotals): RunTotals => ({
+ ...totals,
+ models: [...totals.models].sort((a, b) => a.model.localeCompare(b.model)),
+})
+
+/** Name of the env var carrying the summary path, shared with the workflow that sets it. */
+export const DISCOUNT_SUMMARY_ENV = 'DISCOUNT_SUMMARY_PATH'
+
+/** The summary only decorates a PR body, so it goes last and cannot throw away a
+ * completed run's fetching. */
+export const writeOutputs = (sortedCosts: ModelRow[], discounts: DiscountReportEntry[], unchecked: number): void => {
+ fs.writeFileSync(path.join(PATH_TO_PROVIDERS, OPENROUTER_COSTS_FILENAME), JSON.stringify(sortedCosts, null, 4))
+ console.log(`Wrote OpenRouter costs to ${OPENROUTER_COSTS_FILENAME}`)
+
+ generateCanonicalProviders(sortedCosts)
+
+ const summaryPath = process.env[DISCOUNT_SUMMARY_ENV]
+ console.log(`Found discounted endpoints on ${discounts.length} model(s)`)
+ if (!summaryPath) {
+ return
+ }
try {
- data = await res.json()
- } catch {
- throw new Error('Failed to parse OpenRouter API response as JSON')
+ fs.writeFileSync(summaryPath, renderDiscountReport(discounts, unchecked))
+ console.log(`Wrote discount summary to ${summaryPath}`)
+ } catch (error) {
+ console.warn('Failed to write discount summary:', error)
}
+}
- console.log('OpenRouter models:', data.data.length)
- const models = data.data
+/** Reads one model's endpoints payload. Split out so the loop is testable. */
+export type EndpointFetcher = (modelId: string) => Promise<unknown[]>
+
+interface ListedModel {
+ id?: string
+ pricing?: Record<string, unknown>
+}
- const allModels: ModelRow[] = []
+/** Takes the endpoint reader as an argument so this loop is reachable without a
+ * network. */
+export const collectModelRows = async (models: ListedModel[], readEndpoints: EndpointFetcher): Promise<RunTotals> => {
+ let totals: RunTotals = { models: [], discounts: [], uncheckedModels: 0 }
for (const [modelIndex, model] of models.entries()) {
if (!model?.id) {
@@ -128,65 +406,62 @@ const fetchOpenRouterCosts = async (): Promise<ModelRow[]> => {
continue
}
- const defaultCost = buildModelCost(model.pricing)
- if (!defaultCost) {
- console.warn('Skipping model without valid pricing:', model.id)
- continue
- }
+ console.log(`Fetching endpoint pricing for ${modelIndex + 1}/${models.length} ${model.id}...`)
+ totals = foldModelIntoTotals(model.id, model.pricing, await readEndpoints(model.id), totals)
+ }
- const costs: Record<string, ModelCost> = {
- default: defaultCost,
+ if (totals.uncheckedModels > 0) {
+ // Alias and meta-router models have no per-endpoint pricing, so a handful is
+ // the steady state; the denominator separates that from a degraded API.
+ const line = `${totals.uncheckedModels}/${models.length} model(s) had no usable endpoint pricing`
+ if (totals.uncheckedModels > models.length * UNCHECKED_WARN_FRACTION) {
+ console.warn(`${line}: endpoint pricing looks degraded, per-provider prices are missing`)
+ } else {
+ console.log(line)
}
+ }
+
+ return finalizeTotals(totals)
+}
+
+/** Endpoint reader against the live API. Every failure degrades to no endpoints. */
+export const readEndpointsFromOpenRouter: EndpointFetcher = async (modelId) => {
+ const encoded = modelId
+ .split('/')
+ .map((segment: string) => encodeURIComponent(segment))
+ .join('/')
- const encodedModelId = model.id
- .split('/')
- .map((segment: string) => encodeURIComponent(segment))
- .join('/')
-
- try {
- console.log(`Fetching endpoint pricing for ${modelIndex + 1}/${models.length} ${model.id}...`)
- // eslint-disable-next-line no-restricted-globals
- const endpointRes = await fetch(`https://openrouter.ai/api/v1/models/${encodedModelId}/endpoints`, {})
- if (!endpointRes.ok) {
- console.warn(
- `Failed to fetch endpoint pricing for ${model.id}: ${endpointRes.status} ${endpointRes.statusText}`
- )
- } else {
- let endpointsPayload
- try {
- endpointsPayload = await endpointRes.json()
- } catch (parseError) {
- console.warn('Failed to parse endpoint pricing payload for model:', model.id, parseError)
- }
-
- const endpoints = endpointsPayload?.data?.endpoints ?? []
- for (const endpoint of endpoints) {
- const endpointCost = buildModelCost(endpoint?.pricing)
- if (!endpointCost) {
- continue
- }
-
- const providerKey = normalizeProviderKey(endpoint)
- const safeProviderKey =
- providerKey && providerKey !== 'default'
- ? providerKey
- : `provider-${endpoint.provider_name ?? 'unknown'}`
- costs[safeProviderKey] = endpointCost
- }
- }
- } catch (error) {
- console.warn('Error fetching endpoint pricing for model:', model.id, error)
+ try {
+ // eslint-disable-next-line no-restricted-globals
+ const res = await fetch(`https://openrouter.ai/api/v1/models/${encoded}/endpoints`, {})
+ if (!res.ok) {
+ console.warn(`Failed to fetch endpoint pricing for ${modelId}: ${res.status} ${res.statusText}`)
+ return []
}
+ const payload = await res.json()
+ return payload?.data?.endpoints ?? []
+ } catch (error) {
+ console.warn('Error fetching endpoint pricing for model:', modelId, error)
+ return []
+ }
+}
- allModels.push({
- model: model.id,
- cost: costs,
- })
+export const fetchOpenRouterCosts = async (): Promise<RunTotals> => {
+ // eslint-disable-next-line no-restricted-globals
+ const res = await fetch('https://openrouter.ai/api/v1/models', {})
+ if (!res.ok) {
+ throw new Error(`Failed to fetch OpenRouter models: ${res.status} ${res.statusText}`)
}
- allModels.sort((a, b) => a.model.localeCompare(b.model))
+ let data
+ try {
+ data = await res.json()
+ } catch {
+ throw new Error('Failed to parse OpenRouter API response as JSON')
+ }
- return allModels
+ console.log('OpenRouter models:', data.data.length)
+ return collectModelRows(data.data, readEndpointsFromOpenRouter)
}
const sortProviderCosts = (models: ModelRow[]): ModelRow[] => {
@@ -254,23 +529,22 @@ const main = async () => {
// Fetch costs from both providers
console.log('Fetching costs from OpenRouter...')
- const openRouterCosts = await fetchOpenRouterCosts()
+ const { models: openRouterCosts, discounts, uncheckedModels } = await fetchOpenRouterCosts()
console.log(`Fetched ${openRouterCosts.length} models from OpenRouter`)
// Sort provider costs deterministically (default first, then alphabetically)
const sortedCosts = sortProviderCosts(openRouterCosts)
- // Write OpenRouter costs as backup
- fs.writeFileSync(path.join(PATH_TO_PROVIDERS, OPENROUTER_COSTS_FILENAME), JSON.stringify(sortedCosts, null, 4))
- console.log(`Wrote OpenRouter costs to ${OPENROUTER_COSTS_FILENAME}`)
-
- // Generate canonical providers TypeScript file
- generateCanonicalProviders(sortedCosts)
+ writeOutputs(sortedCosts, discounts, uncheckedModels)
}
-;(async () => {
- await main()
-})().catch((e) => {
- console.error('Error updating AI costs:', e)
- process.exit(1)
-})
+// Only run when invoked directly (`pnpm update-ai-costs`). Importing this
+// module from a test must not fetch the live API or rewrite the generated files.
+if (require.main === module) {
+ ;(async () => {
+ await main()
+ })().catch((e) => {
+ console.error('Error updating AI costs:', e)
+ process.exit(1)
+ })
+}