feat: Store and display reasoning tokens separately#5429
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Claude finished @H2Shami's task —— View job PR Review: Store and Display Reasoning Tokens SeparatelyReviewing this PR for critical issues, code quality, and best practices... Review Tasks:
Issues Found:🚨 CRITICAL: NULL Handling in ClickHouse Queries (Lines 198, 281)
(prompt_tokens + completion_tokens + ifNull(reasoning_tokens, 0)) AS total_tokensThis affects:
✅ Positive Findings:
Performance & Code Quality:Good:
Minor Suggestions:
Score: 6/10 - Has one critical NULL handling bug that must be fixed before merge Suggestions Summary:
The implementation is solid overall with good test coverage and proper separation of concerns. The critical NULL handling issue is straightforward to fix and then this will be ready to merge. |
There was a problem hiding this comment.
Pull request overview
This PR adds support for storing and displaying reasoning tokens separately from completion tokens. Reasoning tokens (also known as "thinking" tokens) are used by models like o1 that perform internal reasoning before generating responses. The implementation ensures these tokens are tracked independently for proper cost calculation and analytics.
Key changes:
- Added
reasoning_tokensfield to database schema and API types - Modified token calculation logic to exclude reasoning tokens from completion tokens
- Updated total_tokens calculation to include reasoning tokens separately
- Added UI column and filter for reasoning tokens in the requests table
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| clickhouse/migrations/schema_77_reasoning_tokens.sql | Adds reasoning_tokens column to ClickHouse table |
| valhalla/jawn/src/lib/handlers/HandlerContext.ts | Implements getReasoningTokens function and updates token calculations |
| valhalla/jawn/src/lib/handlers/LoggingHandler.ts | Integrates reasoning tokens into logging logic |
| valhalla/jawn/src/lib/stores/request/request.ts | Updates SQL queries to include reasoning_tokens in calculations |
| valhalla/jawn/src/lib/handlers/tests/HandlerContext.test.ts | Adds comprehensive tests for token calculation functions |
| web/components/templates/requests/initialColumns.tsx | Adds "Reasoning Tokens" column to requests table |
| web/filterAST/filterUIDefinitions/staticDefinitions.ts | Adds filter definition for reasoning tokens |
| packages/llm-mapper/types.ts | Adds reasoning_tokens to type definitions |
| Multiple API/type files | Updates generated types and schemas to include reasoning_tokens |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ai_gateway_body_mapping, | ||
| time_to_first_token, | ||
| (prompt_tokens + completion_tokens) AS total_tokens, | ||
| (prompt_tokens + completion_tokens + reasoning_tokens) AS total_tokens, |
There was a problem hiding this comment.
The total_tokens calculation may produce incorrect results when reasoning_tokens is NULL. In ClickHouse, NULL + any number = NULL. Consider using COALESCE or ifNull to handle NULL values:
(prompt_tokens + completion_tokens + ifNull(reasoning_tokens, 0)) AS total_tokens
This ensures that when reasoning_tokens is NULL (for models that don't support reasoning tokens), it's treated as 0 rather than making the entire sum NULL.
| (prompt_tokens + completion_tokens + reasoning_tokens) AS total_tokens, | |
| (prompt_tokens + completion_tokens + ifNull(reasoning_tokens, 0)) AS total_tokens, |
| ai_gateway_body_mapping, | ||
| time_to_first_token, | ||
| (prompt_tokens + completion_tokens) AS total_tokens, | ||
| (prompt_tokens + completion_tokens + reasoning_tokens) AS total_tokens, |
There was a problem hiding this comment.
The total_tokens calculation may produce incorrect results when reasoning_tokens is NULL. In ClickHouse, NULL + any number = NULL. Consider using COALESCE or ifNull to handle NULL values:
(prompt_tokens + completion_tokens + ifNull(reasoning_tokens, 0)) AS total_tokens
This ensures that when reasoning_tokens is NULL (for models that don't support reasoning tokens), it's treated as 0 rather than making the entire sum NULL.
| (prompt_tokens + completion_tokens + reasoning_tokens) AS total_tokens, | |
| (prompt_tokens + completion_tokens + ifNull(reasoning_tokens, 0)) AS total_tokens, |
| @@ -0,0 +1,2 @@ | |||
| ALTER TABLE request_response_rmt | |||
| ADD COLUMN reasoning_tokens Nullable(Int64) DEFAULT 0; | |||
There was a problem hiding this comment.
Should not be Nullable
Ticket
Link to the ticket(s) this pull request addresses.
Component/Service
What part of Helicone does this affect?
Type of Change
Testing
Technical Considerations
Dependencies
Deployment Notes
Context
UX + more info
Screenshots / Demos
Misc. Review Notes