# A service's inference metrics

> GET /v1/services/{service_id}/metrics. Serving metrics of a service, or of one of its replicas with replica, over window: a time series (points) and a summary of the whole window.

Source: https://legion.ambient.xyz/docs/api/services/get-service-metrics

`GET https://api.legion.ambient.xyz/v1/services/{service_id}/metrics`

Authenticate with an API key: `Authorization: Bearer $LEGION_API_KEY`.

Serving metrics of a service, or of one of its replicas with `replica`, over `window`: a time series
(`points`) and a `summary` of the whole window.

Points: `1h` in 1-minute steps, `24h` in 5-minute steps, `7d` hourly, `30d` every 6 hours; `step_seconds`
says which. A step with no traffic is still listed, with zero counts and null percentiles.

Three sources, kept apart:
- `points` and `summary`: observed by Legion's router on every request (authoritative). Status classes
  count every answer a caller got; `refused_*` are answers Legion gave itself (rate limit, starting, busy,
  paused, spend cap, credit, key) and `upstream_*` are failures of the replica. Latency percentiles come
  from fixed histograms (accurate to the bucket). Tokens come from the OpenAI `usage` object of a
  non-streamed response or the final stream chunk, when the replica sends one (`requests_with_usage`). Per-replica figures leave out requests no replica handled (refusals), so they add up to less than the
  service's.
- `engine`: what each replica's engine (vLLM `/metrics`) reports about itself. Host data, shown as the
  replica reported it after range checks; Legion never uses it for routing, scaling or billing.
- `gpu`: GPU utilisation and memory of the replicas' GPUs from the host agent's telemetry.

A source that has nothing for the window says so with `available: false` and a `reason`. Kept 30 days. Never a request or response body, a key or a query string. Needs `services.read`.

## Path parameters

- `service_id`, string (uuid), required

## Query parameters

- `window`, ServiceInferenceWindow
- `replica`, string (uuid): A replica's rental ID; only that replica's metrics. 404 `replica_not_found` when it never served this service.

## Responses

### 200

The metrics.

- `window`, ServiceInferenceWindow, required: `1h` (1-minute steps, the default), `24h` (5-minute), `7d` (hourly), `30d` (6-hourly).
- `step_seconds`, integer, required: 60, 300, 3600 or 21600.
- `from`, string (date-time), required: Start of the first step.
- `to`, string (date-time), required: End of the last step, the one now is in.
- `replica_id`, string (uuid), required: The `replica` asked for; null for the whole service.
- `replicas`, array of ServiceMetricsReplica, required
  - `rental_id`, string (uuid), required
  - `rental_name`, string, required
  - `gpu_model`, string
  - `gpu_count`, integer
  - `current`, boolean, required: Still a replica of the service.
- `summary`, ServiceInferenceSummary, required
- `points`, array of ServiceInferencePoint, required: Oldest first, one per step.
  
- `engine`, ServiceEngineMetrics, required: Replica-reported engine metrics. Untrusted host data, shown as reported; never used for routing, scaling or billing.
  - `source`, string, required
  - `available`, boolean, required
  - `reason`, string: Why `available` is false: `not_reported` (no replica reported in the window) or `not_supported` (protocol `http`).
  - `points`, array of ServiceEnginePoint, required
    - `at`, string (date-time), required
    - `kv_cache_usage_pct`, number (double), required: 0 to 100, the share of the KV cache blocks in use.
    - `prefix_cache_hit_rate_pct`, number (double), required: 0 to 100, hits / queries over the step.
    - `running_requests`, number (double), required
    - `waiting_requests`, number (double), required
    - `prompt_tokens_per_second`, number (double), required
    - `generation_tokens_per_second`, number (double), required
- `gpu`, ServiceGPUMetrics, required: The GPUs of the replicas, from the host agents' telemetry.
  - `available`, boolean, required
  - `reason`, string: Why `available` is false: `not_reported` or `telemetry_unavailable`.
  - `points`, array of ServiceGPUPoint, required
    - `at`, string (date-time), required
    - `utilization_pct`, number (double), required: Mean over the replicas' GPUs, 0 to 100.
    - `memory_used_bytes`, integer (int64), required: Summed over the replicas' GPUs (mean over the step).
    - `memory_total_bytes`, integer (int64), required

Example:

```json
{
  "window": "1h",
  "step_seconds": 0,
  "from": "2026-10-15T12:00:00Z",
  "to": "2026-10-15T12:00:00Z",
  "replica_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
  "replicas": [
    {
      "rental_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "rental_name": "string",
      "gpu_model": "string",
      "gpu_count": 0,
      "current": false
    }
  ],
  "summary": {
    "requests": 0,
    "requests_per_second": 0,
    "status_2xx": 0,
    "status_3xx": 0,
    "status_4xx": 0,
    "status_5xx": 0,
    "refused_rate_limited": 0,
    "refused_starting": 0,
    "refused_busy": 0,
    "refused_paused": 0,
    "refused_spend_cap": 0,
    "refused_no_credit": 0,
    "refused_unauthorized": 0,
    "refused_other": 0,
    "upstream_4xx": 0,
    "upstream_5xx": 0,
    "upstream_unreachable": 0,
    "upstream_timeout": 0,
    "time_to_first_token": {
      "p50_ms": 0,
      "p90_ms": 0,
      "p99_ms": 0,
      "samples": 0
    },
    "end_to_end": {
      "p50_ms": 0,
      "p90_ms": 0,
      "p99_ms": 0,
      "samples": 0
    },
    "queue_wait": {
      "p50_ms": 0,
      "p90_ms": 0,
      "p99_ms": 0,
      "samples": 0
    },
    "inter_token": {
      "p50_ms": 0,
      "p90_ms": 0,
      "p99_ms": 0,
      "samples": 0
    },
    "request_bytes": 0,
    "response_bytes": 0,
    "input_tokens": 0,
    "output_tokens": 0,
    "requests_with_usage": 0,
    "output_tokens_per_second": 0,
    "streamed": 0,
    "non_streamed": 0,
    "client_disconnects": 0,
    "max_in_flight": 0,
    "max_queued": 0,
    "status_codes": [
      {
        "code": 0,
        "count": 0,
        "source": "legion",
        "reason": "string"
      }
    ]
  },
  "points": [
    {
      "requests": 0,
      "requests_per_second": 0,
      "status_2xx": 0,
      "status_3xx": 0,
      "status_4xx": 0,
      "status_5xx": 0,
      "refused_rate_limited": 0,
      "refused_starting": 0,
      "refused_busy": 0,
      "refused_paused": 0,
      "refused_spend_cap": 0,
      "refused_no_credit": 0,
      "refused_unauthorized": 0,
      "refused_other": 0,
      "upstream_4xx": 0,
      "upstream_5xx": 0,
      "upstream_unreachable": 0,
      "upstream_timeout": 0,
      "time_to_first_token": {
        "p50_ms": 0,
        "p90_ms": 0,
        "p99_ms": 0,
        "samples": 0
      },
      "end_to_end": {
        "p50_ms": 0,
        "p90_ms": 0,
        "p99_ms": 0,
        "samples": 0
      },
      "queue_wait": {
        "p50_ms": 0,
        "p90_ms": 0,
        "p99_ms": 0,
        "samples": 0
      },
      "inter_token": {
        "p50_ms": 0,
        "p90_ms": 0,
        "p99_ms": 0,
        "samples": 0
      },
      "request_bytes": 0,
      "response_bytes": 0,
      "input_tokens": 0,
      "output_tokens": 0,
      "requests_with_usage": 0,
      "output_tokens_per_second": 0,
      "streamed": 0,
      "non_streamed": 0,
      "client_disconnects": 0,
      "max_in_flight": 0,
      "max_queued": 0,
      "at": "2026-10-15T12:00:00Z"
    }
  ],
  "engine": {
    "source": "replica_reported",
    "available": false,
    "reason": "string",
    "points": [
      {
        "at": "2026-10-15T12:00:00Z",
        "kv_cache_usage_pct": 0,
        "prefix_cache_hit_rate_pct": 0,
        "running_requests": 0,
        "waiting_requests": 0,
        "prompt_tokens_per_second": 0,
        "generation_tokens_per_second": 0
      }
    ]
  },
  "gpu": {
    "available": false,
    "reason": "string",
    "points": [
      {
        "at": "2026-10-15T12:00:00Z",
        "utilization_pct": 0,
        "memory_used_bytes": 0,
        "memory_total_bytes": 0
      }
    ]
  }
}
```

### 400

The request is malformed or fails validation. An image reference that
is not a container image reference answers `invalid_image_reference`.

### 401

Missing, unknown, expired or revoked credentials.

### 403

The caller lacks the permission this operation requires (`forbidden`),
or signed in without having joined an organization yet
(`no_organization`; `getOnboarding` and the invitation and
organization operations under it work), or could not sign in
(`email_unverified`, `account_conflict`, and `domain_not_allowed`). `not_invited` is no longer returned to
session tokens.

`access_not_approved`: a signed-in person who is not approved
for the beta yet. Every operation that takes a credential answers it to
them, except `getMe` and `joinMyWaitlist`; the problem carries
`access` (`waitlisted` or `not_requested`, and the email). Approved
means a member of an organization, holding a pending invitation, or Legion; an API key always belongs to a member.

`oauth_token_mcp_only`: the caller sent an OAuth access token (`lgo_`), which is accepted
only by `mcpMessage` and the tool calls it dispatches. Every other operation answers it, whatever
the token's scope. Use an API key or the Legion web app.

`org_suspended`: the caller's organization is suspended. Every operation that takes a
credential answers it, by session or API key, except `getMe` (which shows `org.suspended_at` and
`org.suspension_reason`) and the support operations under `/v1/support/`.

`terms_not_accepted`: an approved caller who has not accepted
the current version of the Terms of Use and the Acceptable Use Policy. Every operation that takes a credential answers it, except `getMe`,
`acceptLegalDocuments`, `joinMyWaitlist` and the wind-down operations
(`listRentals`, `stopRental`, `deleteRental`, `listHosts`,
`updateListing` with `listed` false, `removeHost`); the problem
carries `legal`. An API key acts for the person who created it, so it
is refused until that person accepts. Terminal
operations also return
`session_required` (called with an API key) and `step_up_required`
(neither a sign-in nor a step-up challenge within the step-up window).

### 404

The resource does not exist or belongs to another organization.

### 500

An unexpected server error. Safe to retry.

## Errors

- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 404 [`replica_not_found`](https://legion.ambient.xyz/docs/errors#replica_not_found)

## Example request

```sh
curl -s "https://api.legion.ambient.xyz/v1/services/$SERVICE_ID/metrics" \
  -H "Authorization: Bearer $LEGION_API_KEY"
```
