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
34 changes: 34 additions & 0 deletions apps/web/src/features/pricing/components/model-details.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,40 @@ describe('ModelDetails group pricing', () => {
await unmount(rendered)
})

test('recalculates the request estimate when cached input tokens change', async () => {
const rendered = await renderModelDetails('cached-model', {
cache_read_price: 0.2,
})
const section = [...rendered.container.querySelectorAll('h3')]
.find((element) => element.textContent === 'Request cost estimate')
?.closest('section')
assert.ok(section)
const cached = section.querySelector<HTMLInputElement>(
'input[id$="-cached"]'
)
assert.ok(cached)
const setValue = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value'
)?.set
assert.ok(setValue)
assert.equal(section.querySelector('[aria-live]')?.textContent, '0.024 USD')
for (const [value, expected] of [
['4000', '0.0168 USD'],
['10000', '0.006 USD'],
['10001', 'Estimate unavailable'],
['', 'Estimate unavailable'],
['0', '0.024 USD'],
]) {
await act(async () => {
setValue.call(cached, value)
cached.dispatchEvent(new Event('input', { bubbles: true }))
})
assert.equal(section.querySelector('[aria-live]')?.textContent, expected)
}
await unmount(rendered)
})

test('does not offer chat token presets for native sessions or their aliases', async () => {
for (const [modelName, endpoint] of [
['gpt-live-1', 'live'],
Expand Down
44 changes: 44 additions & 0 deletions apps/web/src/features/pricing/lib/native-billing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,55 @@ describe('native cache and duration billing', () => {
assert.equal(evalExprLocally(expression, 1000, 0, emptyExtras).error, null)
})

test('uses the same effective cache count for generic prices and tier context', () => {
const expression =
'len < 1200 ? tier("short", p * 2 + cr * 0.1) : tier("long", p * 4 + cr * 0.2)'
for (const [details, cost, matchedTier] of [
[{ cacheTextReadTokens: 100 }, 2010, 'short'],
[{ cacheImageReadTokens: 200 }, 4040, 'long'],
[{ cacheAudioReadTokens: 200 }, 4040, 'long'],
[
{
cacheTextReadTokens: 50,
cacheImageReadTokens: 100,
cacheAudioReadTokens: 50,
},
4040,
'long',
],
[{ cacheReadTokens: 400, cacheTextReadTokens: 100 }, 4080, 'long'],
[{ cacheReadTokens: 0, cacheTextReadTokens: 0 }, 2000, 'short'],
] as const) {
assert.deepEqual(
evalExprLocally(expression, 1000, 0, { ...emptyExtras, ...details }),
{ cost, matchedTier, error: null }
)
}
})

test('keeps text-only estimates accurate and refuses missing duration', () => {
assert.equal(
evaluateTextRequestExpression('p * 1 + cr_text * 0.1', 1000, 0, 100),
910
)
assert.equal(
evaluateTextRequestExpression(
'p * 1 + cr_img * 10 + cr_audio * 100',
1000,
0,
100
),
1000
)
assert.equal(
evaluateTextRequestExpression(
'p * 1 + cr_text * 0.1 + cr_img * 10 + cr_audio * 100',
1000,
0,
100
),
910
)
assert.throws(
() => evaluateTextRequestExpression('audio_s * 100', 1000, 0, 0),
/Audio duration is required/
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/features/pricing/lib/tier-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ export function evalExprLocally(
for (const field of ESTIMATOR_VARS) {
variables[field.var] = extraTokenValues[field.stateKey] || 0
}
// A generic cache rate also applies when only classified counts are supplied.
variables.cr = cacheReadTokens
const evaluated = evaluateBillingExpression(exprStr, variables)
return {
cost: evaluated.value || 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,27 @@ test('estimator maps separate cache counts and decimal audio seconds independent
await ctx.cleanup()
}
})

test('estimator prices classified cache counts with a generic cache rate', async () => {
const ctx = await setup('tier("base", p * 2 + c * 3 + cr * 0.1)')
try {
for (const [field, value, cost] of [
['Text Cache Read', '1000', 0.0001],
['Image Cache Read', '2000', 0.0003],
['Audio Cache Read', '3000', 0.0006],
['Cache Read', '4000', 0.0004],
['Cache Read', '0', 0.0006],
['Text Cache Read', '', 0.0005],
] as const) {
await ctx.change(field, value)
assert.ok(
ctx.container.textContent?.includes(
`Estimated cost (USD): ${cost.toLocaleString(undefined, { maximumFractionDigits: 12 })}`
)
)
}
assert.deepEqual(ctx.changes, [])
} finally {
await ctx.cleanup()
}
})