New to Claude Skills? Learn how to install them →

ruvnet on GitHub

Cost Anomaly Detection

Free

Identify individual spending outliers in session data.

by ruvnet67.6k stars on ruvnet/ruflo
1 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What Cost Anomaly Detection does

Cost Anomaly Detection is a specialized tool designed to pinpoint anomalous spending in session data using a robust statistical approach. Unlike traditional methods that rely on mean and standard deviation, this skill employs Median Absolute Deviation (MAD) for outlier detection, making it resilient to the very anomalies it aims to identify. This ensures that even in datasets with extreme values, the detection remains accurate and reliable. The skill calculates modified z-scores for each session, allowing users to determine which sessions deviate significantly from typical spending patterns.

The skill operates by reading session records from the cost-tracking namespace and filtering them based on a specified time window. By calculating the median total cost and MAD, it flags sessions with modified z-scores exceeding a defined threshold, typically set at 3.5. This approach is particularly useful for teams looking to maintain budgetary control and identify rogue spending that could impact financial forecasts. The ability to integrate with CI pipelines through alert exit codes further enhances its utility, allowing for automated checks on spending anomalies as part of the development workflow.

Cost Anomaly Detection is particularly beneficial for developers and financial analysts who need to monitor and analyze cost behaviors in real-time. By providing insights into individual session outliers, it complements aggregate trend analysis tools like cost-burn, enabling a comprehensive understanding of spending patterns. This dual approach helps teams not only assess whether overall spending is accelerating but also to investigate specific sessions that may warrant further scrutiny.

In summary, this skill is an essential addition for any team focused on financial oversight and session tracking, providing a clear mechanism to detect and address unusual spending behaviors efficiently.

When to use it

Use this tool when you need to detect specific outlier sessions in your cost data, especially when monitoring for unexpected spending spikes.

When not to use it

This skill is not suitable for general cost analysis or aggregate trend monitoring; it focuses solely on identifying individual session anomalies.

What you can build with it

Monitoring Weekly Spending

Integrate the skill into your CI pipeline to automatically check for outlier sessions in the past week.

Investigating Cost Spikes

Use this tool to identify specific sessions that caused a sudden increase in spending, allowing for targeted analysis.

Budget Compliance Checks

Regularly run this skill to ensure that no individual session exceeds expected spending thresholds, maintaining budget compliance.

How to install Cost Anomaly Detection

View source

1. Install with the skills CLI

npx skills add ruvnet/ruflo/cost-anomaly --agent claude-code

2. 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 ruvnet

Per-session outlier detection — the diagnostic counterpart to cost-burn's aggregate-trend signal.

QuestionSkill
"Is the AGGREGATE rate accelerating?"cost-burn
"Which SPECIFIC sessions are anomalous outliers?"cost-anomaly ← this
"Could we have spent less in aggregate?"cost-counterfactual
"When will we hit budget?"cost-projection

Algorithm

Implementation: scripts/anomaly.mjs.

  1. Read all session-* records from cost-tracking namespace.
  2. Filter to --since window (default: all-time).
  3. Compute median(total_cost_usd) and MAD = median(|x - median|).
  4. Per-session modified z-score (Iglewicz-Hoaglin 1993): z = 0.6745 * (x - median) / MAD
  5. Flag sessions with |z| > --threshold (default 3.5).

Why MAD and not mean + sigma?

ApproachWhat breaks
mean + sigmaA single $50 session inflates BOTH mean and sigma so badly that subsequent outliers hide inside the new "normal" band. Catastrophic on small samples.
median + MADBoth estimators ignore up to 50% of the data — the outliers themselves can't shift them. Robust on n=10. The canonical cutoff |z| > 3.5 is from Iglewicz-Hoaglin (1993).

Smoke transcript (5 baseline sessions $0.08-$0.12 + 1 outlier $5.00)

| Sessions considered | 5 |
| Threshold (|modified z|) | 3.5 |
| Median spend | $0.100000 |
| MAD | $0.010000 |
| Min / Max | $0.080000 / $5.000000 |
| **Outliers found** | **1** |

## Outlier sessions
| Session | Spend | Deviation | Modified z | Direction |
| outlier- | $5.000000 | +$4.900000 | 330.505 | high |

Exit codes

$ cost anomaly --alert-on-outliers 1
⚠ ALERT: found 1 outlier session(s) (|modified z| > 3.5); threshold was ≥1
exit 1

$ cost anomaly --alert-on-outliers 5
✓ found 1 outlier session(s); under threshold ≥5 — OK
exit 0

CI integration

# Fail the build if any session this week is a >3.5σ outlier
cost anomaly --since 7d --alert-on-outliers 1 || investigate-bad-session

Most useful when paired with cost-burn:

cost burn  --alert-on-acceleration-pct 50  || page-oncall   # rate-of-change alert
cost anomaly --alert-on-outliers 1         || investigate   # point-anomaly alert

Together they cover "is the average shifting?" AND "is there a single rogue session?" — both can fire independently.

Edge cases

  • n < 3: emit "Insufficient data" message, exit 0. MAD on 1-2 samples is meaningless.
  • MAD = 0: ≥50% of sessions share the exact same spend, so z-scores collapse. Emit explainer instead of dividing by zero. Common cause: dry-run sessions all at $0.
  • Low-direction outliers: usually crashed or dropped sessions, not over-spending. The output table explicitly labels direction so operators interpret correctly.
  • Very small MAD: even tiny absolute deviations produce huge z-scores. The $5 outlier with MAD=$0.01 yields z=330 — that's correct, not a bug.

Direction column

DirectionLikely causeAction
highLong session, stuck in expensive tier, or runaway loopcost report + cost conversation to investigate
lowCrash, dropped session, or unfinished workVerify the session completed normally

Frequently asked questions about Cost Anomaly Detection

Similar skills