# Usage and billable metrics

> From a customer activity event to a billable, auditable quantity.

Source: https://app.relvoerp.com/docs/product/en/usage-billing/

A usage charge needs a quantity. That quantity is not entered by hand: it is calculated from the events your system reports. This page covers the three pieces of that chain.

**From activity to quantity:** Event → Billable metric → Filters → Aggregation → Period quantity

## Usage events

A usage event is a timestamped fact: *what happened, when, and for which customer*. It is immutable — Relvo never edits an event once ingested.

### The ingestion contract

Events are sent in batches to `POST /api/v1/public/usage-events`, **1 to 500 events per call**. It requires the `usage_events:write` scope. It takes no `Idempotency-Key` header, because each event carries its own `idempotency_key`.

| Field | Required | Description |
| --- | :---: | --- |
| `event_name` | ✅ | Event name. This is what the metric selects on. |
| `timestamp` | ✅ | When the usage occurred, ISO 8601 in UTC. Determines which period it belongs to. |
| `idempotency_key` | ✅ | Unique key for the event. Resending the same key does not duplicate usage. |
| `customer_id` | — | Internal UUID of the customer. Mutually exclusive with `external_customer_id`. |
| `external_customer_id` | — | The customer's code in your system. Mutually exclusive with `customer_id`. |
| `properties` | — | The event's dimensions and quantities. |

`properties` is a **flat** map—no nested objects—of strings and booleans. Quantities go as decimal strings (`"42"`, `"0.75"`), never as JSON numbers. Up to 100 properties, with keys of at most 128 characters.

> **Key rule: Fractional quantities go as strings**
>
> `"0.75"` as text, not `0.75` as a number. A floating-point decimal loses precision in transit, and in billing that loss ends up as a wrong amount. Relvo rejects the ambiguous format before accepting it, not after invoicing it.

### The response is 202, not 201

Ingestion is asynchronous: Relvo accepts the batch into an idempotent inbox and processes it afterwards. The response tells you exactly what happened to each event:

| Field | Meaning |
| --- | --- |
| `receipts` | One ingestion receipt per new event: `id`, `idempotency_key`, `status` (`pending`, `processing`, `processed`, `failed`, `dead`), `received_at`, `processed_at` and `usage_event_id` |
| `duplicate_idempotency_keys` | Keys that were already in the inbox. They are not re-queued, and this is not an error |
| `validation_failed` | Rejected events, each with its `idempotency_key` and its `errors` |

A receipt can be looked up later with `GET /api/v1/public/usage-event-receipts/{receipt_id}`, or by key with `GET /api/v1/public/usage-event-receipts?idempotency_key=…`. Reusing a key with a different body rejects the whole batch with `409`. If the queue is full, the response is `429` with `Retry-After`.

A batch can have mixed results: 480 queued, 15 duplicates and 5 rejected. Check `validation_failed` on every response — a `202` does not mean all 500 events got in.

**Example: Safe retry after a timeout**

Your process sends 500 events, the connection drops and you don't know whether they arrived. You resend the same batch with the same `idempotency_key` values.

```json
{ "receipts": [], "duplicate_idempotency_keys": ["evt-0001", "…", "evt-0500"], "validation_failed": [] }
```

No usage was duplicated. This is exactly why `idempotency_key` is required rather than optional.

### Origin and status

Each event records where it came from (`api`, `manual` or `csv`) and what status it is in:

| Status | Meaning |
| --- | --- |
| `active` | Counts toward the calculation |
| `voided` | Voided by a correction. No longer counts |
| `superseded` | Replaced by a later event. No longer counts |

Events are listed with `GET /api/v1/public/usage-events`, which accepts the filters `event_name`, `customer_id`, `external_customer_id`, `status`, `query` and `timestamp_from`/`timestamp_to`, and returns `{items, next_cursor}`. Hourly volumes come from `GET /api/v1/public/usage-events/volume`.

### Corrections

An event is not edited: it is corrected with `POST /api/v1/public/usage-events/{usage_event_id}/corrections`, with the `usage_events:correct` scope and an `Idempotency-Key`. There are two possible operations.

| Operation | Effect | When to use it |
| --- | --- | --- |
| `void` | Withdraws the event (it becomes `voided`) | The usage never happened |
| `replace` | Substitutes it with the `replacement_event` (the original becomes `superseded`) | The usage happened, but with different data |

Only an `active` event can be corrected. Correcting any other returns `409`. The response tells you what the consequences are:

- **`rerating_required`** — the affected period must be recalculated. `rerating_status` shows where that recalculation stands (`not_required`, `requested`, `completed` or `failed`).
- **`adjustment_required`** — the period has already been invoiced, so the correction cannot change the issued document and needs a credit or debit note.

> **Caution: Correcting already-invoiced usage does not rewrite the invoice**
>
> An issued invoice is an immutable financial record. If the correction affects a period that has already been invoiced, Relvo flags it with `adjustment_required` and the difference is settled with a [credit or debit note](https://app.relvoerp.com/docs/product/en/credit-debit-notes.md). Never by altering the original document.

## Billable metrics

A billable metric turns a pile of events into a number. It defines three things: **which events it selects**, **which property it measures** and **how it combines them**.

They can be defined in two ways:

| Mode | For whom | What it allows |
| --- | --- | --- |
| **Guided** | Operations and RevOps | Choosing the event, property, aggregation method and filters from the interface |
| **SQL** | Data teams | Writing the query when the logic does not fit guided mode |

Both modes produce an equivalent metric downstream: the charge does not know how it was authored.

### Aggregation methods

Guided mode offers:

| Method | What it calculates | Example |
| --- | --- | --- |
| `count_agg` | Counts events | Number of API calls |
| `sum_agg` | Sums a property | GB transferred |
| `avg_agg` | Average of a property | Mean latency |
| `max_agg` | Maximum for the period | Peak concurrent connections |
| `min_agg` | Minimum for the period | Floor of capacity used |
| `unique_count_agg` | Distinct values | Unique active users |
| `latest_agg` | Last observed value | Storage at period close |

Two additional methods exist at the engine level but cannot be authored from guided mode: `weighted_sum_agg` (a sum weighted by how long each value was in effect) and `custom_agg`.

> **Key rule: `latest_agg` cannot serve as a percentage base**
>
> `latest_agg` returns a point-in-time snapshot, not a series of quantities: there is no transacted value to apply a rate to. Relvo rejects the combination with `percentage` and `graduated_percentage` **when the charge is created**, not at invoicing, so the error shows up while it is still cheap to fix.

### Filters

A filter restricts the metric to a subset of events based on their properties. The same event can feed several metrics filtered in different ways.

**Example: One event, three charges**

Your system emits a single `api_request` event with `properties.region`. With filters, that same stream feeds three charges with different prices:

| Metric | Filter | Charge |
| --- | --- | --- |
| LATAM requests | `region = latam` | USD 0.04 / request |
| US requests | `region = us-east` | USD 0.06 / request |
| Total requests | *(no filter)* | Monthly minimum |

There is no need to change your instrumentation: you change the metric.

## How the period closes

When the period closes, Relvo aggregates the `ACTIVE` events whose `timestamp` falls within the window, applies the metric's filters and hands the quantity to the charge's pricing model. An event that arrives late, with a `timestamp` in an already-invoiced period, does not rewrite that invoice: it triggers the same adjustment path as a correction.

- [Charges and pricing](https://app.relvoerp.com/docs/product/en/pricing.md): What the pricing model does with the quantity the metric produced.
- [Seat-based charges](https://app.relvoerp.com/docs/product/en/seat-based-charges.md): Quantities resolved from seats instead of reported events.
- [Operational integrations](https://app.relvoerp.com/docs/product/en/integrations.md): How to connect your system to report usage.
- [Idempotency](https://app.relvoerp.com/docs/product/guides/idempotency.md): The retry contract across the entire public API.
