Skip to content

refactor: removed the token calculator logic#636

Open
blackmammoth wants to merge 2 commits intomainfrom
refactor/remove-token-usage-logic
Open

refactor: removed the token calculator logic#636
blackmammoth wants to merge 2 commits intomainfrom
refactor/remove-token-usage-logic

Conversation

@blackmammoth
Copy link
Copy Markdown
Collaborator

@blackmammoth blackmammoth commented Apr 10, 2026

The information given was not accurate for the model providers. We can add it later on when we can get accurate responses from the sdks/spawned processes.

Summary by CodeRabbit

  • Removed Features
    • Token usage tracking and visual indicators have been removed from the chat UI and composer.
    • The built-in /cost command has been removed.
    • Non-streaming API responses and session/history endpoints no longer include token usage or token-budget data; streaming messages no longer emit token-usage/status updates.
  • Documentation
    • Public API docs updated to omit token accounting references.

The information given was not accurate for the model providers
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 10, 2026

📝 Walkthrough

Walkthrough

This PR removes token-usage and token-budget tracking across backend, adapters, API surface, and frontend UI/state, deleting the token-usage endpoint, built-in /cost command, token-related normalized/status messages, and associated UI components/props.

Changes

Cohort / File(s) Summary
Docs
public/api-docs.html
Removed token accounting text and example usage/tokens fields from the non-streaming /api/agent documentation.
Server: routing & endpoints
server/index.js, server/routes/agent.js, server/routes/commands.js
Deleted GET /api/.../token-usage endpoint; removed getTotalTokens() and token aggregation from agent responses; removed built-in /cost command and handler.
Server: SDK & streaming
server/claude-sdk.js
Removed extractTokenBudget and eliminated emission of token_budget status messages from streaming/result events.
Server: providers & normalization
server/openai-codex.js, server/providers/codex/adapter.js, server/providers/gemini/adapter.js, server/projects.js
Stopped extracting/propagating usage/tokenUsage/tokenBudget from provider events/history; simplified result normalization to drop token/status emissions.
Types & stores
server/providers/types.js, src/stores/useSessionStore.ts
Removed tokens, tokenBudget, and tokenUsage fields from JSDoc/TypeScript interfaces and store slot shapes.
Frontend: hooks & state
src/components/chat/hooks/useChatSessionState.ts, src/components/chat/hooks/useChatComposerState.ts, src/components/chat/hooks/useChatRealtimeHandlers.ts
Removed tokenBudget state and setters, deleted fetch effect for token usage, and updated claudeStatus/handler signatures to exclude tokens.
Frontend: components & UI
src/components/chat/view/ChatInterface.tsx, src/components/chat/view/subcomponents/ChatComposer.tsx, src/components/chat/view/subcomponents/ChatInputControls.tsx, src/components/chat/view/subcomponents/ClaudeStatus.tsx
Removed tokenBudget prop threading, stopped passing token data to composer/controls, and updated claudeStatus prop type to omit tokens.
Frontend: removed component
src/components/chat/view/subcomponents/TokenUsagePie.tsx
Deleted the TokenUsagePie component (SVG donut visualization and tooltip) and its props.

Sequence Diagram(s)

sequenceDiagram
  participant Provider as Provider Adapter
  participant Server as Server (normalize/SDK)
  participant WS as WebSocket Emission
  participant Client as Frontend Client

  Provider->>Server: emits result/turn.completed (with usage)
  alt Old flow (before PR)
    Server->>WS: emit normalized messages + status(token_budget)
    WS->>Client: receive status(token_budget) -> update tokenBudget/UI
  else New flow (after PR)
    Server->>WS: emit normalized messages (no token_budget status)
    WS->>Client: receive messages -> no tokenBudget updates
  end
Loading

Possibly related PRs

  • PR #558: Modifies provider adapters and normalized message emission that this PR removes; strong overlap in messaging surface.
  • PR #208: Adds token-budget reporting and websocket emissions which this PR deletes; directly conflicts on token paths.
  • PR #374: Implements token-budget emission and /cost handling that this PR removes; related at the command/endpoint level.

Suggested reviewers

  • blackmammoth

Poem

🐇 I hopped through logs and trimmed the trace,
Tokens tucked away without a chase,
No doughnut charts to nibble now,
Messages leaner—take a bow,
Hooray for lighter, tidier space! 🌿

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: removal of token calculator logic across backend and frontend code, including token tracking, counting, API endpoints, and UI components.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/remove-token-usage-logic

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@blackmammoth blackmammoth requested a review from viper151 April 10, 2026 13:26
@viper151
Copy link
Copy Markdown
Contributor

@blackmammoth this removes more than it should actually. The tokens being returned from at least claude code are accurate. the visualization we have indeed is not relevant anymore. But the tokens are coming straight out of the sdk.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
server/projects.js (1)

1814-1814: Minor inconsistency in return structure.

The non-paginated return at line 1814 returns { messages } while the paginated return (lines 1805-1811) returns { messages, total, hasMore, offset, limit }. This asymmetry could cause issues for callers expecting consistent structure.

Consider returning a consistent structure for both paths:

♻️ Suggested fix for consistent return structure
-    return { messages };
+    return { messages, total: messages.length, hasMore: false, offset: 0, limit: null };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/projects.js` at line 1814, The final non-paginated return currently
returns only { messages } and should match the paginated shape used earlier ({
messages, total, hasMore, offset, limit }); update the return at the end of the
function in server/projects.js so it returns the same keys (e.g., include total,
hasMore, offset and limit with sensible defaults like total = messages.length,
hasMore = false, offset = 0 and limit = messages.length or null) to ensure
callers always receive a consistent response shape.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@server/projects.js`:
- Line 1814: The final non-paginated return currently returns only { messages }
and should match the paginated shape used earlier ({ messages, total, hasMore,
offset, limit }); update the return at the end of the function in
server/projects.js so it returns the same keys (e.g., include total, hasMore,
offset and limit with sensible defaults like total = messages.length, hasMore =
false, offset = 0 and limit = messages.length or null) to ensure callers always
receive a consistent response shape.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 400c9012-2e14-442a-98d9-d85e8f046b97

📥 Commits

Reviewing files that changed from the base of the PR and between 2207d05 and cc7f652.

📒 Files selected for processing (19)
  • public/api-docs.html
  • server/claude-sdk.js
  • server/index.js
  • server/openai-codex.js
  • server/projects.js
  • server/providers/codex/adapter.js
  • server/providers/gemini/adapter.js
  • server/providers/types.js
  • server/routes/agent.js
  • server/routes/commands.js
  • src/components/chat/hooks/useChatComposerState.ts
  • src/components/chat/hooks/useChatRealtimeHandlers.ts
  • src/components/chat/hooks/useChatSessionState.ts
  • src/components/chat/view/ChatInterface.tsx
  • src/components/chat/view/subcomponents/ChatComposer.tsx
  • src/components/chat/view/subcomponents/ChatInputControls.tsx
  • src/components/chat/view/subcomponents/ClaudeStatus.tsx
  • src/components/chat/view/subcomponents/TokenUsagePie.tsx
  • src/stores/useSessionStore.ts
💤 Files with no reviewable changes (8)
  • server/claude-sdk.js
  • server/providers/codex/adapter.js
  • server/routes/commands.js
  • src/components/chat/view/ChatInterface.tsx
  • src/stores/useSessionStore.ts
  • server/index.js
  • src/components/chat/view/subcomponents/TokenUsagePie.tsx
  • src/components/chat/view/subcomponents/ChatInputControls.tsx

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/routes/agent.js`:
- Around line 549-554: The code currently deletes provider-native usage by
removing assistantMessage.message.usage before pushing to assistantMessages;
instead, stop deleting message.usage so the raw provider usage block from
parsed.data is preserved for non-streaming responses—remove the lines that clone
and delete assistantMessage.message.usage in the assistantMessage creation (and
the parallel block that does the same around the 1131–1139 pattern) so
parsed.data.message.usage is retained when pushing to assistantMessages.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 445ea302-be74-4b2a-8eb0-97580732b86f

📥 Commits

Reviewing files that changed from the base of the PR and between cc7f652 and fce8ad0.

📒 Files selected for processing (3)
  • public/api-docs.html
  • server/index.js
  • server/routes/agent.js
💤 Files with no reviewable changes (1)
  • server/index.js
✅ Files skipped from review due to trivial changes (1)
  • public/api-docs.html

Comment on lines +549 to +554
const assistantMessage = { ...parsed.data };
if (assistantMessage.message?.usage) {
assistantMessage.message = { ...assistantMessage.message };
delete assistantMessage.message.usage;
}
assistantMessages.push(assistantMessage);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Preserve provider-native message.usage in non-streaming responses.

This strips the Claude SDK’s raw usage block from messages, and the non-streaming response later returns those filtered messages directly. That removes the only accurate token data for non-streaming clients while streaming clients still receive it, which is broader than removing the derived token calculator.

Suggested fix
-          if (parsed.type === 'claude-response' && parsed.data && parsed.data.type === 'assistant') {
-            const assistantMessage = { ...parsed.data };
-            if (assistantMessage.message?.usage) {
-              assistantMessage.message = { ...assistantMessage.message };
-              delete assistantMessage.message.usage;
-            }
-            assistantMessages.push(assistantMessage);
+          if (parsed.type === 'claude-response' && parsed.data && parsed.data.type === 'assistant') {
+            assistantMessages.push({
+              ...parsed.data,
+              ...(parsed.data.message ? { message: { ...parsed.data.message } } : {})
+            });
           }

Also applies to: 1131-1139

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/routes/agent.js` around lines 549 - 554, The code currently deletes
provider-native usage by removing assistantMessage.message.usage before pushing
to assistantMessages; instead, stop deleting message.usage so the raw provider
usage block from parsed.data is preserved for non-streaming responses—remove the
lines that clone and delete assistantMessage.message.usage in the
assistantMessage creation (and the parallel block that does the same around the
1131–1139 pattern) so parsed.data.message.usage is retained when pushing to
assistantMessages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants