Skip to content

Add cost tracking and pricing display for agent runs#9

Merged
jhd3197 merged 1 commit into
mainfrom
dev
Feb 2, 2026
Merged

Add cost tracking and pricing display for agent runs#9
jhd3197 merged 1 commit into
mainfrom
dev

Conversation

@jhd3197

@jhd3197 jhd3197 commented Feb 2, 2026

Copy link
Copy Markdown
Owner

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.

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.
Copilot AI review requested due to automatic review settings February 2, 2026 03:12
@jhd3197 jhd3197 merged commit 5f7129f into main Feb 2, 2026
6 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

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 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+$/, "");

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
return `$${rate.toFixed(4)}`.replace(/0+$/, "");
return `$${rate.toFixed(4)}`.replace(/\.?0+$/, "");

Copilot uses AI. Check for mistakes.
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"])

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +58 to +59
except Exception:
pass

Copilot AI Feb 2, 2026

Copy link

Choose a reason for hiding this comment

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

'except' clause does nothing but pass and there is no explanatory comment.

Suggested change
except Exception:
pass
except Exception as exc:
logger.debug(
"Failed to fetch pricing for %s/%s: %s", provider, raw_id, exc
)

Copilot uses AI. Check for mistakes.
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.

2 participants