Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(model-bank): fix $0 chat cost for range-priced gpt-5.5/5.4 models (#16991)

Lee Junhocabf21b5402e
diff --git a/packages/model-bank/src/aiModels/aihubmix.ts b/packages/model-bank/src/aiModels/aihubmix.ts
index a1fe32b117f..74089030bc3 100644
--- a/packages/model-bank/src/aiModels/aihubmix.ts
+++ b/packages/model-bank/src/aiModels/aihubmix.ts
@@ -22,39 +22,30 @@ const aihubmixChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 5,
- '[0.272, infinity]': 10,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 5, upTo: 272_000 },
+ { rate: 10, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 0.5,
- '[0.272, infinity]': 1,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput_cacheRead',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 0.5, upTo: 272_000 },
+ { rate: 1, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 30,
- '[0.272, infinity]': 45,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 30, upTo: 272_000 },
+ { rate: 45, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
@@ -86,39 +77,30 @@ const aihubmixChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 2.5,
- '[0.272, infinity]': 5,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 2.5, upTo: 272_000 },
+ { rate: 5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 0.25,
- '[0.272, infinity]': 0.5,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput_cacheRead',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 0.25, upTo: 272_000 },
+ { rate: 0.5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 15,
- '[0.272, infinity]': 22.5,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 15, upTo: 272_000 },
+ { rate: 22.5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
@@ -212,27 +194,21 @@ const aihubmixChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 30,
- '[0.272, infinity]': 60,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 30, upTo: 272_000 },
+ { rate: 60, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 180,
- '[0.272, infinity]': 270,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 180, upTo: 272_000 },
+ { rate: 270, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
diff --git a/packages/model-bank/src/aiModels/openai.ts b/packages/model-bank/src/aiModels/openai.ts
index 193e0d9dfb5..3caa5084aef 100644
--- a/packages/model-bank/src/aiModels/openai.ts
+++ b/packages/model-bank/src/aiModels/openai.ts
@@ -252,39 +252,30 @@ export const openaiChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 5,
- '[0.272, infinity]': 10,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 5, upTo: 272_000 },
+ { rate: 10, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 0.5,
- '[0.272, infinity]': 1,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput_cacheRead',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 0.5, upTo: 272_000 },
+ { rate: 1, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 30,
- '[0.272, infinity]': 45,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 30, upTo: 272_000 },
+ { rate: 45, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
@@ -357,39 +348,30 @@ export const openaiChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 2.5,
- '[0.272, infinity]': 5,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 2.5, upTo: 272_000 },
+ { rate: 5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 0.25,
- '[0.272, infinity]': 0.5,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput_cacheRead',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 0.25, upTo: 272_000 },
+ { rate: 0.5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 15,
- '[0.272, infinity]': 22.5,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 15, upTo: 272_000 },
+ { rate: 22.5, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
@@ -420,27 +402,21 @@ export const openaiChatModels: AIChatModelCard[] = [
pricing: {
units: [
{
- lookup: {
- prices: {
- '[0, 0.272]': 30,
- '[0.272, infinity]': 60,
- },
- pricingParams: ['textInput'],
- },
name: 'textInput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 30, upTo: 272_000 },
+ { rate: 60, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
{
- lookup: {
- prices: {
- '[0, 0.272]': 180,
- '[0.272, infinity]': 270,
- },
- pricingParams: ['textInput'],
- },
name: 'textOutput',
- strategy: 'lookup',
+ strategy: 'tiered',
+ tiers: [
+ { rate: 180, upTo: 272_000 },
+ { rate: 270, upTo: 'infinity' },
+ ],
unit: 'millionTokens',
},
],
diff --git a/packages/model-runtime/src/core/usageConverters/utils/computeChatCost.test.ts b/packages/model-runtime/src/core/usageConverters/utils/computeChatCost.test.ts
index 099ab428db7..490fd2230fa 100644
--- a/packages/model-runtime/src/core/usageConverters/utils/computeChatCost.test.ts
+++ b/packages/model-runtime/src/core/usageConverters/utils/computeChatCost.test.ts
@@ -1,5 +1,6 @@
import type { ModelTokensUsage } from '@lobechat/types';
import type { Pricing } from 'model-bank';
+import aihubmixChatModels from 'model-bank/aihubmix';
import anthropicChatModels from 'model-bank/anthropic';
import azureChatModels from 'model-bank/azure';
import deepseekChatModels from 'model-bank/deepseek';
@@ -805,6 +806,166 @@ describe('computeChatPricing', () => {
});
});
+ describe.each([
+ ['OpenAI', openaiChatModels],
+ ['AiHubMix', aihubmixChatModels],
+ ])('%s range-priced GPT cards', (_provider, models) => {
+ const findPricing = (id: string) =>
+ (models as { id: string; pricing?: Pricing }[]).find((m) => m.id === id)?.pricing;
+
+ it.each([
+ { id: 'gpt-5.5', inputRate: 5, outputRate: 30 },
+ { id: 'gpt-5.4', inputRate: 2.5, outputRate: 15 },
+ { id: 'gpt-5.4-pro', inputRate: 30, outputRate: 180 },
+ ])(
+ 'computes nonzero cost without lookup params at the 272K boundary for $id',
+ ({ id, inputRate, outputRate }) => {
+ const pricing = findPricing(id);
+ expect(pricing).toBeDefined();
+
+ const usage: ModelTokensUsage = {
+ inputCacheMissTokens: 272_000,
+ inputTextTokens: 272_000,
+ outputTextTokens: 1_000,
+ totalInputTokens: 272_000,
+ totalOutputTokens: 1_000,
+ totalTokens: 273_000,
+ };
+
+ const result = computeChatCost(pricing, usage);
+ expect(result).toBeDefined();
+ expect(result?.issues).toHaveLength(0);
+
+ // 272,000 total input tokens is still within the lower tier (inclusive bound)
+ const input = result?.breakdown.find((item) => item.unit.name === 'textInput');
+ expect(input?.segments).toEqual([
+ { credits: 272_000 * inputRate, quantity: 272_000, rate: inputRate },
+ ]);
+
+ const output = result?.breakdown.find((item) => item.unit.name === 'textOutput');
+ expect(output?.segments).toEqual([
+ { credits: 1_000 * outputRate, quantity: 1_000, rate: outputRate },
+ ]);
+
+ expect(result!.totalCredits).toBeGreaterThan(0);
+ },
+ );
+
+ it.each([
+ { id: 'gpt-5.5', inputRate: 10, outputRate: 45 },
+ { id: 'gpt-5.4', inputRate: 5, outputRate: 22.5 },
+ { id: 'gpt-5.4-pro', inputRate: 60, outputRate: 270 },
+ ])(
+ 'bills every unit at the higher tier above 272K total input for $id',
+ ({ id, inputRate, outputRate }) => {
+ const pricing = findPricing(id);
+ expect(pricing).toBeDefined();
+
+ const usage: ModelTokensUsage = {
+ inputCacheMissTokens: 272_001,
+ inputTextTokens: 272_001,
+ outputTextTokens: 1_000,
+ totalInputTokens: 272_001,
+ totalOutputTokens: 1_000,
+ totalTokens: 273_001,
+ };
+
+ const result = computeChatCost(pricing, usage);
+ expect(result?.issues).toHaveLength(0);
+
+ const input = result?.breakdown.find((item) => item.unit.name === 'textInput');
+ expect(input?.segments).toEqual([
+ { credits: 272_001 * inputRate, quantity: 272_001, rate: inputRate },
+ ]);
+
+ const output = result?.breakdown.find((item) => item.unit.name === 'textOutput');
+ expect(output?.segments).toEqual([
+ { credits: 1_000 * outputRate, quantity: 1_000, rate: outputRate },
+ ]);
+ },
+ );
+
+ it.each([
+ { cacheRate: 0.5, id: 'gpt-5.5', missRate: 5, outputRate: 30 },
+ { cacheRate: 0.25, id: 'gpt-5.4', missRate: 2.5, outputRate: 15 },
+ ])(
+ 'keeps cache-read units on the lower tier at the 272K boundary for $id',
+ ({ id, missRate, cacheRate, outputRate }) => {
+ const pricing = findPricing(id);
+ expect(pricing).toBeDefined();
+
+ // 271K cached + 1K missed = exactly 272K total input, still the lower tier
+ const usage: ModelTokensUsage = {
+ inputCachedTokens: 271_000,
+ inputCacheMissTokens: 1_000,
+ inputTextTokens: 272_000,
+ outputTextTokens: 100,
+ totalInputTokens: 272_000,
+ totalOutputTokens: 100,
+ totalTokens: 272_100,
+ };
+
+ const result = computeChatCost(pricing, usage);
+ expect(result?.issues).toHaveLength(0);
+
+ const input = result?.breakdown.find((item) => item.unit.name === 'textInput');
+ expect(input?.segments).toEqual([
+ { credits: 1_000 * missRate, quantity: 1_000, rate: missRate },
+ ]);
+
+ const cached = result?.breakdown.find((item) => item.unit.name === 'textInput_cacheRead');
+ expect(cached?.segments).toEqual([
+ { credits: 271_000 * cacheRate, quantity: 271_000, rate: cacheRate },
+ ]);
+
+ const output = result?.breakdown.find((item) => item.unit.name === 'textOutput');
+ expect(output?.segments).toEqual([
+ { credits: 100 * outputRate, quantity: 100, rate: outputRate },
+ ]);
+ },
+ );
+
+ it.each([
+ { cacheRate: 1, id: 'gpt-5.5', missRate: 10, outputRate: 45 },
+ { cacheRate: 0.5, id: 'gpt-5.4', missRate: 5, outputRate: 22.5 },
+ ])(
+ 'bills cache-read units at the higher tier above 272K total input for $id',
+ ({ id, missRate, cacheRate, outputRate }) => {
+ const pricing = findPricing(id);
+ expect(pricing).toBeDefined();
+
+ // 272K cached + 1K missed = 273K total input, so every unit moves up a tier
+ const usage: ModelTokensUsage = {
+ inputCachedTokens: 272_000,
+ inputCacheMissTokens: 1_000,
+ inputTextTokens: 273_000,
+ outputTextTokens: 100,
+ totalInputTokens: 273_000,
+ totalOutputTokens: 100,
+ totalTokens: 273_100,
+ };
+
+ const result = computeChatCost(pricing, usage);
+ expect(result?.issues).toHaveLength(0);
+
+ const input = result?.breakdown.find((item) => item.unit.name === 'textInput');
+ expect(input?.segments).toEqual([
+ { credits: 1_000 * missRate, quantity: 1_000, rate: missRate },
+ ]);
+
+ const cached = result?.breakdown.find((item) => item.unit.name === 'textInput_cacheRead');
+ expect(cached?.segments).toEqual([
+ { credits: 272_000 * cacheRate, quantity: 272_000, rate: cacheRate },
+ ]);
+
+ const output = result?.breakdown.find((item) => item.unit.name === 'textOutput');
+ expect(output?.segments).toEqual([
+ { credits: 100 * outputRate, quantity: 100, rate: outputRate },
+ ]);
+ },
+ );
+ });
+
describe('MiniMax', () => {
it('uses total input tokens to select tiered rates for MiniMax-M3', () => {
const pricing = minimaxChatModels.find(