Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions docs/CLI_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5674,8 +5674,10 @@ devops ai cost [OPTIONS] COMMAND [ARGS]...

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, markdown. |
Expand All @@ -5693,8 +5695,10 @@ devops ai cost report [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, prometheus. |
Expand All @@ -5715,6 +5719,9 @@ devops ai cost roi [OPTIONS]
| `--hardware-cost`, `-H` | `float` | - | Total hardware purchase cost in USD (e.g. 1599.0 for GPU/workstation). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--by`, `-b` | `string` | - | Grouping period: day, week. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml. |
| `--json` | `boolean` | - | First-class alias for --format json. |

Expand Down Expand Up @@ -5812,8 +5819,10 @@ devops ai spend [OPTIONS] COMMAND [ARGS]...

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, markdown. |
Expand All @@ -5831,8 +5840,10 @@ devops ai spend report [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, prometheus. |
Expand All @@ -5853,6 +5864,9 @@ devops ai spend roi [OPTIONS]
| `--hardware-cost`, `-H` | `float` | - | Total hardware purchase cost in USD (e.g. 1599.0 for GPU/workstation). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--by`, `-b` | `string` | - | Grouping period: day, week. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml. |
| `--json` | `boolean` | - | First-class alias for --format json. |

Expand Down
4 changes: 3 additions & 1 deletion docs/MCP_TOOLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ Generate detailed spend and token report across backend services and servers.

| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| `by` | `string` | No | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `by` | `string` | No | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `days` | `integer` | No | - | Filter usage to the last N days (default: all lifetime). |
| `since` | `string` | No | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `until` | `string` | No | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `reference_model` | `string` | No | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `hardware_cost` | `number` | No | - | Hardware purchase cost in USD to track pay-off against. |
| `format_opt` | `string` | No | `table` | Output format: table, json, yaml, prometheus. |
Expand Down
22 changes: 18 additions & 4 deletions docs/commands/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -2024,8 +2024,10 @@ devops ai cost [OPTIONS] COMMAND [ARGS]...

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, markdown. |
Expand All @@ -2043,8 +2045,10 @@ devops ai cost report [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, prometheus. |
Expand All @@ -2065,6 +2069,9 @@ devops ai cost roi [OPTIONS]
| `--hardware-cost`, `-H` | `float` | - | Total hardware purchase cost in USD (e.g. 1599.0 for GPU/workstation). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--by`, `-b` | `string` | - | Grouping period: day, week. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml. |
| `--json` | `boolean` | - | First-class alias for --format json. |

Expand Down Expand Up @@ -2164,8 +2171,10 @@ devops ai spend [OPTIONS] COMMAND [ARGS]...

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, markdown. |
Expand All @@ -2183,8 +2192,10 @@ devops ai spend report [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, all. |
| `--by`, `-b` | `string` | `server` | Breakdown grouping dimension: server, model, provider, backend, stage, day, week, all. |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--hardware-cost`, `-H` | `float` | - | Hardware purchase cost in USD to track pay-off against. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml, prometheus. |
Expand All @@ -2205,6 +2216,9 @@ devops ai spend roi [OPTIONS]
| `--hardware-cost`, `-H` | `float` | - | Total hardware purchase cost in USD (e.g. 1599.0 for GPU/workstation). |
| `--reference-model`, `-m` | `string` | - | Reference model for counterfactual pricing (default: the ai.reference_model setting). |
| `--days`, `-d` | `integer` | - | Filter usage to the last N days (default: all lifetime). |
| `--since`, `-s` | `string` | - | Filter usage since ISO date or datetime (default: all lifetime). |
| `--until`, `-u` | `string` | - | Filter usage until ISO date or datetime (default: all lifetime). |
| `--by`, `-b` | `string` | - | Grouping period: day, week. |
| `--format`, `-f` | `string` | `table` | Output format: table, json, yaml. |
| `--json` | `boolean` | - | First-class alias for --format json. |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-09
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Design

## Decisions

- **Use half-open intervals `[start, end)` for periods**: Each timestamp belongs to exactly one period. The sum of period columns equals the ungrouped range report.
- **Support `--days N` as short form of `--since`**: Maintains backward compatibility with existing scripts.
- **Normalize dates and datetimes to UTC ISO strings**: Parses ISO date and datetime inputs and sets UTC timezone when no offset is provided.
- **Model period outputs with `PeriodSpendSummary`**: Standardizes fields (`period`, `requests`, `local_tokens`, `actual_spend`, `local_cost_equivalent`, `cloud_spend_avoided`) across CLI JSON, YAML, and tables.
- **Generate FastMCP tool arguments dynamically**: Uses Typer option metadata so `ai_cost_report` automatically exposes `since`, `until`, and `by`.

## Rejected alternatives

- **Closed intervals `[start, end]`**: Boundary records would appear in multiple intervals and invalidate column sum totals.
- **Fixed calendar week boundaries (e.g. Monday-Sunday only)**: Custom arbitrary ranges such as `2026-10-01` to `2026-10-22` would split into partial weeks instead of three equal 7-day periods.
- **Separate CLI commands for grouped reporting**: Adds unnecessary command surface area; extending `--by` aligns with existing grouping parameters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Proposal

## Why

The `devops ai cost report` and `devops ai cost roi` commands accept `--days N` only. A user who needs metrics for a specific date range must copy the ledger database and delete records outside the range. The commands need explicit `--since` and `--until` parameters, and need `--by day` and `--by week` grouping. Closes #2193.

## What Changes

- Add `PeriodSpendSummary` in `src/devops_cli/ai/spend/models.py` to hold spend, token, and savings metrics for a single period.
- Update `SpendLedger` in `src/devops_cli/ai/spend/ledger.py` so queries filter with `since` and `until` timestamps.
- Add `get_period_report` in `SpendLedger` to calculate metrics grouped into day or week intervals.
- Update `devops ai cost report` and `devops ai cost roi` in `src/devops_cli/commands/ai_cost.py` to accept `--since`, `--until`, and `--by`.
- Expose `--since`, `--until`, and `--by` in the generated FastMCP tool `ai_cost_report`.
- Add tests for range filtering, weekly grouping, column sum equivalence, and MCP arguments.
- Regenerate documentation and FastMCP tool snapshots.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `ai-spend-pricing`: Spend report and ROI accept time range and period grouping.

## Impact

- Code: `src/devops_cli/ai/spend/models.py`, `src/devops_cli/ai/spend/ledger.py`, `src/devops_cli/commands/ai_cost.py`
- Documents: `docs/CLI_REFERENCE.md`, `docs/MCP_TOOLS.md`, `docs/commands/ai.md`
- Tests: `tests/ai/spend/test_ai_spend_ledger.py`, `tests/ai/spend/test_ai_spend_cli.py`, `tests/ai/mcp/test_mcp_tool_argv.py`

## Feasibility

- The `ai_spend_records` table indexes `timestamp`.
- The query conditions `WHERE (:since IS NULL OR timestamp >= :since) AND (:until IS NULL OR timestamp < :until)` filter rows in SQLite.
- Half-open intervals `[start, end)` partition the range so the sum of grouped rows equals the ungrouped report.
- The `ai_cost_report` tool generates dynamically from Click parameter metadata.

## Existing Tool Considered

The roadmap check evaluated `ccusage/ccusage`. That tool tracks only Claude and Anthropic usage. It does not read the local SQLite ledger or connect to DevOps CLI workflows. Building this feature in DevOps CLI costs approximately 25,000 tokens and 1 hour.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## ADDED Requirements

### Requirement: Spend report and ROI accept time range and period grouping
The `devops ai cost report` and `devops ai cost roi` commands SHALL accept `--since` and `--until` for ISO dates or datetimes (UTC unless an offset is specified), and SHALL accept `--by day` and `--by week` to return spend, token and savings summaries grouped by period. The `--days N` flag SHALL remain as a short form equivalent to `--since` set to `N` days before the current time.

#### Scenario: Cost report grouped by week across a date range
- **WHEN** `devops ai cost report --since 2026-10-01 --until 2026-10-22 --by week --json` runs against a ledger
- **THEN** it outputs three period rows whose column sums equal the ungrouped report for the same date range

#### Scenario: Cost ROI grouped by week
- **WHEN** `devops ai cost roi --since 2026-10-01 --until 2026-10-22 --by week --json` runs against a ledger
- **THEN** each period row contains requests, local tokens, actual spend, local cost equivalent, and cloud spend avoided

#### Scenario: The MCP tool ai_cost_report accepts date range and grouping
- **WHEN** the MCP tool `ai_cost_report` is called with `since`, `until`, and `by`
- **THEN** it passes the options to the command line and returns the matching report
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Tasks

## 1. Spend ledger date range and period grouping

Reads: `src/devops_cli/ai/spend/models.py`, `src/devops_cli/ai/spend/ledger.py`, `tests/ai/spend/test_ai_spend_ledger.py`.
Changes: `src/devops_cli/ai/spend/models.py`, `src/devops_cli/ai/spend/ledger.py`, `tests/ai/spend/test_ai_spend_ledger.py`.

- [x] 1.1 In `tests/ai/spend/test_ai_spend_ledger.py`, add failing tests asserting `since` and `until` range filtering and `get_period_report` output for day and week grouping
- Verification: `$ uv run devops ci test tests/ai/spend/test_ai_spend_ledger.py`
- [x] 1.2 Add `PeriodSpendSummary` in `src/devops_cli/ai/spend/models.py`, and implement `since`, `until`, and `get_period_report` in `src/devops_cli/ai/spend/ledger.py`
- Verification: `$ uv run devops ci test tests/ai/spend/test_ai_spend_ledger.py`

## 2. CLI commands for cost report and ROI

Reads: `src/devops_cli/commands/ai_cost.py`, `tests/ai/spend/test_ai_spend_cli.py`.
Changes: `src/devops_cli/commands/ai_cost.py`, `tests/ai/spend/test_ai_spend_cli.py`.

- [x] 2.1 In `tests/ai/spend/test_ai_spend_cli.py`, add failing tests for `devops ai cost report` and `devops ai cost roi` with `--since`, `--until`, `--by week`, and `--days`
- Verification: `$ uv run devops ci test tests/ai/spend/test_ai_spend_cli.py`
- [x] 2.2 Update `cost_report`, `cost_roi`, and `cost_default` in `src/devops_cli/commands/ai_cost.py` to accept `--since`, `--until`, and `--by day|week`
- Verification: `$ uv run devops ci test tests/ai/spend/test_ai_spend_cli.py`

## 3. FastMCP tool ai_cost_report arguments and snapshot

Reads: `tests/ai/mcp/test_mcp_tool_argv.py`, `tests/ai/mcp/test_mcp_tool_listing.py`.
Changes: `tests/ai/mcp/test_mcp_tool_argv.py`, `tests/ai/mcp/__snapshots__/test_mcp_tool_listing/test_the_tool_listing_matches_the_snapshot.json`.

- [x] 3.1 In `tests/ai/mcp/test_mcp_tool_argv.py`, add a test verifying `ai_cost_report` passes `since`, `until`, and `by`
- Verification: `$ uv run devops ci test tests/ai/mcp/test_mcp_tool_argv.py`
- [x] 3.2 Update tool listing snapshot in `tests/ai/mcp/test_mcp_tool_listing.py`
- Verification: `$ uv run devops ci test tests/ai/mcp/test_mcp_tool_listing.py`

## 4. Documentation updates

Reads: `docs/MCP_TOOLS.md`, `docs/CLI_REFERENCE.md`, `docs/commands/ai.md`.
Changes: `docs/MCP_TOOLS.md`, `docs/CLI_REFERENCE.md`, `docs/commands/ai.md`.

- [x] 4.1 Run `uv run devops docs generate --sync-readme` to regenerate documentation manuals
- Verification: `$ uv run devops ci docs`

## Criteria

- Criterion 1 (Weekly grouping column sums): group 2
- Criterion 2 (Weekly grouping ROI output): group 2
- Criterion 3 (--days short form equivalence): group 2
- Criterion 4 (FastMCP tool ai_cost_report): group 3

## Workflow follow-up

- Pending a person: review the diff in tuicr (`tuicr -r <base>..HEAD`).
Loading
Loading