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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
perf(cost): a local chat does not buy a context lookup it cannot use
Davidc27a74e16936
diff --git a/src/hooks/__tests__/chat-budget-wiring.test.ts b/src/hooks/__tests__/chat-budget-wiring.test.tsindex 37744a28..b3c67394 100644--- a/src/hooks/__tests__/chat-budget-wiring.test.ts+++ b/src/hooks/__tests__/chat-budget-wiring.test.ts@@ -47,7 +47,7 @@ describe('a group round is budgeted per model', () => {chat.indexOf('export function useChat'),)expect(groupTurn).toMatch(/applyChatSendBudget\(/)- expect(groupTurn).toMatch(/modelWindow: await getModelMaxTokens\(model\)/)+ expect(groupTurn).toMatch(/await getModelMaxTokens\(model\)/)expect(groupTurn).toMatch(/capMessageCount\(\[/)expect(groupTurn).toMatch(/contextDecay: settings\.contextDecay/)})@@ -72,6 +72,11 @@ describe('compare caps the shared base before the fan-out', () => {it('honours the contextDecay notaus', () => {expect(compare).toMatch(/contextDecay: settings\.contextDecay/)})++ it('does not spend a context lookup on a pairing it cannot cap', () => {+ expect(compare).toMatch(/chatBudgetApplies\(providerId, settings\.contextDecay\)/)+ expect(chat).toMatch(/chatBudgetApplies\(providerId, settings\.contextDecay\)/)+ })})describe('the agent and coding loops get the image rule for free', () => {diff --git a/src/hooks/useABCompare.ts b/src/hooks/useABCompare.tsindex 084b440e..cb39f304 100644--- a/src/hooks/useABCompare.ts+++ b/src/hooks/useABCompare.ts@@ -7,7 +7,7 @@ import { useCompareStore } from '../stores/compareStore'import { useSettingsStore } from '../stores/settingsStore'import { getProviderForModel, getProviderIdFromModel } from '../api/providers'import { getModelMaxTokens } from '../lib/context-compaction'-import { applySendBudget, sharedChatSendBudget } from '../lib/chat-send-budget'+import { applySendBudget, chatBudgetApplies, sharedChatSendBudget } from '../lib/chat-send-budget'import { v4 as uuid } from 'uuid'import type { ChatMessage } from '../api/providers/types'import type { Message } from '../types/chat'@@ -53,12 +53,19 @@ export function useABCompare() {// the paid side's budget for both; two local models are untouched.const budget = sharedChatSendBudget(await Promise.all(- [modelA, modelB].map(async (m) => ({- providerId: getProviderIdFromModel(m),- modelWindow: await getModelMaxTokens(m),- sendWindowTokens: settings.codexSendWindowTokens,- contextDecay: settings.contextDecay,- })),+ [modelA, modelB].map(async (m) => {+ const providerId = getProviderIdFromModel(m)+ return {+ providerId,+ // Two local models must not buy two /api/show round trips for a+ // budget that will come back null either way.+ modelWindow: chatBudgetApplies(providerId, settings.contextDecay)+ ? await getModelMaxTokens(m)+ : 0,+ sendWindowTokens: settings.codexSendWindowTokens,+ contextDecay: settings.contextDecay,+ }+ }),),)const sendMessages = applySendBudget(chatMessages, budget).messagesdiff --git a/src/hooks/useChat.ts b/src/hooks/useChat.tsindex 8c2226da..18e7b1e1 100644--- a/src/hooks/useChat.ts+++ b/src/hooks/useChat.ts@@ -9,7 +9,7 @@ import { useVoiceStore } from "../stores/voiceStore"import { autoSpeak } from "../lib/ttsBridge"import { retrieveContext } from "../api/rag"import { getModelMaxTokens, capMessageCount } from "../lib/context-compaction"-import { applyChatSendBudget } from "../lib/chat-send-budget"+import { applyChatSendBudget, chatBudgetApplies } from "../lib/chat-send-budget"import { getModelContextCached } from "../api/ollama"import { requestGenerationCancel } from "../api/vram-handoff"import { effectiveContextWindow } from "../lib/context-window"@@ -91,7 +91,12 @@ async function runGroupTurn(convId: string, model: string, allModels: string[],]),{providerId,- modelWindow: await getModelMaxTokens(model),+ // Only asked where the answer can change something: resolving the window+ // is an /api/show round trip, and a local member of the line-up is not+ // capped at all.+ modelWindow: chatBudgetApplies(providerId, settings.contextDecay)+ ? await getModelMaxTokens(model)+ : 0,sendWindowTokens: settings.codexSendWindowTokens,contextDecay: settings.contextDecay,},diff --git a/src/lib/__tests__/chat-send-budget.test.ts b/src/lib/__tests__/chat-send-budget.test.tsindex 6ba6ceb6..c12d71a6 100644--- a/src/lib/__tests__/chat-send-budget.test.ts+++ b/src/lib/__tests__/chat-send-budget.test.ts@@ -14,6 +14,7 @@ import { describe, it, expect } from 'vitest'import {applyChatSendBudget,applySendBudget,+ chatBudgetApplies,chatSendBudget,sharedChatSendBudget,} from '../chat-send-budget'@@ -116,6 +117,16 @@ describe('A4: a local backend is byte-identical to 2.6.5', () => {it('an unknown window resolves to no cap rather than to a tiny one', () => {expect(chatSendBudget({ providerId: 'lu-cloud', modelWindow: 0, contextDecay: true })).toBeNull()})++ it('says so before the window is looked up, so no round trip is spent on it', () => {+ // Resolving a window is an /api/show call. Asking it for a payload that+ // cannot be capped would put a new request on the local path.+ expect(chatBudgetApplies('ollama', true)).toBe(false)+ expect(chatBudgetApplies('lm-studio', true)).toBe(false)+ expect(chatBudgetApplies('lu-cloud', false)).toBe(false)+ expect(chatBudgetApplies('lu-cloud', true)).toBe(true)+ expect(chatBudgetApplies('openai', undefined)).toBe(true)+ })})describe('A4: a group round is budgeted per model per round', () => {diff --git a/src/lib/chat-send-budget.ts b/src/lib/chat-send-budget.tsindex 7e8c89c6..56716127 100644--- a/src/lib/chat-send-budget.ts+++ b/src/lib/chat-send-budget.ts@@ -37,14 +37,25 @@ export interface ChatSendBudgetInput {contextDecay?: boolean}+/**+ * Whether a payload to this provider is capped at all, answerable before the+ * model window has been looked up.+ *+ * Resolving that window is an /api/show round trip on Ollama, and a local+ * backend is never capped, so it must not pay for a question whose answer+ * cannot change anything.+ */+export function chatBudgetApplies(providerId: string, contextDecay?: boolean): boolean {+ return contextDecay !== false && isPaidProvider(providerId)+}+/*** The budget one send may carry, or null when this surface is not capped at* all. Null is not "unlimited by accident": it is the explicit 2.6.5 path, and* callers hand the untouched array straight through on it.*/export function chatSendBudget(input: ChatSendBudgetInput): number | null {- if (input.contextDecay === false) return null- if (!isPaidProvider(input.providerId)) return null+ if (!chatBudgetApplies(input.providerId, input.contextDecay)) return nullconst window = effectiveSendWindow({providerId: input.providerId,modelWindow: input.modelWindow,