
Cost Counterfactual
FreeAnalyze routing costs against baseline models.
Free · Opens the source repo
What Cost Counterfactual does
Cost Counterfactual is a specialized tool designed for multi-baseline counterfactual cost analysis, primarily aimed at developers and teams managing AI routing costs. This skill allows users to compare actual session spending to hypothetical baselines such as 'always-haiku', 'always-sonnet', and 'always-opus'. By determining whether the routing is yielding savings or incurring unnecessary costs, it helps teams make informed decisions about their routing strategies.
The skill operates by reading session records from the cost-tracking namespace and applying a specified time window to filter the data. It calculates the total tokens used across various models and compares the actual costs against the counterfactual costs derived from the chosen baselines. The output includes savings percentages and total costs for each baseline, enabling teams to quickly assess their routing efficiency.
This tool is particularly useful during quarterly cost reviews, where teams can present data on savings achieved compared to the 'always-sonnet' baseline. It can also serve as a gate in continuous integration processes, ensuring that routing configurations meet specific savings thresholds. Additionally, it aids in validating routing configurations when introducing new parameters, ensuring that savings do not regress.
While the skill provides valuable insights, users should be aware that the counterfactual analysis assumes that the same tokens at the same complexity would yield similar outcomes from the baseline model. This means that the results should be interpreted as an upper bound on potential savings, as variations in session complexity may not be fully captured in the analysis.
When to use it
Use this skill during cost reviews or when validating routing configurations to ensure savings are being achieved.
When not to use it
Avoid using this skill for real-time cost tracking or scenarios where immediate cost feedback is required.
What you can build with it
Quarterly Cost Review
Use the skill to present data on savings achieved compared to the 'always-sonnet' baseline during quarterly financial reviews.
CI Gate Implementation
Integrate the skill into CI pipelines to ensure that routing configurations maintain a minimum savings threshold.
Routing Configuration Validation
Run the analysis when adjusting routing parameters to confirm that new settings do not lead to increased costs.
How to install Cost Counterfactual
View source1. Install with the skills CLI
npx skills add ruvnet/ruflo/cost-counterfactual --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by ruvnetMulti-baseline counterfactual cost analysis. Pairs with the existing observability surface:
cost-budget-check— "have we crossed a threshold?" (reactive)cost-projection— "when will we cross a threshold?" (predictive)cost-counterfactual— "is the routing earning its keep?" (comparative) ← this one
Algorithm
- Read all
session-*records from thecost-trackingnamespace. - Apply
--sincewindow filter (default all-time). - Sum tokens across
byModel[*]entries for each session. - For each requested baseline (default: all three):
counterfactualUsd = (input × tier.input + output × tier.output + cache_write × tier.cache_write + cache_read × tier.cache_read) / 1M
- Compute
savings = counterfactualUsd − actualUsd. - Emit per-baseline totals + savings % across the comparison set.
Smoke transcript (2 sessions: 50K haiku tokens + 50K sonnet tokens)
| Sessions considered | 2 |
| Total input tokens | 100,000 |
| Actual spend | $0.162500 |
| Baseline | Hypothetical | Actual | Savings | % |
| `always-haiku` | $0.025000 | $0.162500 | -$0.137500 | -550.00% |
| `always-sonnet` | $0.300000 | $0.162500 | +$0.137500 | 45.83% |
| `always-opus` | $1.500000 | $0.162500 | +$1.337500 | 89.17% |
How to read negative savings
A negative always-haiku result means the router chose more-expensive models than haiku on tasks haiku could have handled. That's an over-escalation signal:
- Maybe qualityBar is set too high
- Maybe the sonnet/opus session was warranted by complexity but the baseline doesn't know that
- Run
cost optimize(or inspect specific sessions viacost conversation) to investigate
Positive savings quantify the router's win against that baseline. The most informative number is usually always-sonnet — it's the standard "safe default" baseline most teams would pick if they didn't have routing.
When to use
- Quarterly cost review: "We saved $X vs always-Sonnet — here's the proof."
- CI gate:
cost counterfactual --format json | jq '.baselines[1].savingsPct > 30'— fail builds if routing isn't saving ≥30% vs sonnet baseline (workload-shift detector). - Routing-config validation: When introducing a new qualityBar or cost-ceiling, re-run counterfactual to confirm savings didn't regress.
Stationarity caveat
Like all counterfactual analyses, this assumes the same tokens at the same complexity would have produced the same outcome from the baseline model. That's an upper bound — the baseline might have failed and required retries, which the math doesn't capture. Treat the numbers as a quality-blind ceiling.
Frequently asked questions about Cost Counterfactual
Similar skills
Arize Evaluator
Streamline LLM evaluation workflows on Arize.
Troubleshoot
Analyze logs to understand chat agent behavior.
Agentic Evaluation
Enhance AI outputs through iterative evaluation and refinement.
RAG Evaluation
Evaluate retrieval-augmented generation benchmarks efficiently.
NV-Reason-CXR
Run smoke tests for chest X-ray reasoning models.
Clinical ASR Evaluation
Score and evaluate clinical ASR manifests effectively.
