Files
ruflo/plugins/ruflo-cost-tracker
Reuven 5addd83b43 perf(plugins): ADR-098 Part 2 (slice 4/4) — token diet for ruflo-cost-tracker/cost-analyst
Closes Part 2 of ADR-098. Final and largest token diet — moves
the model pricing table, cost attribution formula, budget alert
ladder, optimization strategy catalog, and cost-report shape out
of the agent prompt into a sibling REFERENCE.md.

Before: 105 lines per spawn (~1260 tokens).
After:  41 lines per spawn (~490 tokens). Net savings ~770 tokens
        per cost-analyst spawn — biggest single-agent win of Part 2.

Reference content lives at plugins/ruflo-cost-tracker/REFERENCE.md
(63 lines):
  - Pricing table per 1M tokens for Haiku/Sonnet/Opus across
    Input/Output/Cache-Write/Cache-Read
  - Cost attribution formula
  - 4-tier budget alert ladder
  - Optimization strategies with savings ranges + quality impact
  - Standard markdown cost-report shape
  - Federation cost integration note (ADR-097 Phase 3 lookup)

The agent prompt keeps:
  - 5-row responsibilities block
  - 5 AgentDB MCP tool entries
  - Memory + Neural Learning tails (Part 4 invariant)
  - Related plugins list (added ruflo-federation per Part 1 sync)

Plugin version: 0.2.0 → 0.2.1 (patch — token diet only). The 0.2.0
bump came from Part 1 slice 3 (federation budget pairing).

ADR-098 Part 2 final progress (all 4 slices):
  - device-coordinator: 30 ≤ 60 ✓ (was 69, slice 1)
  - domain-modeler:     54 ≤ 60 ✓ (was 93, slice 2)
  - adr-architect:      45 ≤ 60 ✓ (was 96, slice 3)
  - cost-analyst:       41 ≤ 60 ✓ (was 105, slice 4)

Net cumulative savings across 4 agents: ~2400 tokens per spawn.
Multiplied by spawn frequency, this is the largest measurable
optimization landing in the 3.6.x cycle.

Remaining ADR-098 work: Part 5 (worker dispatch — ≥ 8 work-producing
agents fire the right background worker on completion).
2026-05-03 22:33:03 -04:00
..

ruflo-cost-tracker

Token usage tracking, model cost attribution per agent, budget alerts, and optimization recommendations.

Overview

Tracks token usage per agent, task, and model, then computes USD cost attribution using current model pricing. Monitors configurable budgets with tiered alerts (info at 50%, warning at 75%, critical at 90%, hard stop at 100%). Analyzes usage patterns and recommends optimizations such as model downgrades, prompt caching, and batch operations.

Installation

claude --plugin-dir plugins/ruflo-cost-tracker

Agents

Agent Model Role
cost-analyst haiku Token usage tracking, USD cost attribution, budget monitoring, optimization recommendations

Skills

Skill Usage Description
cost-report /cost-report [--period today] Generate a cost report with token usage and USD costs by agent and model
cost-optimize /cost-optimize Analyze usage patterns and recommend cost optimizations with estimated savings

Commands (5 subcommands)

cost report [--period today|week|month]  # Generate cost report for a period
cost breakdown [--by agent|model|task]   # Detailed breakdown by dimension
cost budget set <amount>                 # Set budget limit in USD
cost optimize                            # Analyze usage and suggest savings
cost history                             # Show cost tracking over time

Model Pricing (per 1M tokens)

Model Input Output Cache Write Cache Read
Haiku $0.25 $1.25 $0.30 $0.03
Sonnet $3.00 $15.00 $3.75 $0.30
Opus $15.00 $75.00 $18.75 $1.50

Budget Alert Thresholds

Level Threshold Action
Info 50% consumed Log notification
Warning 75% consumed Display warning, suggest optimizations
Critical 90% consumed Urgent alert, recommend model downgrades
Hard Stop 100% consumed Halt non-essential agent spawns

Optimization Strategies

Strategy Savings Impact
Downgrade simple tasks to Haiku 80-92% Minimal for low-complexity work
Enable prompt caching 90% on reads None (same quality)
Batch similar operations 15-25% Slight latency increase
Use Agent Booster (Tier 1) 100% Only for simple transforms
Shorten system prompts 10-20% Requires careful pruning

Federation budget circuit breaker pairing (ruflo 3.6.25+)

This plugin pairs naturally with the federation budget envelope shipped in ADR-097. The federation_send MCP tool now accepts caller-supplied caps that this plugin's tracking should respect:

Field Default Effect
maxHops 8 Hard ceiling on recursive delegation across federated peers — defangs cost cascades from runaway sub-swarms.
maxTokens unbounded Σ tokens across the whole hop chain. Returns BUDGET_EXCEEDED (constant string, no oracle leak) on overshoot.
maxUsd unbounded Σ USD across hops. Same enforcement.
hopCount 0 Pass-through for re-forwarded messages.
spent.{tokens,usd} 0 Caller-reported usage from previous legs. Negatives clamped to 0.

Phase 1 of ADR-097 enforces at the send side. Two follow-up phases will tighten the integration:

  • Phase 2 (deferred) — peer state machine ACTIVE / SUSPENDED / EVICTED driven by trailing 24h cost (default suspension threshold $5) + 1h failure ratio (>50% over ≥10 sends). Auto-recovery after 30 min cooldown.
  • Phase 3 (deferred) — federation_spend event bus. Each federation_send completion publishes {peerId, taskId, tokensUsed, usdSpent, ts}. This plugin's cost-tracker should aggregate per-peer rolling windows (1h / 24h / 7d) and expose them via the existing cost-report skill. Breaker queries the aggregate to evaluate suspension thresholds.

Until Phase 3 ships, federated spend is not counted in the host's cost-tracker — only local agent spend. Treat cost-report numbers as a lower bound when federation is in use.

  • ruflo-observability -- Token usage metrics collected via observability instrumentation
  • ruflo-neural-trader -- PnL tracking and cost-adjusted return calculation
  • ruflo-federation -- Budget circuit breaker on outbound federation_send (ADR-097)

License

MIT