Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): basic context window progress bar
Elian Doran16806275e037
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 0b686ceca3..23fcc7788b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1629,7 +1629,8 @@"tokens_used": "{{prompt}} prompt + {{completion}} completion = {{total}} tokens","tokens_used_with_cost": "{{prompt}} prompt + {{completion}} completion = {{total}} tokens (~${{cost}})","tokens_used_with_model": "{{model}}: {{prompt}} prompt + {{completion}} completion = {{total}} tokens",- "tokens_used_with_model_and_cost": "{{model}}: {{prompt}} prompt + {{completion}} completion = {{total}} tokens (~${{cost}})"+ "tokens_used_with_model_and_cost": "{{model}}: {{prompt}} prompt + {{completion}} completion = {{total}} tokens (~${{cost}})",+ "tokens": "tokens"},"shared_switch": {"shared": "Shared",diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.cssindex a66761fd73..8b9b619b1f 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.css@@ -565,3 +565,42 @@.llm-chat-usage-text {font-family: var(--monospace-font-family, monospace);}++/* Context window indicator */+.llm-chat-context-window {+ display: flex;+ align-items: center;+ gap: 0.75rem;+ padding: 0.5rem 0;+ margin-bottom: 0.5rem;+}++.llm-chat-context-bar {+ flex: 1;+ height: 6px;+ background: var(--accented-background-color);+ border-radius: 3px;+ overflow: hidden;+}++.llm-chat-context-fill {+ height: 100%;+ background: var(--main-selection-color, #007bff);+ border-radius: 3px;+ transition: width 0.3s ease;+}++.llm-chat-context-fill.warning {+ background: var(--warning-color, #f0ad4e);+}++.llm-chat-context-fill.critical {+ background: var(--danger-color, #d9534f);+}++.llm-chat-context-label {+ font-size: 0.75rem;+ color: var(--muted-text-color);+ font-family: var(--monospace-font-family, monospace);+ white-space: nowrap;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsxindex b7de4c83ba..743e3a7609 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx@@ -46,6 +46,11 @@ interface ModelOption extends LlmModelInfo {costDescription?: string;}+/** Format token count with thousands separators */+function formatTokenCount(tokens: number): string {+ return tokens.toLocaleString();+}+export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {const [messages, setMessages] = useState<StoredMessage[]>([]);const [input, setInput] = useState("");@@ -61,6 +66,7 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {const [enableExtendedThinking, setEnableExtendedThinking] = useState(false);const [error, setError] = useState<string | null>(null);const [shouldSave, setShouldSave] = useState(false);+ const [lastPromptTokens, setLastPromptTokens] = useState<number>(0);const messagesEndRef = useRef<HTMLDivElement>(null);const textareaRef = useRef<HTMLTextAreaElement>(null);@@ -234,6 +240,7 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {},onUsage: (u) => {usage = u;+ setLastPromptTokens(u.promptTokens);},onError: (errorMsg) => {console.error("Chat error:", errorMsg);@@ -369,6 +376,27 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {)}<div ref={messagesEndRef} /></div>+ {lastPromptTokens > 0 && (() => {+ const currentModel = availableModels.find(m => m.id === selectedModel);+ const contextWindow = currentModel?.contextWindow || 200000;+ const percentage = Math.min((lastPromptTokens / contextWindow) * 100, 100);+ const isWarning = percentage > 75;+ const isCritical = percentage > 90;++ return (+ <div className="llm-chat-context-window">+ <div className="llm-chat-context-bar">+ <div+ className={`llm-chat-context-fill ${isCritical ? "critical" : isWarning ? "warning" : ""}`}+ style={{ width: `${percentage}%` }}+ />+ </div>+ <span className="llm-chat-context-label">+ {formatTokenCount(lastPromptTokens)} / {formatTokenCount(contextWindow)} {t("llm_chat.tokens")} ({percentage.toFixed(0)}%)+ </span>+ </div>+ );+ })()}<form className="llm-chat-input-form" onSubmit={handleSubmit}><div className="llm-chat-input-row"><textareadiff --git a/apps/server/src/services/llm/providers/anthropic.ts b/apps/server/src/services/llm/providers/anthropic.tsindex 8e3adc11f6..87ed2d459a 100644--- a/apps/server/src/services/llm/providers/anthropic.ts+++ b/apps/server/src/services/llm/providers/anthropic.ts@@ -26,43 +26,51 @@ const BASE_MODELS: Omit<ModelInfo, "costMultiplier">[] = [id: "claude-sonnet-4-6",name: "Claude Sonnet 4.6",pricing: { input: 3, output: 15 },+ contextWindow: 200000,isDefault: true},{id: "claude-opus-4-6",name: "Claude Opus 4.6",- pricing: { input: 5, output: 25 }+ pricing: { input: 5, output: 25 },+ contextWindow: 200000},{id: "claude-haiku-4-5-20251001",name: "Claude Haiku 4.5",- pricing: { input: 1, output: 5 }+ pricing: { input: 1, output: 5 },+ contextWindow: 200000},// ===== Legacy Models ====={id: "claude-sonnet-4-5-20250929",name: "Claude Sonnet 4.5",- pricing: { input: 3, output: 15 }+ pricing: { input: 3, output: 15 },+ contextWindow: 200000},{id: "claude-opus-4-5-20251101",name: "Claude Opus 4.5",- pricing: { input: 5, output: 25 }+ pricing: { input: 5, output: 25 },+ contextWindow: 200000},{id: "claude-opus-4-1-20250805",name: "Claude Opus 4.1",- pricing: { input: 15, output: 75 }+ pricing: { input: 15, output: 75 },+ contextWindow: 200000},{id: "claude-sonnet-4-20250514",name: "Claude Sonnet 4.0",- pricing: { input: 3, output: 15 }+ pricing: { input: 3, output: 15 },+ contextWindow: 200000},{id: "claude-opus-4-20250514",name: "Claude Opus 4.0",- pricing: { input: 15, output: 75 }+ pricing: { input: 15, output: 75 },+ contextWindow: 200000}];diff --git a/apps/server/src/services/llm/types.ts b/apps/server/src/services/llm/types.tsindex f3e87a8406..5253ab9b19 100644--- a/apps/server/src/services/llm/types.ts+++ b/apps/server/src/services/llm/types.ts@@ -44,6 +44,8 @@ export interface ModelInfo {isDefault?: boolean;/** Cost multiplier relative to the cheapest model (1x = cheapest) */costMultiplier?: number;+ /** Maximum context window size in tokens */+ contextWindow?: number;}export interface LlmProvider {diff --git a/packages/commons/src/lib/llm_api.ts b/packages/commons/src/lib/llm_api.tsindex 81e93d1eef..01523c2ec3 100644--- a/packages/commons/src/lib/llm_api.ts+++ b/packages/commons/src/lib/llm_api.ts@@ -65,6 +65,8 @@ export interface LlmModelInfo {isDefault?: boolean;/** Cost multiplier relative to the cheapest model (1x = cheapest) */costMultiplier?: number;+ /** Maximum context window size in tokens */+ contextWindow?: number;}/**