Skip to content

feat: per-plugin cost trend + threshold warnings (closes #201)#500

Merged
vivekchand merged 1 commit intomainfrom
fix/gh-clawmetry-201-plugin-cost-attr-v2
Apr 2, 2026
Merged

feat: per-plugin cost trend + threshold warnings (closes #201)#500
vivekchand merged 1 commit intomainfrom
fix/gh-clawmetry-201-plugin-cost-attr-v2

Conversation

@vivekchand
Copy link
Copy Markdown
Owner

Closes #201

What

Extends the existing per-plugin cost attribution with the two missing sub-features from GH#201:

  1. Trend over time — detects which plugins are getting more expensive by comparing the last 7 days vs the prior 7 days of cost share
  2. Threshold warnings — alerts when a plugin's cost share exceeds a configurable percentage (default 50%)

How

New: _compute_plugin_trend(plugin, plugin_daily_stats)

Computes trend direction (increasing / decreasing / stable) for a given plugin by comparing its average daily cost-share % in the last 7 days vs the prior 7 days. A >20% relative delta is flagged as increasing or decreasing.

Enhanced: GET /api/usage/by-plugin

Each plugin row now includes a trend field. A warnings array is emitted listing any plugins whose cost share exceeds the threshold (default 50%, configurable via ?threshold=N).

New: GET /api/usage/by-plugin/trend?days=14

Returns a per-day time-series for every plugin in the requested window (1–90 days). Shape:

{
  "days": ["2026-03-20", ...],
  "plugins": {
    "exec": [{"day": "2026-03-20", "tokens": 120, "cost_usd": 0.001, "calls": 3}, ...],
    ...
  }
}

This powers frontend trend charts without any additional backend work.

Data collection

_compute_transcript_analytics now builds a plugin_daily_stats dict (day → plugin → {tokens, cost, calls}) alongside the existing aggregate plugin_stats.

Tests

6 new tests in TestPluginTrend:

  • test_by_plugin_returns_200
  • test_by_plugin_structure — verifies plugins + warnings keys
  • test_by_plugin_row_has_trend — verifies trend is one of valid values
  • test_by_plugin_trend_endpoint_returns_200
  • test_by_plugin_trend_structure
  • test_by_plugin_trend_days_param — verifies ?days=7 returns exactly 7 days

- Track per-day plugin token/cost breakdown in _compute_transcript_analytics
- Add _compute_plugin_trend() helper: compares recent 7d vs prior 7d cost share
  and returns 'increasing' / 'decreasing' / 'stable'
- Enhance GET /api/usage/by-plugin: each row now includes a trend field;
  warnings[] array emitted when plugin cost share exceeds threshold (default 50%)
- New GET /api/usage/by-plugin/trend?days=N: returns daily time-series for all
  plugins in the requested window (default 14 days, max 90) enabling frontend
  charts and 'which plugins are getting more expensive?' analysis
- 6 new tests covering both endpoints and threshold/trend fields
@vivekchand vivekchand merged commit 79d9379 into main Apr 2, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Per-plugin & per-skill cost attribution

1 participant