Repository navigation
Fix cost tracking: use actual routed provider for cost calculation (#341) - #405
Conversation
There was a problem hiding this comment.
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/actualModelfrom the routedrequestand use them forcalculateModelCost()instead ofthis.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.
| 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, |
There was a problem hiding this comment.
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).
| // 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 | ||
| ); |
There was a problem hiding this comment.
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.
Summary
calculateModelCost()usedthis.modelConfig.provider(persona's default, e.g., candle=$0) instead of the actual routed provider (e.g., deepseek after task routing upgrade)ai/costreportsrequest.provider(post-routing) for cost calculationPartial fix for #341 — cost tracking now accurate. Budget limits and hard caps still TODO.
Test plan
ai/costcommand returns data (667 generations in 1h)🤖 Generated with Claude Code