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
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function buildTypeDetailSegments(
})
} else if (other.model_ratio != null) {
const tokenPrice = (multiplier: number) =>
formatLogTokenPrice(other.model_ratio ?? 0, multiplier, currency)
formatLogTokenPrice(other.model_ratio ?? 0, multiplier, other, currency)
const baseEntries = [tokenPrice(1)]
if (other.completion_ratio != null) {
baseEntries.push(tokenPrice(other.completion_ratio))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function BillingBreakdown(props: {
formatLogAddonPrice(amount, other, currency)
const structuredTools = other.tool_surcharges ?? []
const fmtTokenPrice = (multiplier: number) =>
formatLogTokenPrice(other.model_ratio ?? 0, multiplier, currency)
formatLogTokenPrice(other.model_ratio ?? 0, multiplier, other, currency)

if (isTieredExpr) {
rows.push({
Expand Down Expand Up @@ -402,6 +402,7 @@ function BillingBreakdown(props: {
pricing_schema_version: other.pricing_schema_version,
pricing_currency_basis: item.price_currency_basis,
pricing_unit_credits_per_unit: item.pricing_unit_credits_per_unit,
pricing_credits_per_usd: other.pricing_credits_per_usd,
},
currency
)}/1K)`,
Expand Down Expand Up @@ -1174,7 +1175,7 @@ export function DetailsDialog(props: DetailsDialogProps) {
<DetailSection label={t('Tiered pricing')}>
<DynamicPricingBreakdown
compact
{...logExpressionCurrency(other, currency.config.creditsPerUsd)}
{...logExpressionCurrency(other)}
displayCurrency={currency.currency}
billingExpr={decodeBillingExprB64(other.expr_b64)}
matchedTierLabel={other.matched_tier}
Expand Down
156 changes: 135 additions & 21 deletions apps/web/src/features/usage-logs/lib/money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ const config = {
quotaPerUnit: 2_000_000,
}

function formatter(): LogCurrencyFormatter {
function formatter(
overrides: Partial<typeof config> = {}
): LogCurrencyFormatter {
const currency = 'USD' as const
const currentConfig = { ...config, ...overrides }
return {
currency,
config,
config: currentConfig,
formatQuota: (quota, options) =>
formatQuotaInCurrency(
quota,
currency,
{ ...options, locale: 'en' },
config
currentConfig
),
formatUSD: (amount, options) =>
formatUSDInCurrency(
amount,
currency,
{ ...options, locale: 'en' },
config
currentConfig
),
}
}
Expand Down Expand Up @@ -79,8 +82,29 @@ describe('log monetary snapshots', () => {
)
})
test('converts a frozen legacy absolute price once into USD', () => {
const other = { pricing_unit_credits_per_unit: 500_000 }
assert.equal(formatLogPrice(7, other, formatter()), '7 USD')
const other = {
pricing_unit_credits_per_unit: 500_000,
pricing_credits_per_usd: 3_500_000,
}
const original = JSON.stringify(other)
assert.equal(formatLogPrice(7, other, formatter()), '1 USD')
assert.equal(
formatLogPrice(
7,
other,
formatter({ creditsPerUsd: 1_000_000, cnyPerUsd: 8 })
),
'1 USD'
)
assert.equal(JSON.stringify(other), original)
assert.equal(
formatLogPrice(
7,
{ pricing_unit_credits_per_unit: 500_000 },
formatter()
),
'-'
)
})

test('requires a versioned basis before treating a price as real USD', () => {
Expand All @@ -91,7 +115,12 @@ describe('log monetary snapshots', () => {
assert.equal(
formatLogPrice(
7,
{ pricing_schema_version: 2, pricing_currency_basis: 'USD' },
{
pricing_schema_version: 2,
pricing_currency_basis: 'USD',
pricing_unit_credits_per_unit: 500_000,
pricing_credits_per_usd: 3_500_000,
},
formatter()
),
'7 USD'
Expand All @@ -100,26 +129,84 @@ describe('log monetary snapshots', () => {
})

test('token ratios use Credits per token, independent of the old QPU', () => {
assert.equal(formatLogTokenPrice(3.5, 1, formatter()), '7 USD')
assert.equal(formatLogTokenPrice(3.5, 2, formatter()), '14 USD')
const other = { pricing_credits_per_usd: 500_000 }
assert.equal(formatLogTokenPrice(3.5, 1, other, formatter()), '7 USD')
assert.equal(formatLogTokenPrice(3.5, 2, other, formatter()), '14 USD')
assert.match(
formatLogTokenPrice(0.000_000_01, 1, formatter()),
formatLogTokenPrice(0.000_000_01, 1, other, formatter()),
/^0\.00000002 USD$/
)
})

test('token prices retain their captured scale after the ledger anchor changes', () => {
const frozen = {
model_ratio_usd_per_million_multiplier: 1_000_000 / 3_500_000,
pricing_credits_per_usd: 500_000,
pricing_unit_credits_per_unit: 2_000_000,
}
assert.equal(formatLogTokenPrice(3.5, 1, frozen, formatter()), '1 USD')
assert.equal(formatLogTokenPrice(3.5, 2, frozen, formatter()), '2 USD')
assert.equal(
formatLogTokenPrice(
3.5,
1,
{ pricing_credits_per_usd: 3_500_000 },
formatter()
),
'1 USD'
)
assert.equal(formatLogTokenPrice(3.5, 1, {}, formatter()), '-')
for (const value of [0, -1, Number.NaN, Number.POSITIVE_INFINITY]) {
assert.equal(
formatLogTokenPrice(
3.5,
1,
{ pricing_credits_per_usd: value },
formatter()
),
'-'
)
}
})

test('uses the frozen expression scale and leaves unknown historical prices unavailable', () => {
assert.deepEqual(logExpressionCurrency({}, 3_500_000), {})
assert.deepEqual(logExpressionCurrency({}), {})
assert.equal(formatLogPrice(7, {}, formatter(), true), '-')
const frozen = { billing_expr_usd_multiplier: 1 / 7 }
const frozen = {
billing_expr_usd_multiplier: 1 / 7,
pricing_unit_credits_per_unit: 1_000_000,
pricing_credits_per_usd: 500_000,
}
assert.equal(formatLogPrice(7, frozen, formatter(), true), '1 USD')
assert.equal(
logExpressionCurrency(
{ pricing_unit_credits_per_unit: 500_000 },
3_500_000
).expressionUsdMultiplier,
logExpressionCurrency({
pricing_unit_credits_per_unit: 500_000,
pricing_credits_per_usd: 3_500_000,
}).expressionUsdMultiplier,
1 / 7
)
assert.equal(
formatLogPrice(
7,
{ pricing_unit_credits_per_unit: 500_000 },
formatter(),
true
),
'-'
)
assert.equal(
formatLogPrice(
7,
{
...frozen,
pricing_schema_version: 2,
billing_expr_currency_basis: 'USD',
},
formatter(),
true
),
'7 USD'
)
})

test('copies the displayed bill with its unit while preserving original content verbatim', () => {
Expand Down Expand Up @@ -152,6 +239,7 @@ describe('log monetary snapshots', () => {
matched_tier: 'matched',
pricing_schema_version: 2,
pricing_unit_credits_per_unit: 500_000,
pricing_credits_per_usd: 3_500_000,
billing_expr_usd_multiplier: 1 / 7,
model_ratio: 0.7,
completion_ratio: 2,
Expand All @@ -174,18 +262,44 @@ describe('log monetary snapshots', () => {
assert.match(copy, /Input: 1 USD\/M/)
assert.match(copy, /Output: 2 USD\/M/)
assert.doesNotMatch(copy, /0\.2 USD|0\.4 USD/)
assert.match(copy, /web_search: 2x \(14 USD\/1K\)/)
assert.match(copy, /web_search: 2x \(2 USD\/1K\)/)
})

test('copied standard token prices use the same frozen scale as the bill details', () => {
const other = {
model_ratio: 3.5,
completion_ratio: 2,
pricing_credits_per_usd: 3_500_000,
}
const copy = buildLogCopyText(
'Raw recorded evidence',
{ type: 2, quota: 500_000 } as UsageLog,
other,
formatter(),
(key) => key
)
assert.match(copy, /Input: 1 USD\/M/)
assert.match(copy, /Output: 2 USD\/M/)
assert.ok(copy.endsWith('Raw recorded evidence'))
})
})

test('audio addon prices use their own frozen unit rather than a separately captured tool scale', () => {
const other = {
tool_pricing_unit_credits_per_unit: 500000,
audio_input_pricing_unit_credits_per_unit: 1000000,
pricing_credits_per_usd: 3_500_000,
}
assert.equal(formatLogAddonPrice(7, other, formatter()), '7 USD')
assert.equal(formatLogAddonPrice(7, other, formatter(), 'audio'), '14 USD')
assert.equal(formatLogAddonPrice(7, other, formatter(), 'audio'), '14 USD')
assert.match(formatLogAddonPrice(7, other, formatter(), 'audio'), /^14 USD$/)
assert.equal(formatLogAddonPrice(7, other, formatter()), '1 USD')
assert.equal(formatLogAddonPrice(7, other, formatter(), 'audio'), '2 USD')
assert.equal(
formatLogAddonPrice(
7,
{ ...other, pricing_schema_version: 2, pricing_currency_basis: 'USD' },
formatter(),
'audio'
),
'2 USD'
)
assert.equal(formatLogAddonPrice(7, {}, formatter(), 'audio'), '-')
})
52 changes: 30 additions & 22 deletions apps/web/src/features/usage-logs/lib/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function formatLogPrice(
expression = false
): string {
if (expression) {
const metadata = logExpressionCurrency(other, currency.config.creditsPerUsd)
const metadata = logExpressionCurrency(other)
return metadata.expressionUsdMultiplier == null
? '-'
: currency.formatUSD(
Expand All @@ -42,10 +42,13 @@ export function formatLogPrice(
) {
return currency.formatUSD(amount, PRICE_OPTIONS)
}
if (positiveNumber(other.pricing_unit_credits_per_unit)) {
if (
positiveNumber(other.pricing_unit_credits_per_unit) &&
positiveNumber(other.pricing_credits_per_usd)
) {
return currency.formatUSD(
(amount * other.pricing_unit_credits_per_unit) /
Number(currency.config.creditsPerUsd),
other.pricing_credits_per_usd,
PRICE_OPTIONS
)
}
Expand All @@ -59,25 +62,26 @@ export function formatLogAddonPrice(
currency: LogCurrencyFormatter,
kind: 'tool' | 'audio' = 'tool'
): string {
const creditsPerUnit =
kind === 'audio'
? (other.audio_input_pricing_unit_credits_per_unit ??
other.tool_pricing_unit_credits_per_unit)
: other.tool_pricing_unit_credits_per_unit
return formatLogPrice(
amount,
{
...other,
pricing_unit_credits_per_unit:
kind === 'audio'
? (other.audio_input_pricing_unit_credits_per_unit ??
other.tool_pricing_unit_credits_per_unit)
: other.tool_pricing_unit_credits_per_unit,
pricing_currency_basis: positiveNumber(creditsPerUnit)
? 'legacy_pricing_unit'
: other.pricing_currency_basis,
pricing_unit_credits_per_unit: creditsPerUnit,
},
currency
)
}

/** Historical expressions without a frozen scale cannot safely expose prices. */
export function logExpressionCurrency(
other: LogOtherData,
creditsPerUSD: number | undefined
): {
export function logExpressionCurrency(other: LogOtherData): {
expressionCurrencyBasis?: 'USD' | 'legacy_pricing_unit'
expressionUsdMultiplier?: number
} {
Expand All @@ -95,28 +99,31 @@ export function logExpressionCurrency(
}
if (
positiveNumber(other.pricing_unit_credits_per_unit) &&
positiveNumber(creditsPerUSD)
positiveNumber(other.pricing_credits_per_usd)
) {
return {
expressionCurrencyBasis: 'legacy_pricing_unit',
expressionUsdMultiplier:
other.pricing_unit_credits_per_unit / creditsPerUSD,
other.pricing_unit_credits_per_unit / other.pricing_credits_per_usd,
}
}
return {}
}

/** Ratios are calibrated Credits per token; they do not depend on legacy QPU. */
/** Ratios need the request's captured Credit-to-USD scale, never today's anchor. */
export function formatLogTokenPrice(
modelRatio: number,
multiplier: number,
other: LogOtherData,
currency: LogCurrencyFormatter
): string {
return currency.formatUSD(
(modelRatio * 1_000_000 * multiplier) /
Number(currency.config.creditsPerUsd),
PRICE_OPTIONS
)
const scale = positiveNumber(other.model_ratio_usd_per_million_multiplier)
? other.model_ratio_usd_per_million_multiplier
: positiveNumber(other.pricing_credits_per_usd)
? 1_000_000 / other.pricing_credits_per_usd
: null
if (scale == null) return '-'
return currency.formatUSD(modelRatio * scale * multiplier, PRICE_OPTIONS)
}

/** Copy the currently displayed bill and preserve the original text as evidence. */
Expand Down Expand Up @@ -155,11 +162,11 @@ export function buildLogCopyText(
)
} else if (other?.model_ratio != null) {
rows.push(
`${t('Input')}: ${formatLogTokenPrice(other.model_ratio, 1, currency)}/M`
`${t('Input')}: ${formatLogTokenPrice(other.model_ratio, 1, other, currency)}/M`
)
if (other.completion_ratio != null) {
rows.push(
`${t('Output')}: ${formatLogTokenPrice(other.model_ratio, other.completion_ratio, currency)}/M`
`${t('Output')}: ${formatLogTokenPrice(other.model_ratio, other.completion_ratio, other, currency)}/M`
)
}
}
Expand All @@ -171,6 +178,7 @@ export function buildLogCopyText(
pricing_schema_version: other?.pricing_schema_version,
pricing_currency_basis: item.price_currency_basis,
pricing_unit_credits_per_unit: item.pricing_unit_credits_per_unit,
pricing_credits_per_usd: other?.pricing_credits_per_usd,
},
currency
)}/1K)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function BillingHistoryDialog({
{t('Credited balance')}
</Label>
<div className='text-sm font-semibold'>
{/* Prefer immutable credited quota; bridge old batch snapshots when absent. */}
{/* Missing historical credit evidence remains unavailable. */}
{formatQuota(getTopupRecordQuota(record))}
</div>
</div>
Expand Down
Loading