feat(cost): token usage tracking and budget enforcement#78
Closed
feat(cost): token usage tracking and budget enforcement#78
Conversation
- Fix SQL injection risk: replace format!()-interpolated date_expr in summary_since() with a fully parameterized query; add all_summary() method with its own safe prepared statement - Fix bounds panic: replace prices[0]/prices[1] index access on config.model_prices entries with .get(n).copied().unwrap_or(0.0) - Implement check_session_budget(): session_budget_usd was declared in CostConfig but had no corresponding enforcement method; add it with the same warn/block semantics as check_daily_budget() - Remove misplaced delivery_channel_severity test from cost.rs (alerts domain logic has no place in the cost module's test suite) - Fix CLI 'all' scope: previously fell through silently to today_summary(); now routes to all_summary() and prints an actionable error for unknown scopes - Add missing tests: calculate_cost_zero_tokens, cost_tracker_disabled_skips_record, check_daily_budget_warn_does_not_err, check_daily_budget_block_returns_err, check_session_budget_block_returns_err (12 tests total, up from 8) - Restore sonarqube.rs dependency that main.rs declares as a mod Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
src/alerts.rs only exists on the proactive-alerting branch, not here. The stray `mod alerts;` caused a compile error that failed all CI jobs. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The proactive alerting engine only exists on the proactive-alerting branch. Remove the AlertEngine instantiation that was erroneously added to the telegram startup path, which caused a compile error when building with --features telegram. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Not that important for now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds per-session and per-day token cost tracking with configurable budget enforcement.
Changes
src/cost.rs—CostTracker(SQLite-backed),calculate_costwith built-in model pricing (Claude + OpenAI),CostSummarywith by-model and by-ghost breakdownssrc/config.rs—CostConfigwithdaily_budget_usd,session_budget_usd,on_budget_exceeded(warn/block),model_pricesoverride mapsrc/main.rs—sparks cost [today|session:<key>]subcommandconfig.example.toml—[cost]section with all fields documentedBuilt-in model pricing
Prices can be overridden per-model in
config.toml.Type of Change
Pre-PR Checklist
cargo check -qpassescargo test -qpasses (415 tests, 0 failures)🤖 Generated with Claude Code