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(gemini): handle zero cachedContentTokenCount in usage conversion (#14567)

sxjeru307cd8e523e4
diff --git a/packages/model-bank/src/aiModels/google.ts b/packages/model-bank/src/aiModels/google.ts
index c87e6b565d0..f090ed72e5e 100644
--- a/packages/model-bank/src/aiModels/google.ts
+++ b/packages/model-bank/src/aiModels/google.ts
@@ -282,6 +282,40 @@ const googleChatModels: AIChatModelCard[] = [
},
type: 'chat',
},
+ {
+ abilities: {
+ functionCall: true,
+ reasoning: true,
+ search: true,
+ structuredOutput: true,
+ video: true,
+ vision: true,
+ },
+ contextWindowTokens: 1_048_576 + 65_536,
+ description:
+ "Gemini 3.1 Flash-Lite is Google's most cost-efficient multimodal model, optimized for high-volume agentic tasks, translation, and data processing.",
+ displayName: 'Gemini 3.1 Flash-Lite',
+ enabled: true,
+ id: 'gemini-3.1-flash-lite',
+ maxOutput: 65_536,
+ pricing: {
+ units: [
+ { name: 'textInput_cacheRead', rate: 0.025, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textInput', rate: 0.25, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'imageInput', rate: 0.25, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'videoInput', rate: 0.25, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'audioInput', rate: 0.5, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textOutput', rate: 1.5, strategy: 'fixed', unit: 'millionTokens' },
+ ],
+ },
+ releasedAt: '2026-05-07',
+ settings: {
+ extendParams: ['thinkingLevel5', 'urlContext'],
+ searchImpl: 'params',
+ searchProvider: 'google',
+ },
+ type: 'chat',
+ },
{
abilities: {
functionCall: true,
@@ -295,7 +329,6 @@ const googleChatModels: AIChatModelCard[] = [
description:
"Gemini 3.1 Flash-Lite Preview is Google's most cost-efficient multimodal model, optimized for high-volume agentic tasks, translation, and data processing.",
displayName: 'Gemini 3.1 Flash-Lite Preview',
- enabled: true,
id: 'gemini-3.1-flash-lite-preview',
maxOutput: 65_536,
pricing: {
diff --git a/packages/model-bank/src/aiModels/wenxin.ts b/packages/model-bank/src/aiModels/wenxin.ts
index bd1de95edd1..f4a03c9112d 100644
--- a/packages/model-bank/src/aiModels/wenxin.ts
+++ b/packages/model-bank/src/aiModels/wenxin.ts
@@ -1123,30 +1123,6 @@ const wenxinChatModels: AIChatModelCard[] = [
},
type: 'chat',
},
- {
- abilities: {
- functionCall: true,
- reasoning: true,
- search: true,
- },
- contextWindowTokens: 144_000,
- description:
- 'DeepSeek R1 (current 250120) is a deep reasoning model with open chain-of-thought output.',
- displayName: 'DeepSeek R1 250120',
- id: 'deepseek-r1',
- maxOutput: 32_768,
- pricing: {
- currency: 'CNY',
- units: [
- { name: 'textInput', rate: 4, strategy: 'fixed', unit: 'millionTokens' },
- { name: 'textOutput', rate: 16, strategy: 'fixed', unit: 'millionTokens' },
- ],
- },
- settings: {
- searchImpl: 'params',
- },
- type: 'chat',
- },
{
abilities: {
functionCall: true,
diff --git a/packages/model-runtime/src/core/streams/google/google-ai.test.ts b/packages/model-runtime/src/core/streams/google/google-ai.test.ts
index ace3c4961d9..8111e9a5214 100644
--- a/packages/model-runtime/src/core/streams/google/google-ai.test.ts
+++ b/packages/model-runtime/src/core/streams/google/google-ai.test.ts
@@ -155,7 +155,7 @@ describe('GoogleGenerativeAIStream', () => {
`data: "STOP"\n\n`,
'id: chat_E5M9dFKw\n',
'event: usage\n',
- `data: {"inputCachedTokens":0,"inputImageTokens":0,"inputTextTokens":0,"outputImageTokens":0,"outputTextTokens":0,"totalInputTokens":0,"totalOutputTokens":0,"totalTokens":0}\n\n`,
+ `data: {"inputCacheMissTokens":0,"inputCachedTokens":0,"inputImageTokens":0,"inputTextTokens":0,"outputImageTokens":0,"outputTextTokens":0,"totalInputTokens":0,"totalOutputTokens":0,"totalTokens":0}\n\n`,
]);
});
diff --git a/packages/model-runtime/src/core/usageConverters/google-ai.test.ts b/packages/model-runtime/src/core/usageConverters/google-ai.test.ts
index 22399ca27be..edcbe36a505 100644
--- a/packages/model-runtime/src/core/usageConverters/google-ai.test.ts
+++ b/packages/model-runtime/src/core/usageConverters/google-ai.test.ts
@@ -83,6 +83,35 @@ describe('convertGoogleAIUsage', () => {
});
});
+ it('should calculate cache miss tokens when cached content token count is zero', () => {
+ const usage: GenerateContentResponseUsageMetadata = {
+ cachedContentTokenCount: 0,
+ candidatesTokenCount: 217,
+ candidatesTokensDetails: undefined,
+ promptTokenCount: 5491,
+ promptTokensDetails: undefined,
+ thoughtsTokenCount: 0,
+ toolUsePromptTokenCount: 0,
+ toolUsePromptTokensDetails: undefined,
+ totalTokenCount: 6973,
+ };
+
+ const pricing: Pricing = {
+ units: [
+ { name: 'textInput', rate: 1, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textInput_cacheRead', rate: 0.25, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textOutput', rate: 2, strategy: 'fixed', unit: 'millionTokens' },
+ ],
+ };
+
+ const result = convertGoogleAIUsage(usage, pricing);
+
+ expect(result.inputCacheMissTokens).toBe(5491);
+ expect(result.inputCachedTokens).toBe(0);
+ expect(result.totalInputTokens).toBe(5491);
+ expect(result.cost).toBeCloseTo((5491 + 217 * 2) / 1_000_000, 10);
+ });
+
it('should attach cost when pricing provided', () => {
const usage: GenerateContentResponseUsageMetadata = {
candidatesTokenCount: 100_000,
diff --git a/packages/model-runtime/src/core/usageConverters/google-ai.ts b/packages/model-runtime/src/core/usageConverters/google-ai.ts
index 26564715489..938b34c480a 100644
--- a/packages/model-runtime/src/core/usageConverters/google-ai.ts
+++ b/packages/model-runtime/src/core/usageConverters/google-ai.ts
@@ -14,7 +14,7 @@ export const convertGoogleAIUsage = (
pricing?: Pricing,
): ModelUsage => {
const inputCacheMissTokens =
- usage.promptTokenCount && usage.cachedContentTokenCount
+ typeof usage.promptTokenCount === 'number' && typeof usage.cachedContentTokenCount === 'number'
? usage.promptTokenCount - usage.cachedContentTokenCount
: undefined;