Commit 1bf9cee
authored
feat: Add cost tracking for API usage (#324)
Adds comprehensive cost tracking functionality to the CLI. Includes:
- Real-time cost calculation based on token usage
- Default pricing for 30+ models across all major providers
- New /cost command to view session breakdowns
- Status bar integration showing current session cost
- Configurable pricing with support for custom models
This helps users monitor and manage their API spending directly within
the CLI.
## Key Changes
### 1. **New Pricing Configuration** (`config/pricing.go`)
- Default pricing for 30+ models across Anthropic, OpenAI, Google,
DeepSeek, Groq, Mistral, etc.
- Configurable via `config.yaml` with support for custom models
- Regular updates to match current provider rates
### 2. **Cost Tracking Service**
(`internal/services/pricing_service.go`)
- Real-time cost calculation based on token usage (input/output)
- Session-level and conversation-level cost aggregation
- Integration with existing conversation and state management
### 3. **New `/cost` Command**
- Displays detailed cost breakdown by model and session
- Shows total cost and per-model usage statistics
- Accessible via chat interface
### 4. **Status Bar Integration**
- Real-time cost display in the input status bar
- Shows current session cost as users interact with models
- Configurable display options
### 5. **Database Schema Updates**
- Extended storage to persist cost statistics
- Backward compatible migration
- Historical cost tracking for conversations
### 6. **Configuration Options**
- Enable/disable cost tracking via `cost_tracking.enabled`
- Custom pricing configuration for specific models
- Default pricing that's regularly updated
## Technical Details
- **Files Changed**: 34 files, 1099 insertions(+), 125 deletions(-)
- **New Files**:
- `config/pricing.go` - Default pricing configuration
- `internal/domain/pricing.go` - Pricing domain models
- `internal/services/pricing_service.go` - Core cost calculation service
- **Integration**: Works with all existing providers and models
- **Backward Compatibility**: Existing conversations show $0.00 cost
until new usage
- **Testing**: Includes comprehensive unit tests for cost calculation
## Usage
1. **Enable/Disable**: Configure via `cost_tracking.enabled` in config
2. **View Costs**: Use `/cost` command in chat interface
3. **Custom Pricing**: Add custom model pricing in config
4. **Real-time Display**: See costs in status bar during conversations
This feature provides valuable visibility into API spending, helping
users manage costs effectively while using the CLI.
Closes #80
---------
Signed-off-by: Eden Reich <eden.reich@gmail.com>1 parent fd9b053 commit 1bf9cee
File tree
37 files changed
+1300
-154
lines changed- .infer
- cmd
- config
- internal
- app
- container
- domain
- handlers
- infra
- adapters
- storage
- services
- shortcuts
- ui/components
- tests/mocks/domain
37 files changed
+1300
-154
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
| |||
529 | 533 | | |
530 | 534 | | |
531 | 535 | | |
| 536 | + | |
532 | 537 | | |
533 | 538 | | |
534 | 539 | | |
| |||
565 | 570 | | |
566 | 571 | | |
567 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
568 | 577 | | |
569 | 578 | | |
570 | 579 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
379 | 381 | | |
380 | 382 | | |
381 | 383 | | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
382 | 468 | | |
383 | 469 | | |
384 | 470 | | |
| |||
449 | 535 | | |
450 | 536 | | |
451 | 537 | | |
| 538 | + | |
452 | 539 | | |
453 | 540 | | |
454 | 541 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| |||
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
| 101 | + | |
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
359 | 360 | | |
360 | 361 | | |
361 | 362 | | |
| 363 | + | |
362 | 364 | | |
363 | 365 | | |
364 | 366 | | |
| |||
486 | 488 | | |
487 | 489 | | |
488 | 490 | | |
| 491 | + | |
489 | 492 | | |
490 | 493 | | |
491 | 494 | | |
| |||
510 | 513 | | |
511 | 514 | | |
512 | 515 | | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
513 | 520 | | |
514 | 521 | | |
515 | 522 | | |
| |||
851 | 858 | | |
852 | 859 | | |
853 | 860 | | |
854 | | - | |
| 861 | + | |
| 862 | + | |
855 | 863 | | |
856 | 864 | | |
857 | 865 | | |
| |||
0 commit comments