-
Notifications
You must be signed in to change notification settings - Fork 2
Wire CostTracker to agent loop #447
Copy link
Copy link
Closed
Description
Context
Parent: DALLAY-164 (Cost Governance Productization)
Proposal: openspec/changes/2026-04-06-cost-governance-productization/proposal.md
Problem
CostTracker in src/cost/tracker.rs is fully implemented with tests but never instantiated in production — it's dead code. No provider TokenUsage is piped into it, and no pre-flight budget check gates LLM calls.
Goal
Activate cost tracking and budget enforcement in the agent loop, behind the existing cost.enabled feature flag (defaults to false).
Acceptance Criteria
-
CostTracker::new()is instantiated during runtime bootstrap whencost.enabled = true - Provider responses pipe
TokenUsageintoCostTracker::record_usage()after each LLM call -
CostTracker::check_budget()is called as a pre-flight gate before each LLM call - When budget is exceeded (
BudgetCheck::Exceeded), the LLM call is rejected with a structured error - When budget is at warning threshold (
BudgetCheck::Warning), a warning is emitted via observer - All enforcement is gated behind
cost.enabledflag — zero behavior change when disabled - JSONL storage rotation/pruning strategy is defined (at minimum, configurable retention window)
- Providers that don't return token counts use estimation fallback from
CostConfig.prices
Technical Notes
CostTrackeruses in-memory cache with JSONL flush —check_budget()is O(1)CostConfigalready hasdaily_limit_usd,monthly_limit_usd,warn_at_percent,prices- This is the foundation — all other cost governance issues (B-F) depend on this
Reactions are currently unavailable