Conversation
Implements cost calculation and backfill for agent runs, updates API endpoints and repository methods to support cost aggregation, and displays model pricing and run costs in the frontend. Upgrades Prompture dependency to >=0.0.49 for improved reasoning and pricing support.
There was a problem hiding this comment.
Pull request overview
This PR implements comprehensive cost tracking and pricing display for agent runs by integrating Prompture's pricing engine, updating the backend to capture and aggregate cost data, and enhancing the frontend to display both model pricing and run costs.
Changes:
- Upgraded Prompture dependency from >=0.0.47 to >=0.0.49 for improved pricing support
- Added cost extraction from usage data in the pipeline, cost aggregation in daily stats, and a backfill endpoint to recalculate historical costs
- Updated the models API to return grouped providers with pricing information, and added UI components to display model pricing and per-agent run costs
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| requirements.txt | Upgraded prompture to >=0.0.49 |
| pyproject.toml | Upgraded prompture to >=0.0.49 in project dependencies |
| agentsite/storage/repository.py | Added cost column to daily stats aggregation and implemented backfill_costs method to recalculate historical costs |
| agentsite/engine/pipeline.py | Added cost extraction from usage data and included cost in agent_complete events |
| agentsite/api/routes/models.py | Integrated pricing data from prompture.model_rates into model listings |
| agentsite/api/routes/agents.py | Added POST endpoint for cost backfill operation |
| agentsite/api/app.py | Added automatic cost backfill on application startup |
| agentsite/engine/reasoning_patch.py | Updated comment to reflect Prompture version 0.0.49 |
| frontend/src/pages/ModelsPage.jsx | Added formatPrice function and pricing display column for model costs per 1M tokens |
| frontend/src/hooks/useGeneration.js | Added cost field to agent state from WebSocket events |
| frontend/src/components/builder/ProgressPipeline.jsx | Added formatCost function and cost display in agent progress indicators |
| tests/test_api.py | Updated test to expect "groups" dict instead of "models" list in API response |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (rate == null) return "—"; | ||
| if (rate === 0) return "free"; | ||
| if (rate >= 1) return `$${rate.toFixed(2)}`; | ||
| return `$${rate.toFixed(4)}`.replace(/0+$/, ""); |
There was a problem hiding this comment.
The regex pattern could leave a trailing decimal point for very small rates. If a rate rounds to 0.0000 with toFixed(4) (e.g., rate = 0.00001), the regex will produce "0." which is invalid. Consider using the pattern /\.?0+$/ to also remove the decimal point if all trailing digits are zeros, or add an additional check for rate === 0 after rounding.
| return `$${rate.toFixed(4)}`.replace(/0+$/, ""); | |
| return `$${rate.toFixed(4)}`.replace(/\.?0+$/, ""); |
| model_id = model_str.split("/", 1)[1] if "/" in model_str else model_str | ||
| try: | ||
| drv = get_driver_for_model(model_str) | ||
| cost = drv._calculate_cost(provider, model_id, row["input_tokens"], row["output_tokens"]) |
There was a problem hiding this comment.
This code is accessing a private method _calculate_cost from the Prompture driver, which could break if the library's internal implementation changes. Consider using the public API get_model_rates from prompture.model_rates (already imported elsewhere in the codebase at agentsite/api/routes/models.py:34) and calculating the cost manually from the rates, or check if Prompture >=0.0.49 provides a public method for cost calculation.
| except Exception: | ||
| pass |
There was a problem hiding this comment.
'except' clause does nothing but pass and there is no explanatory comment.
| except Exception: | |
| pass | |
| except Exception as exc: | |
| logger.debug( | |
| "Failed to fetch pricing for %s/%s: %s", provider, raw_id, exc | |
| ) |
Implements cost calculation and backfill for agent runs, updates API endpoints and repository methods to support cost aggregation, and displays model pricing and run costs in the frontend. Upgrades Prompture dependency to >=0.0.49 for improved reasoning and pricing support.