Skip to content

Fix cost tracking: use actual routed provider for cost calculation (#341) - #405

Merged
joelteply merged 1 commit into
mainfrom
fix/cost-tracking-341
Mar 25, 2026
Merged

joelteply merged 1 commit into
mainfrom
fix/cost-tracking-341

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

Summary

  • calculateModelCost() used this.modelConfig.provider (persona's default, e.g., candle=$0) instead of the actual routed provider (e.g., deepseek after task routing upgrade)
  • All cloud-upgraded generations showed $0 cost in ai/cost reports
  • Fix: use request.provider (post-routing) for cost calculation

Partial fix for #341 — cost tracking now accurate. Budget limits and hard caps still TODO.

Test plan

  • TypeScript compilation clean
  • ai/cost command returns data (667 generations in 1h)
  • Verify non-zero costs appear after routing upgrades

🤖 Generated with Claude Code

)

calculateModelCost() used this.modelConfig.provider (persona's default candle)
instead of the routed provider (deepseek after task routing upgrade).
All cloud-upgraded generations showed $0 cost.

Fix: use request.provider (post-routing) for cost calculation.
Copilot AI review requested due to automatic review settings March 25, 2026 05:41
@joelteply
joelteply merged commit 90866ce into main Mar 25, 2026
@joelteply
joelteply deleted the fix/cost-tracking-341 branch March 25, 2026 05:41

Copilot AI left a comment

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.

Pull request overview

This PR aims to fix incorrect cost attribution when a persona’s default provider/model is upgraded by task routing, so cost calculations reflect the post-routing provider/model rather than the persona defaults.

Changes:

  • Compute actualProvider / actualModel from the routed request and use them for calculateModelCost() instead of this.modelConfig.*.
  • Add inline comments clarifying why the routed provider/model must be used.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +572 to +576
const actualProvider = request.provider ?? this.modelConfig.provider;
const actualModel = request.model ?? this.modelConfig.model;
const cost = calculateModelCost(
this.modelConfig.provider,
this.modelConfig.model,
actualProvider,
actualModel,

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

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

cost is now calculated using request.provider/model, but CognitionLogger.logResponseGeneration is still being passed this.modelConfig.provider/model immediately below. That will store a provider/model that no longer matches the cost you computed, which can break any provider/model breakdowns from response_generation_logs and makes the telemetry inconsistent. Pass the same actualProvider/actualModel into logResponseGeneration (and consider reusing them for any related generation metadata emitted in this block).

Copilot uses AI. Check for mistakes.
Comment on lines +570 to 579
// Use the ACTUAL provider/model (after task routing), not the persona's default.
// Without this, candle→deepseek upgrades show $0 cost.
const actualProvider = request.provider ?? this.modelConfig.provider;
const actualModel = request.model ?? this.modelConfig.model;
const cost = calculateModelCost(
this.modelConfig.provider,
this.modelConfig.model,
actualProvider,
actualModel,
inputTokenEstimate,
outputTokenEstimate
);

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

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

PR description/title mention fixing $0 costs in ai/cost reports, but this change only affects the cost estimate logged via CognitionLogger.logResponseGeneration (per-persona response_generation_logs). The ai/cost command currently queries ai_generations from the metrics DB (logged by AIProviderDaemon) and won't be impacted by this change. Either update the PR description/scope, or apply the routed-provider fix in the code path that populates AIGenerationEntity.estimatedCost.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants