# Service metrics

> What the Metrics tab and the metrics API of a service show: requests, errors, latency, queue wait, tokens, then what each replica's engine and GPU report, which numbers Legion measures and which a host reports, and the windows.

Source: https://legion.ambient.xyz/docs/renting/services/metrics

The **Metrics** tab of a service shows how it is being used and how well it serves. After this page you can read each chart and know which numbers to trust for what.

## Where the numbers come from

Every figure has one of three sources, and the tab marks which. They are kept apart on purpose.

| Source | Measured by | Use it for |
| --- | --- | --- |
| **Traffic** | Legion's router, on every request | The truth about what callers got: counts, status codes, latency, queue wait, tokens, data. This is what you can quote and act on. |
| **Engine** | The replica's own model server, as it reported itself | Hints about how the model server is doing: cache use, requests running and waiting, tokens per second. |
| **GPU** | The host's telemetry for the replicas' GPUs | GPU use and memory. |

**Engine and GPU numbers come from the host.** A host can see and change what it reports, so Legion shows them as reported after a range check (values outside their range are dropped) and never uses them for routing, scaling or billing. Treat them as hints. If a source has nothing for the window, the tab says why instead of drawing zeros: `not_reported` (no replica reported), `not_supported` (engine metrics for an `http` service) or `telemetry_unavailable` (no GPU telemetry).

## Windows

Choose **1 hour** (one point a minute), **24 hours** (every 5 minutes), **7 days** (hourly) or **30 days** (every 6 hours). Data is kept 30 days. A step with no traffic is still on the chart, with zero counts and no percentiles. You can look at the whole service or at one replica, picked from the replicas that served in the window.

A replica's figures leave out requests that no replica handled, such as refusals, so replicas add up to less than the service.

## Traffic charts

- **Requests** and **requests per second.** Every request that finished, whatever the outcome.
- **Status codes.** Every answer a caller got, in classes (2xx, 3xx, 4xx, 5xx) and, in the summary, code by code and by who answered: `legion` (the router, with its reason) or `upstream` (the replica).
- **Refused by Legion.** Requests Legion answered itself, by reason: rate limited, starting (no ready replica), busy (queue full), paused, spend cap reached, no credit, unauthorized, and other (for example a request too large or from an address that isn't allowed). A lot of **starting** says callers meet cold starts. A lot of **busy** says you need more replicas.
- **Replica failures.** What the replica did wrong: 4xx and 5xx it answered, and requests it couldn't be reached for (502) or that timed out (504).
- **Latency.** Percentiles (p50, p90, p99), accurate to a fixed bucket:

  - **Time to first token:** from sending the request to the replica until its first response byte. For a stream, the first token.
  - **End to end:** from arriving at the router to the last response byte, queue wait included.
  - **Queue wait:** time spent waiting for a free replica, for every request that reached the queue, or for a replica to be ready when the caller sent `X-Legion-Wait`.
  - **Inter-token gap:** the mean time between stream chunks after the first.
- **Tokens.** Input and output tokens and output tokens per second, summed from the `usage` the replica sends in a response (the last chunk of a stream). Only requests that carried a usage object count, and the tab shows how many did.
- **Data.** Request and response bytes, streamed and non-streamed responses, and callers that left before the answer ended.
- **Load.** The most requests served at once and the most queued at once, as the busiest router pod saw them.

## Engine and GPU charts

- **Engine:** the share of the key-value cache in use, the prefix-cache hit rate, requests running and waiting in the engine, and prompt and generated tokens per second.
- **GPU:** utilization and memory used out of memory available, over the replicas' GPUs.

## Reading them together

- High **queue wait** and **busy** refusals with GPU use near its top: add replicas or lower the target requests per replica ([Scaling](https://legion.ambient.xyz/docs/renting/services/scaling)).
- High latency with low GPU use: look at the engine's waiting requests and its cache use, then at the model's settings.
- **Upstream** failures that match one replica: the host or the replica has a problem, and Legion replaces replicas that fail.

Nothing on this tab contains a request or a response body, a key or a query string.

## With the API and agents

`getServiceMetrics` (`GET /v1/services/{service_id}/metrics`) returns all of the above, with `window` of `1h`, `24h`, `7d` or `30d` and an optional `replica` (a replica's rental id; `404 replica_not_found` when it never served this service). The answer has `points`, a `summary` of the window, and the `engine` and `gpu` sources with their `available` flag. The cost per day is in `getServiceUsage` (`GET .../usage`): see [Billing](https://legion.ambient.xyz/docs/renting/services/billing#see-what-it-used).
