Skip to content

Wire CostTracker to agent loop #447

@yacosta738

Description

@yacosta738

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 when cost.enabled = true
  • Provider responses pipe TokenUsage into CostTracker::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.enabled flag — 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

  • CostTracker uses in-memory cache with JSONL flush — check_budget() is O(1)
  • CostConfig already has daily_limit_usd, monthly_limit_usd, warn_at_percent, prices
  • This is the foundation — all other cost governance issues (B-F) depend on this

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions