Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(chat): the prompt-cache share is read and shown under an answer (CA)

purple1daf9fd415db
diff --git a/src/api/providers/lu-cloud-provider.ts b/src/api/providers/lu-cloud-provider.ts
index b798463f..8a6e4014 100644
--- a/src/api/providers/lu-cloud-provider.ts
+++ b/src/api/providers/lu-cloud-provider.ts
@@ -85,7 +85,7 @@ export class LuCloudProvider implements ProviderClient {
messages: ChatMessage[],
tools: ToolDefinition[],
options?: ChatOptions,
- ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; thinking?: string; billing?: ChatBilling }> {
+ ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; cachedPromptCount?: number; thinking?: string; billing?: ChatBilling }> {
try {
const inner = await this.delegate()
return await inner.chatWithTools(model, messages, tools, options)
diff --git a/src/api/providers/openai-provider.ts b/src/api/providers/openai-provider.ts
index 2c86a2c3..b6a755ea 100644
--- a/src/api/providers/openai-provider.ts
+++ b/src/api/providers/openai-provider.ts
@@ -486,6 +486,20 @@ async function builtinEngineRunning(): Promise<boolean | null> {
}
}
+
+/**
+ * How many prompt tokens the upstream served from its cache, in either of the
+ * two spellings OpenAI-compatible servers use. 0 when the server says nothing.
+ */
+export function cachedPromptTokensOf(usage: unknown): number {
+ if (!usage || typeof usage !== 'object') return 0
+ const u = usage as { prompt_tokens_details?: { cached_tokens?: unknown }; prompt_cache_hit_tokens?: unknown }
+ const a = u.prompt_tokens_details?.cached_tokens
+ const b = u.prompt_cache_hit_tokens
+ const n = typeof a === 'number' ? a : typeof b === 'number' ? b : 0
+ return Number.isFinite(n) && n > 0 ? Math.round(n) : 0
+}
+
export class OpenAIProvider implements ProviderClient {
readonly id = 'openai' as const
/** GitHub #173: set when this client serves an additional entry. */
@@ -1204,6 +1218,7 @@ export class OpenAIProvider implements ProviderClient {
const toolCallAccum: Map<number, { id: string; name: string; args: string }> = new Map()
let promptTokens = 0
let completionTokens = 0
+ let cachedTokens = 0
let finishReason: string | undefined
const billing = chatBilling(res)
const doneChunk = (fallbackReason: string): ChatStreamChunk => {
@@ -1215,6 +1230,7 @@ export class OpenAIProvider implements ProviderClient {
finishReason: finishReason ?? fallbackReason,
promptEvalCount: promptTokens || undefined,
evalCount: completionTokens || undefined,
+ cachedPromptCount: cachedTokens || undefined,
billing,
}
}
@@ -1268,6 +1284,7 @@ export class OpenAIProvider implements ProviderClient {
if (u) {
promptTokens = u.prompt_tokens || promptTokens
completionTokens = u.completion_tokens || completionTokens
+ cachedTokens = cachedPromptTokensOf(u) || cachedTokens
}
const choice = chunk.choices?.[0]
@@ -1366,7 +1383,7 @@ export class OpenAIProvider implements ProviderClient {
messages: ChatMessage[],
tools: ToolDefinition[],
options?: ChatOptions,
- ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; thinking?: string; billing?: ChatBilling }> {
+ ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; cachedPromptCount?: number; thinking?: string; billing?: ChatBilling }> {
messages = await resolveMessageAttachments(messages)
const body: OpenAIChatRequest = {
model,
@@ -1443,6 +1460,7 @@ export class OpenAIProvider implements ProviderClient {
toolCalls,
promptEvalCount: usage?.prompt_tokens,
evalCount: usage?.completion_tokens,
+ cachedPromptCount: usage ? cachedPromptTokensOf(usage) || undefined : undefined,
thinking: message?.reasoning_content || message?.reasoning || undefined,
billing: chatBilling(res),
}
diff --git a/src/api/providers/types.ts b/src/api/providers/types.ts
index 5ff3bfeb..33120ffc 100644
--- a/src/api/providers/types.ts
+++ b/src/api/providers/types.ts
@@ -363,6 +363,10 @@ export interface ChatStreamChunk {
// makes client-side TTFT measurement meaningless for fast models.
evalCount?: number // tokens generated by the model
promptEvalCount?: number // tokens in the prompt = real consumed context (system+tools+history+input)
+ /** Of promptEvalCount, how many the upstream served from its prompt cache
+ * (usage.prompt_tokens_details.cached_tokens, or DeepSeek's
+ * prompt_cache_hit_tokens). CA, 14.11.2026. */
+ cachedPromptCount?: number
evalDurationMs?: number // generation time in ms (excludes prompt eval + load)
/** How LU Cloud billed this request (`x-lu-chat-billing`): 'flash' drew no
* credits. Absent on every other backend and when the header was missing. */
@@ -411,7 +415,7 @@ export interface ProviderClient {
messages: ChatMessage[],
tools: ToolDefinition[],
options?: ChatOptions
- ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; thinking?: string; billing?: ChatBilling }>
+ ): Promise<{ content: string; toolCalls: ToolCall[]; promptEvalCount?: number; evalCount?: number; cachedPromptCount?: number; thinking?: string; billing?: ChatBilling }>
/** List available models from this provider. */
listModels(): Promise<ProviderModel[]>
diff --git a/src/lib/__tests__/the-cache-share-is-shown.test.ts b/src/lib/__tests__/the-cache-share-is-shown.test.ts
new file mode 100644
index 00000000..9a471bd7
--- /dev/null
+++ b/src/lib/__tests__/the-cache-share-is-shown.test.ts
@@ -0,0 +1,40 @@
+/**
+ * CA (14.11.2026): customers paid full price on every step of long agent runs
+ * with no prompt-cache hit, and nothing in the app showed it. The cache share
+ * the backend reports is now read and shown under the answer.
+ */
+import { describe, it, expect } from 'vitest'
+import { addCallUsage, requestUsageLine } from '../request-usage'
+import { cachedPromptTokensOf } from '../../api/providers/openai-provider'
+
+describe('cachedPromptTokensOf', () => {
+ it('reads both spellings, and nothing from nothing', () => {
+ expect(cachedPromptTokensOf({ prompt_tokens: 100, prompt_tokens_details: { cached_tokens: 80 } })).toBe(80)
+ expect(cachedPromptTokensOf({ prompt_tokens: 100, prompt_cache_hit_tokens: 64 })).toBe(64)
+ expect(cachedPromptTokensOf({ prompt_tokens: 100 })).toBe(0)
+ expect(cachedPromptTokensOf(null)).toBe(0)
+ })
+})
+
+describe('the line under an answer', () => {
+ it('sums the cached share over the calls and shows it', () => {
+ let u = addCallUsage(undefined, { promptEvalCount: 20_000, evalCount: 300, cachedPromptCount: 0 })
+ u = addCallUsage(u, { promptEvalCount: 22_000, evalCount: 300, cachedPromptCount: 19_800 })
+ u = addCallUsage(u, { promptEvalCount: 24_000, evalCount: 300, cachedPromptCount: 21_900, billing: 'credits' })
+ expect(u!.cachedTokens).toBe(41_700)
+ const line = requestUsageLine(u)!
+ expect(line.text).toBe('66,000 in · 900 out · 63% cached · Credits')
+ expect(line.title).toMatch(/41,700 of the input tokens came from the model's prompt cache/)
+ })
+
+ it('says nothing about a cache nobody reported', () => {
+ const u = addCallUsage(undefined, { promptEvalCount: 1000, evalCount: 10 })
+ expect(u!.cachedTokens).toBeUndefined()
+ expect(requestUsageLine(u)!.text).not.toMatch(/cached/)
+ })
+
+ it('never claims more cached than sent', () => {
+ const u = addCallUsage(undefined, { promptEvalCount: 100, evalCount: 1, cachedPromptCount: 500 })
+ expect(u!.cachedTokens).toBe(100)
+ })
+})
diff --git a/src/lib/provider-stream.ts b/src/lib/provider-stream.ts
index 5400d831..83be79be 100644
--- a/src/lib/provider-stream.ts
+++ b/src/lib/provider-stream.ts
@@ -30,6 +30,8 @@ export interface StreamedProviderTurn {
thinking: string
promptEvalCount?: number
evalCount?: number
+ /** Of promptEvalCount, served from the upstream's prompt cache. */
+ cachedPromptCount?: number
finishReason?: string
billing?: ChatBilling
}
@@ -71,6 +73,7 @@ export async function streamProviderTurn(
if (chunk.toolCalls?.length) turn.toolCalls = chunk.toolCalls
turn.promptEvalCount = chunk.promptEvalCount
turn.evalCount = chunk.evalCount
+ turn.cachedPromptCount = chunk.cachedPromptCount
turn.finishReason = chunk.finishReason
turn.billing = chunk.billing
}
diff --git a/src/lib/request-usage.ts b/src/lib/request-usage.ts
index 4c5ebf63..4b632a13 100644
--- a/src/lib/request-usage.ts
+++ b/src/lib/request-usage.ts
@@ -32,6 +32,7 @@ import { FLASH_MARK_LABEL } from './flash-entitlement'
export interface CallCounts {
promptEvalCount?: number
evalCount?: number
+ cachedPromptCount?: number
billing?: ChatBilling
}
@@ -50,11 +51,14 @@ export function addCallUsage(prev: RequestUsage | undefined, call: CallCounts):
const base: RequestUsage = prev ?? { inputTokens: 0, outputTokens: 0, calls: 0 }
const billing: ChatBilling | undefined =
base.billing === 'credits' || call.billing === 'credits' ? 'credits' : (call.billing ?? base.billing)
+ const cached = Math.min(count(call.cachedPromptCount), input)
+ const cachedSum = (base.cachedTokens ?? 0) + cached
return {
inputTokens: base.inputTokens + input,
outputTokens: base.outputTokens + output,
calls: base.calls + (reported ? 1 : 0),
...(billing ? { billing } : {}),
+ ...(cachedSum > 0 ? { cachedTokens: cachedSum } : {}),
}
}
@@ -88,6 +92,14 @@ export function requestUsageLine(u: RequestUsage | undefined): { text: string; t
`Tokens for this answer, as reported by the model: ${fmt(u.inputTokens)} in (prompt with system prompt, tools and history), ${fmt(u.outputTokens)} out.`,
)
if (u.calls > 1) notes.push(`Summed over ${u.calls} model calls.`)
+ // CA (14.11.2026): what the backend served from its prompt cache. On a
+ // long agent run that is most of the input, and it is billed at a
+ // fraction; a run that shows none is paying full price on every step.
+ if (u.cachedTokens && u.inputTokens > 0) {
+ const pct = Math.round((u.cachedTokens / u.inputTokens) * 100)
+ parts.push(`${pct}% cached`)
+ notes.push(`${fmt(u.cachedTokens)} of the input tokens came from the model's prompt cache.`)
+ }
}
if (u.billing === 'flash') {
parts.push(FLASH_MARK_LABEL)
diff --git a/src/types/chat.ts b/src/types/chat.ts
index 83925a99..d4b86b1f 100644
--- a/src/types/chat.ts
+++ b/src/types/chat.ts
@@ -356,4 +356,7 @@ export interface RequestUsage {
outputTokens: number
calls: number
billing?: 'flash' | 'credits'
+ /** Of inputTokens, how many the backend served from its prompt cache (CA).
+ * Absent when no call reported it. */
+ cachedTokens?: number
}