Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

📦 refactor: Move the Token-Usage Jotai State Into the TokenUsage Feature (#16950)

Marco Beretta9d2e30fcfca8
diff --git a/client/src/components/Chat/Input/TokenUsage/Breakdown.tsx b/client/src/components/Chat/Input/TokenUsage/Breakdown.tsx
index e7281ff6d9..540f1c95d3 100644
--- a/client/src/components/Chat/Input/TokenUsage/Breakdown.tsx
+++ b/client/src/components/Chat/Input/TokenUsage/Breakdown.tsx
@@ -17,7 +17,7 @@ import type { MeterSegment } from '@librechat/client';
import type { TokenUsageView } from '~/hooks/Chat/useTokenUsage';
import type { CurrencyConfig } from '~/utils';
import { groupToolTokens, formatTokens, formatCost, normalizeTokenCount, cn } from '~/utils';
-import { contextBreakdownExpandedAtom } from '~/store/usage';
+import { contextBreakdownExpandedAtom } from './store';
import { useLocalize } from '~/hooks';
interface RowProps {
diff --git a/client/src/store/usage.spec.ts b/client/src/components/Chat/Input/TokenUsage/store.spec.ts
similarity index 99%
rename from client/src/store/usage.spec.ts
rename to client/src/components/Chat/Input/TokenUsage/store.spec.ts
index 79a1d26bf5..0dc1d9684e 100644
--- a/client/src/store/usage.spec.ts
+++ b/client/src/components/Chat/Input/TokenUsage/store.spec.ts
@@ -1,4 +1,4 @@
-import { overheadKey, setModelOverhead, getModelOverhead } from './usage';
+import { overheadKey, setModelOverhead, getModelOverhead } from './store';
describe('model overhead cache', () => {
it('keys agents by agentId so the resolved reader and raw writer agree', () => {
diff --git a/client/src/store/usage.ts b/client/src/components/Chat/Input/TokenUsage/store.ts
similarity index 99%
rename from client/src/store/usage.ts
rename to client/src/components/Chat/Input/TokenUsage/store.ts
index 37b996cd95..20c6fa2b66 100644
--- a/client/src/store/usage.ts
+++ b/client/src/components/Chat/Input/TokenUsage/store.ts
@@ -3,7 +3,7 @@ import { atom, getDefaultStore } from 'jotai';
import type { TMessage, TContextUsageEvent } from 'librechat-data-provider';
import type { BranchTotals, BranchUsage } from '~/utils/tokens';
import { EMPTY_BRANCH, EMPTY_USAGE } from '~/utils/tokens';
-import { createStorageAtom } from './jotai-utils';
+import { createStorageAtom } from '~/store/jotai-utils';
/** Sticky preference: does the context popover open with the breakdown showing?
* The gauge alone is the default; a user who wants the detail sets it once. */
diff --git a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx
index 2178cab33a..0a524a9a51 100644
--- a/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx
+++ b/client/src/hooks/Chat/__tests__/useChatFunctions.regenerate.spec.tsx
@@ -19,8 +19,11 @@ import type {
} from 'librechat-data-provider';
import type { ReactNode } from 'react';
import type { ExtendedFile } from '~/common';
+import {
+ activeUsageResponseIdFamily,
+ pendingUsageFamily,
+} from '~/components/Chat/Input/TokenUsage/store';
import { pendingReasoningOverrideFamily } from '~/components/Chat/Input/Composer/state';
-import { activeUsageResponseIdFamily, pendingUsageFamily } from '~/store/usage';
import { revealedQueuedTurnFamily } from '~/store/steer';
import useChatFunctions from '../useChatFunctions';
import { isPasteSubmitted } from '~/utils';
diff --git a/client/src/hooks/Chat/__tests__/useTokenUsage.spec.tsx b/client/src/hooks/Chat/__tests__/useTokenUsage.spec.tsx
index 0abc3464c6..3c945e5647 100644
--- a/client/src/hooks/Chat/__tests__/useTokenUsage.spec.tsx
+++ b/client/src/hooks/Chat/__tests__/useTokenUsage.spec.tsx
@@ -3,7 +3,7 @@ import { act, renderHook } from '@testing-library/react';
import { Constants, QueryKeys } from 'librechat-data-provider';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { TMessage, TConversation } from 'librechat-data-provider';
-import type { ContextSnapshot } from '~/store/usage';
+import type { ContextSnapshot } from '~/components/Chat/Input/TokenUsage/store';
import {
contextSnapshotFamily,
pendingUsageFamily,
@@ -12,7 +12,7 @@ import {
liveTokensFamily,
removeUsageAtoms,
snapshotsByAnchorFamily,
-} from '~/store/usage';
+} from '~/components/Chat/Input/TokenUsage/store';
import { getRegenerateSubmissionMessages } from '~/hooks/Chat/useChatFunctions';
import { useLatestMessageId } from '~/hooks/Messages/useLatestMessage';
import useUsageHandler from '~/hooks/SSE/useUsageHandler';
diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts
index 368bca7cc8..bfd192c065 100644
--- a/client/src/hooks/Chat/useChatFunctions.ts
+++ b/client/src/hooks/Chat/useChatFunctions.ts
@@ -53,6 +53,7 @@ import {
withSubmittedCodeDecision,
resolveSubmittedCodeApprovalMode,
} from '~/hooks/Agents/codeDecision';
+import { activeUsageResponseIdFamily } from '~/components/Chat/Input/TokenUsage/store';
import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse';
import { usesReasoningCapabilities } from '~/hooks/Endpoint/useModelReasoning';
import useGetConversation from '~/hooks/Conversations/useGetConversation';
@@ -62,7 +63,6 @@ import { useAgentsMapContext } from '~/Providers/AgentsMapContext';
import { useChatSettings } from '~/Providers/ChatSettingsContext';
import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace';
import useGetSender from '~/hooks/Conversations/useGetSender';
-import { activeUsageResponseIdFamily } from '~/store/usage';
import { revealedQueuedTurnFamily } from '~/store/steer';
import store, { useGetEphemeralAgent } from '~/store';
import { startupConfigKey } from '~/data-provider';
diff --git a/client/src/hooks/Chat/useTokenUsage.ts b/client/src/hooks/Chat/useTokenUsage.ts
index 578ab8684f..87cccb3a33 100644
--- a/client/src/hooks/Chat/useTokenUsage.ts
+++ b/client/src/hooks/Chat/useTokenUsage.ts
@@ -3,8 +3,8 @@ import { useQueryClient } from '@tanstack/react-query';
import { useStore, useAtomValue, useSetAtom } from 'jotai';
import { Constants, QueryKeys } from 'librechat-data-provider';
import type { TMessage, TConversation, TModelTokenomics } from 'librechat-data-provider';
+import type { ContextSnapshot } from '~/components/Chat/Input/TokenUsage/store';
import type { BranchTotals, BranchUsage } from '~/utils/tokens';
-import type { ContextSnapshot } from '~/store/usage';
import {
overheadKey,
getModelOverhead,
@@ -19,7 +19,7 @@ import {
branchTotalsFamily,
contextSnapshotFamily,
snapshotsByAnchorFamily,
-} from '~/store/usage';
+} from '~/components/Chat/Input/TokenUsage/store';
import {
buildIndex,
upsertEntries,
diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
index 7aea121390..c172ccdd8f 100644
--- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
+++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
@@ -19,7 +19,7 @@ import {
branchTotalsFamily,
pendingUsageFamily,
removeUsageAtoms,
-} from '~/store/usage';
+} from '~/components/Chat/Input/TokenUsage/store';
import { recoveryDispositionsFamily } from '~/components/Chat/Steering/recovery';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
diff --git a/client/src/hooks/SSE/__tests__/useUsageHandler.spec.tsx b/client/src/hooks/SSE/__tests__/useUsageHandler.spec.tsx
index 954c46f581..29d072e039 100644
--- a/client/src/hooks/SSE/__tests__/useUsageHandler.spec.tsx
+++ b/client/src/hooks/SSE/__tests__/useUsageHandler.spec.tsx
@@ -13,7 +13,7 @@ import {
liveTokensFamily,
subagentUsageFamily,
pendingSubagentUsageFamily,
-} from '~/store/usage';
+} from '~/components/Chat/Input/TokenUsage/store';
import { buildIndex, sumTotalUsage, sumBranch, clearIndex } from '~/utils/tokens';
import useUsageHandler from '~/hooks/SSE/useUsageHandler';
diff --git a/client/src/hooks/SSE/useUsageHandler.ts b/client/src/hooks/SSE/useUsageHandler.ts
index 6b5f47943a..cd0d95b616 100644
--- a/client/src/hooks/SSE/useUsageHandler.ts
+++ b/client/src/hooks/SSE/useUsageHandler.ts
@@ -7,7 +7,7 @@ import type {
TTokenUsageEvent,
TContextUsageEvent,
} from 'librechat-data-provider';
-import type { ContextSnapshot } from '~/store/usage';
+import type { ContextSnapshot } from '~/components/Chat/Input/TokenUsage/store';
import {
overheadKey,
markUsageFolded,
@@ -26,7 +26,7 @@ import {
EMPTY_USAGE_TOTALS,
contextSnapshotFamily,
snapshotsByAnchorFamily,
-} from '~/store/usage';
+} from '~/components/Chat/Input/TokenUsage/store';
import {
sumBranch,
mergeUsage,
diff --git a/client/src/store/index.ts b/client/src/store/index.ts
index f8d12e561f..5ad0853243 100644
--- a/client/src/store/index.ts
+++ b/client/src/store/index.ts
@@ -17,7 +17,6 @@ export * from './mcp';
export * from './favorites';
export * from './sandbox';
export * from './ptc';
-export * from './usage';
export default {
...artifacts,