feat: Add token usage tracking, budget limits, and cost estimation #1535
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.
Summary
--track_usageflag to enable token usage tracking without limits--token_limitand--cost_limitCLI options to cap scan resource usagemodel_pricing.yamlRelated Issues
Motivation
Users conducting garak scans against commercial LLM APIs lack visibility into token consumption and have no way to control costs. This creates:
Community feedback (Discord, April/September 2025):
Usage
Output Example
Supported Generators
Token usage tracking is implemented for:
Trade-offs and Limitations
1. Token Counting Accuracy
Cost estimation depends on APIs returning token counts. When APIs don't provide counts, we estimate using ~4 chars/token ratio (marked as "estimated" in summary).
Impact: Cost estimates may be less accurate for providers that don't return token counts.
2. Parallel Execution Overshoot
With
--parallel_attempts, the budget may slightly overshoot the limit since multiple workers dispatch simultaneously. We use batch processing to minimize this.Impact: If you set
--token_limit 1000, actual usage might be ~1100-1200 tokens due to in-flight requests completing.3. Pricing Data Staleness
Model prices in
model_pricing.yamlmay become outdated. Users should verify current rates with providers for production budgeting.Impact: Cost estimates are approximations. The file includes an update timestamp (2025-12) for reference.
Unknown models use conservative defaults ($5/$15 per 1M tokens input/output).
Test Plan