# Valuation API

## GET /api/v1/admin/insights/wasted-spend

`GET /api/v1/admin/insights/wasted-spend` · operation `getWastedSpend`

Wasted spend (T186.015, failure_insights, a tenant read): what the range paid for nothing, from the valuation ledger, in three kinds that never count one amount twice: retried_sends (every non-final charge line of a retried or fallen-back request, by provider, model, outcome, reason and status), failed_requests (the final send of a request that ended in an error status, an answer an output guardrail blocked after a billed generation included, by provider, model, problem code or guardrail and status) and failed_evaluations (the final send of a request that answered but failed an evaluation, by evaluation). Amounts are exact decimals of what the ledger knows; unknown counts the items with no known amount (never zero), and state is partial when any is unknown. Each group carries up to five evidence traces. Reads the reader's teams, of each what it recorded in its current organization; PostgreSQL only (the ledger has no analytics-store copy). Requires failure_insights, usage_valuation, span_store, usage_analytics and analytics:read.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `from` | query | string (date-time) | Default: 7 days before to |
| `to` | query | string (date-time) | Default: now; at most 31 days after from |
| `team` | query | string | One team the reader reads (404 otherwise) |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | The wasted spend | `application/json`: object |
| `400` | invalid_request | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | feature_disabled | — |
| `503` | storage_unavailable or query_timeout | — |

Response `200` fields:

- `application/json`: object
  - `from` (string (date-time), required)
  - `to` (string (date-time), required)
  - `total_known_usd` (string, required)
  - `state` (string, required, one of `"known"`, `"partial"`)
  - `items` (integer, required)
  - `unknown` (integer, required)
  - `kinds` (array of object, required)
    - `kind` (string, required, one of `"retried_sends"`, `"failed_requests"`, `"failed_evaluations"`)
    - `items` (integer, required)
    - `known_usd` (string, required)
    - `unknown` (integer, required)
  - `groups` (array of object, required)
    - `kind` (string, required, one of `"retried_sends"`, `"failed_requests"`, `"failed_evaluations"`)
    - `provider` (string)
    - `model` (string)
    - `outcome` (string)
    - `reason` (string)
    - `status` (integer)
    - `items` (integer, required)
    - `known_usd` (string, required)
    - `unknown` (integer, required)
    - `evidence_traces` (array of string, required, max items 5)
  - `truncated` (boolean, required)
  - `read_store` (string, required, one of `"database"`, `"database+analytics_store"`) — The ledger is the database's; with the analytics store as span engine, the failing results it keeps are read there


## GET /api/v1/admin/fx

`GET /api/v1/admin/fx` · operation `displayCurrencyStatus`

Requires fx_display and a console session. The gateway default, the session's user and organization display currencies, the effective display (status usd, ok, stale or missing, with per_usd, rate_date and source when ok) and the latest stored reference rates. Amounts elsewhere stay in USD.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Display currency state; private and not cached. | `application/json`: [FXStatus](/docs/api/schemas/f/#fxstatus) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | Storage unavailable | — |


## PUT /api/v1/admin/fx/preference

`PUT /api/v1/admin/fx/preference` · operation `setDisplayCurrency`

Requires fx_display and CSRF. Sets (or, with an empty currency, clears) the display currency of the session's named user (scope user), or of an organization (scope organization) - its own for an organization admin, any for a global admin (organization_id). Audited as fx.preference.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `scope` (string, required, one of `"user"`, `"organization"`)
  - `currency` (string, required, max length 3)
  - `organization_id` (string, max length 128)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Saved preference and the resulting display. | `application/json`: object |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Not allowed to set that organization's currency | — |
| `404` | Feature disabled or organization not found | — |
| `422` | Unknown currency, or a personal preference on a shared session | — |
| `503` | Storage unavailable | — |

Response `200` fields:

- `application/json`: object
  - `scope` (string)
  - `id` (string)
  - `currency` (string)
  - `display` ([FXDisplay](/docs/api/schemas/f/#fxdisplay))


## GET /api/v1/admin/valuation

`GET /api/v1/admin/valuation` · operation `usageValuation`

Requires usage_valuation and analytics:read. Estimated text-token cost with immutable consulted rates. Only known persisted events contribute to known_amount_usd; partial, unknown, unvalued and lost usage are not zero spend. Timeline and model/team/provider aggregates use the authorized selection before the 50-event page within one repeatable-read snapshot. Not a ledger, invoice or budget. Team scope precedes SQL aggregation/pagination; scoped roles do not receive rate provenance.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `tags` | query | string, max length 2048 | Exact JSON object of at most four ASCII slug string pairs combined with AND before authorized aggregation/pagination. Historical values need not exist in current taxonomy. Single query value, at most 2 KiB, duplicate decoded keys rejected. Requires usage_tags; disabled returns 404 and projections omit stored tags. |
| `end_user` | query | string, min length 1, max length 128 | Exact client-declared end user. Requires end_user_tracking; with the flag off the parameter is rejected. Applied in the shared usage predicate, so reports, comparisons, CSV, valuation and receipts agree. |
| `origin` | query | string, one of `"app"`, `"evaluation"`, `"experiment"`, `"playground"`, `"helper"`, `"shadow"`, `"replay"` | T186.004: only the requests of one origin (how the request entered the gateway). Requires evaluation_tracing; with the flag off, or with another value, the parameter is rejected with 400 invalid_origin. A request log row with no origin (written before the flag, or with it off) is app. Applied in the shared usage predicate, so reports, valuation and the request log agree; day rollups are not used while it is set. |
| `helper_kind` | query | string, one of `"guardrail"`, `"classifier"`, `"summary"`, `"ask_ai"`, `"cache_embedding"`, `"tool_search"`, `"vector_store"`, `"model_test"` | T186.004 S3b: only the helper calls of one sub-kind (ask_ai is m7 ai). Requires origin=helper; anything else is rejected with 400 invalid_helper_kind. A fixed value, applied in the same shared usage predicate as origin. |
| `tag_dimension` | query | string, pattern `^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$` | One exact historical tag name for optional top20 usage/valuation breakdown. Empty group means untagged. No global taxonomy discovery or completeness claim. Requires usage_tags. |
| `from` | query | string (date-time) |  |
| `to` | query | string (date-time) |  |
| `principal` | query | string |  |
| `model` | query | string |  |
| `team` | query | string |  |
| `offset` | query | integer, default `0`, min 0, max 100000 |  |
| `provider` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. Duplicate query values are rejected. |
| `key_id` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. Duplicate query values are rejected. |
| `operation` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. Duplicate query values are rejected. |
| `top_requests` | query | integer, default `10`, min 1, max 50 | cost_request_insights (T186.010 follow-up): how many of the most expensive requests most_expensive lists; 400 invalid_top_requests outside 1-50. Ignored with the flag off. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Persisted valuation coverage and at most 50 recent events | `application/json`: [ValuationReport](/docs/api/schemas/v/#valuationreport) |
| `400` | Invalid request or interval beyond 90 days | — |
| `401` | Authentication required | — |
| `403` | Resource or team forbidden | — |
| `404` | Feature disabled or resource unavailable | — |
| `503` | Storage or operation capacity unavailable | — |


## GET /api/v1/admin/valuation/consistency

`GET /api/v1/admin/valuation/consistency` · operation `chargeConsistency`

Requires usage_valuation, budget_charge_reports and analytics:read (T160.003). Compares, per UTC window (hours through 7 days, days above), the budget charge of each authorized event (settled at the approved rate, reconciled to the provider invoice, released, or still held) with its valuation estimate. Agreeing within charge_accounting.consistency_tolerance_usd counts as consistent; a difference is attributed to reconciliation, margin (discounts or margins) or rate (price book or catalog price not the approved rate); events with only one side known, unlinked events (no reservation, or recorded without single_charge_settlement), archived ledger rows and token-only reservations are counted apart. Team scope precedes every join; same filters as the valuation report; one repeatable-read snapshot; no-store.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `tags` | query | string, max length 2048 | Exact JSON object of at most four ASCII slug string pairs combined with AND before authorized aggregation/pagination. Historical values need not exist in current taxonomy. Single query value, at most 2 KiB, duplicate decoded keys rejected. Requires usage_tags; disabled returns 404 and projections omit stored tags. |
| `end_user` | query | string, min length 1, max length 128 | Exact client-declared end user. Requires end_user_tracking; with the flag off the parameter is rejected. Applied in the shared usage predicate, so reports, comparisons, CSV, valuation and receipts agree. |
| `from` | query | string (date-time) |  |
| `to` | query | string (date-time) |  |
| `team` | query | string |  |
| `principal` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. |
| `model` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. |
| `provider` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. |
| `key_id` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. |
| `operation` | query | string, max length 256 | Exact match; at most 256 UTF-8 bytes. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Totals, windows and the reasons estimates are unknown where the budget charge is known | `application/json`: [ChargeConsistencyReport](/docs/api/schemas/c/#chargeconsistencyreport) |
| `400` | Invalid request or interval beyond 90 days | — |
| `401` | Authentication required | — |
| `403` | Resource or team forbidden | — |
| `404` | Feature disabled or resource unavailable | — |
| `503` | Storage or operation capacity unavailable | — |

