feat(provider): add cost controls and fallback #428

Merged
freemo merged 1 commit from feature/m4-provider-costs into master 2026-02-25 17:40:20 +00:00
Owner

Summary

Add token/cost tracking, budget enforcement (per-plan and per-day), provider fallback selection with capability filtering, and cost metadata for plan models. This implements cost controls so that AI spending can be monitored and capped, and provider fallback ensures service continuity when a provider is unavailable or over budget.

Changes

  • New modules:
    • providers/cost_table.py: CostEntry + ProviderCostTable with default pricing for OpenAI, Anthropic, Google, Groq, Together, Cohere, and Mock providers.
    • providers/cost_tracker.py: BudgetStatus enum, BudgetCheckResult, and CostTracker with warn-at-90%/block-at-100% enforcement.
    • providers/fallback_selector.py: FallbackSelector with capability filtering (tool_calls, streaming, vision, json_mode) and budget awareness.
    • domain/models/core/cost_metadata.py: CostMetadata + BudgetExhaustionEvent for plan-level audit trail.
  • Modified:
    • config/settings.py: Added budget_per_plan, budget_per_day, and fallback_providers config keys.
    • domain/models/core/plan.py: Added cost_metadata field and CLI display dict surfacing.
    • providers/__init__.py: Public exports for all new classes.
  • Documentation: Added docs/reference/cost_controls.md with config keys, thresholds, and fallback order.
  • Testing:
    • 71 Behave scenarios in features/cost_controls.feature (all passing).
    • 6 Robot Framework integration tests in robot/cost_controls.robot.
    • ASV benchmarks in benchmarks/cost_controls_bench.py.
  • Vulture whitelist: Updated vulture_whitelist.py with new public API symbols.

Closes #324

## Summary Add token/cost tracking, budget enforcement (per-plan and per-day), provider fallback selection with capability filtering, and cost metadata for plan models. This implements cost controls so that AI spending can be monitored and capped, and provider fallback ensures service continuity when a provider is unavailable or over budget. ### Changes - **New modules:** - `providers/cost_table.py`: `CostEntry` + `ProviderCostTable` with default pricing for OpenAI, Anthropic, Google, Groq, Together, Cohere, and Mock providers. - `providers/cost_tracker.py`: `BudgetStatus` enum, `BudgetCheckResult`, and `CostTracker` with warn-at-90%/block-at-100% enforcement. - `providers/fallback_selector.py`: `FallbackSelector` with capability filtering (tool_calls, streaming, vision, json_mode) and budget awareness. - `domain/models/core/cost_metadata.py`: `CostMetadata` + `BudgetExhaustionEvent` for plan-level audit trail. - **Modified:** - `config/settings.py`: Added `budget_per_plan`, `budget_per_day`, and `fallback_providers` config keys. - `domain/models/core/plan.py`: Added `cost_metadata` field and CLI display dict surfacing. - `providers/__init__.py`: Public exports for all new classes. - **Documentation:** Added `docs/reference/cost_controls.md` with config keys, thresholds, and fallback order. - **Testing:** - 71 Behave scenarios in `features/cost_controls.feature` (all passing). - 6 Robot Framework integration tests in `robot/cost_controls.robot`. - ASV benchmarks in `benchmarks/cost_controls_bench.py`. - **Vulture whitelist:** Updated `vulture_whitelist.py` with new public API symbols. Closes #324
freemo added this to the v3.0.0 milestone 2026-02-25 03:30:01 +00:00
freemo force-pushed feature/m4-provider-costs from 53e3fa90a1
All checks were successful
CI / lint (pull_request) Successful in 25s
CI / security (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 53s
CI / quality (pull_request) Successful in 30s
CI / benchmark-publish (pull_request) Has been skipped
CI / build (pull_request) Successful in 24s
CI / integration_tests (pull_request) Successful in 4m55s
CI / unit_tests (pull_request) Successful in 21m23s
CI / docker (pull_request) Successful in 1m2s
CI / benchmark-regression (pull_request) Successful in 21m40s
CI / coverage (pull_request) Successful in 54m43s
to 4f8b7c92d4
Some checks failed
CI / lint (pull_request) Successful in 21s
CI / benchmark-publish (pull_request) Has been skipped
CI / quality (pull_request) Successful in 21s
CI / security (pull_request) Successful in 40s
CI / build (pull_request) Successful in 30s
CI / typecheck (pull_request) Successful in 58s
CI / integration_tests (pull_request) Successful in 4m18s
CI / unit_tests (pull_request) Successful in 9m27s
CI / docker (pull_request) Successful in 38s
CI / benchmark-regression (pull_request) Has been cancelled
CI / coverage (pull_request) Has been cancelled
2026-02-25 16:39:38 +00:00
Compare
freemo scheduled this pull request to auto merge when all checks succeed 2026-02-25 16:42:20 +00:00
freemo canceled auto merging this pull request when all checks succeed 2026-02-25 16:58:46 +00:00
freemo scheduled this pull request to auto merge when all checks succeed 2026-02-25 17:06:44 +00:00
freemo canceled auto merging this pull request when all checks succeed 2026-02-25 17:07:05 +00:00
freemo force-pushed feature/m4-provider-costs from 803f1a3d2f
Some checks failed
CI / benchmark-publish (pull_request) Has been skipped
CI / lint (pull_request) Successful in 14s
CI / build (pull_request) Successful in 24s
CI / quality (pull_request) Successful in 29s
CI / typecheck (pull_request) Successful in 38s
CI / security (pull_request) Successful in 57s
CI / integration_tests (pull_request) Successful in 2m32s
CI / coverage (pull_request) Has been cancelled
CI / benchmark-regression (pull_request) Has been cancelled
CI / unit_tests (pull_request) Has been cancelled
CI / docker (pull_request) Has been cancelled
to 134dce896a
All checks were successful
CI / benchmark-publish (pull_request) Has been skipped
CI / build (pull_request) Successful in 17s
CI / lint (pull_request) Successful in 18s
CI / quality (pull_request) Successful in 20s
CI / security (pull_request) Successful in 34s
CI / typecheck (pull_request) Successful in 37s
CI / integration_tests (pull_request) Successful in 3m46s
CI / unit_tests (pull_request) Successful in 8m32s
CI / docker (pull_request) Successful in 39s
CI / benchmark-regression (pull_request) Successful in 19m26s
CI / coverage (pull_request) Successful in 31m23s
CI / lint (push) Successful in 13s
CI / build (push) Successful in 15s
CI / quality (push) Successful in 16s
CI / typecheck (push) Successful in 30s
CI / benchmark-regression (push) Has been skipped
CI / security (push) Successful in 33s
CI / integration_tests (push) Successful in 3m44s
CI / unit_tests (push) Successful in 8m53s
CI / docker (push) Successful in 1m1s
CI / benchmark-publish (push) Successful in 10m35s
CI / coverage (push) Successful in 37m55s
2026-02-25 17:08:17 +00:00
Compare
freemo scheduled this pull request to auto merge when all checks succeed 2026-02-25 17:08:58 +00:00
freemo merged commit 134dce896a into master 2026-02-25 17:40:20 +00:00
freemo deleted branch feature/m4-provider-costs 2026-02-25 17:40:21 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
Reference
cleveragents/cleveragents-core!428
No description provided.