- Notifications
You must be signed in to change notification settings - Fork 1.2k
[OPIK-2936] [BE] [FE] Display Token & Cost Columns in Experiment Items table #4061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[OPIK-2936] [BE] [FE] Display Token & Cost Columns in Experiment Items table #4061
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds optional "Total tokens" and "Estimated cost" columns to the Experiment Items comparison table, enabling users to view token usage and cost metrics when comparing experiment results.
Key Changes:
- Added
usageandtotal_estimated_costfields to theExperimentItemTypeScript interface - Created new
TokensCellcomponent with Compare variant for displaying token counts - Extended
CostCellcomponent with Compare variant for experiment comparison views
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/opik-frontend/src/types/datasets.ts | Added usage and total_estimated_cost fields to ExperimentItem interface to support token and cost data |
| apps/opik-frontend/src/components/shared/DataTableCells/TokensCell.tsx | Created new cell component for displaying token counts with Compare variant for experiment comparison |
| apps/opik-frontend/src/components/shared/DataTableCells/CostCell.tsx | Extended existing CostCell with Compare variant for experiment comparison views |
| apps/opik-frontend/src/components/pages/CompareExperimentsPage/ExperimentItemsTab/ExperimentItemsTab.tsx | Added token and cost column definitions to the experiment items comparison table |
SDK E2E Tests Results0 tests 0 ✅ 0s ⏱️ Results for commit 3a01f8b. ♻️ This comment has been updated with latest results. |
649fc86 to 4f97fbb Compare d016fce to 2a245c7 Compare …xperiment Items table Add display and sorting capabilities for token usage and cost metrics in the experiment items comparison view. Features: - Display Total tokens and Estimated cost columns in experiment items table - Sort by both metrics (ascending/descending) - AVG/SUM statistics in column headers - Support for multi-experiment comparison view with vertical split cells Frontend Changes: - Updated ExperimentItem interface to include usage and total_estimated_cost fields - Enhanced CostCell.Compare component with generic accessor support for flexible data access - Added columns to ExperimentItemsTab with proper formatters and statistics Backend Changes: - Extended SQL queries to aggregate usage and cost data from spans - Added USAGE_TOTAL_TOKENS and TOTAL_ESTIMATED_COST to sortable fields - Updated StatsMapper to calculate usage statistics (total, prompt, completion tokens) - Added validation for new sortable fields in SortingFactoryDatasets - Implemented comprehensive test coverage for sorting functionality Testing: - Added backend integration tests for sorting by total_estimated_cost and usage.total_tokens - Verified multi-experiment comparison rendering - Tested null/missing data handling Note: Filtering capability intentionally excluded due to complexity of filtering aggregated fields. Future work tracked in OPIK-3115.
722f0d8 to 4b5f682 Compare …OPIK-2936-display-token-cost-columns-experiment-items
…-experiment-items
| ✅ Test environment is now available! Access Information
The deployment has completed successfully and the version has been verified. |
… ExperimentItemsTab component.
7852e6e to ec7b1fc Compare | ✅ Test environment is now available! Access Information
The deployment has completed successfully and the version has been verified. |
andrescrz left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BE LGTM. You can move forward once all FE reviews pass.
apps/opik-backend/src/main/java/com/comet/opik/domain/stats/StatsMapper.java Outdated Show resolved Hide resolved
Extract duplicated map processing logic from mapExperimentItemsStats into a parameterized helper method (addMapStats) to follow DRY principle. Addresses PR review feedback from @andrescrz about code duplication between feedbackScoresMap and usageMap processing. Changes: - Add addMapStats() helper method with proper Javadoc - Simplify both feedbackScoresMap and usageMap calls to use helper - No behavioral changes, pure refactoring
00e2135 to c6a5eb6 Compare Apply the same DRY principle to the mapProjectStats method, which also had duplicated map processing logic for usage and feedbackScores. Now both mapProjectStats and mapExperimentItemsStats use the same reusable addMapStats helper method, completely eliminating the copy-paste pattern that was mentioned in the PR review. The helper method now takes Row and fieldName directly, making call sites even cleaner by eliminating redundant local variables and field name repetition. Changes: - Refactor usage and feedbackScores processing in mapProjectStats - Refactor feedbackScoresMap and usageMap processing in mapExperimentItemsStats - Update addMapStats to accept Row and extract map internally - All call sites now use: addMapStats(row, FIELD_NAME, stats) - Single @SuppressWarnings at helper method level (not at call sites)
c6a5eb6 to e807a34 Compare
andrescrz left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BE LGTM.
| BigDecimal.valueOf(25.75), | ||
| BigDecimal.valueOf(5.25)); | ||
| | ||
| for (int i = 0; i < 3; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: use PodamFactoryUtils.manufacturePojoList and create spans in batch calls for these tests.
Details
This PR adds display and sorting capabilities for token usage and cost metrics in the experiment items comparison view, enabling users to analyze the resource consumption of their experiment runs.
Features Implemented
Frontend Changes
Files Modified:
ExperimentItemsTab.tsx- Added new column definitions with statistics and formattersCostCell.tsx- Enhanced Compare component with generic accessor support for flexible data accesstypes/datasets.ts- Updated ExperimentItem interface to includeusageandtotal_estimated_costfieldsKey Implementation Details:
accessorincustomMetafor dynamic data extraction from experiment itemsBackend Changes
Files Modified:
DatasetItemDAO.java- Extended SQL queries to aggregateusageandtotal_estimated_costfrom spansExperimentsComparisonValidKnownField.java- AddedUSAGE_TOTAL_TOKENSandTOTAL_ESTIMATED_COSTfieldsSortingFactoryDatasets.java- Added new fields to sortable fields validationFilterQueryBuilder.java- Updated field mappings (for future filtering support)StatsMapper.java- Enhanced to calculate usage statistics (total, prompt, completion tokens) + refactored to eliminate code duplication (see Code Quality section)DatasetsResourceTest.java- Added comprehensive integration testsKey Implementation Details:
sumMap(usage) as usageandSUM(total_estimated_cost)usage.total_tokensdot notation (leveraging existing wildcard support)avgMap(usage)for tokens andavg(total_estimated_cost)for costsCode Quality Improvements
Refactoring to Address PR Feedback (Revisions 2-3):
Based on @andrescrz's review feedback about code duplication in
StatsMapper.java, we refactored the map processing logic:Before (4 duplicated blocks across 2 methods):
After (single reusable helper):
Impact:
Testing
Backend Integration Tests:
sortByTotalEstimatedCost__whenDescendingOrder__thenReturnSortedsortByUsageTotalTokens__whenAscendingOrder__thenReturnSortedManual Testing:
Important Note: Filtering Capability
Filtering by "Total tokens" and "Estimated cost" is intentionally NOT included in this PR due to technical complexity. These metrics are aggregated from spans in ClickHouse, requiring filters to be applied AFTER span aggregation rather than in the main WHERE clause. This necessitates significant SQL query restructuring.
Future work: Filtering capability tracked in OPIK-3115
Change checklist
Issues
Documentation
N/A - No documentation changes needed for this UI enhancement. The feature follows existing patterns for optional columns and sorting in data tables.
Review Status