# Service limits and status codes

> The limits that apply to services and every status and code a caller or a client of the service API can get, with what to do about it.

Source: https://legion.ambient.xyz/docs/renting/services/limits-and-errors

Errors from a service come as `application/problem+json` with a stable `code` and a `request_id`. Branch on `code`, not on the text. Quote the `request_id` when you contact [support](https://legion.ambient.xyz/docs/account/support).

## Limits

| Limit | Value |
| --- | --- |
| Autoscaled services per organization | 10 (services on a rental follow rentals) |
| Active keys per service | 10 |
| Replicas per service | 10, and 20 across the organization (ask support to raise them) |
| Services created | 20 an hour for an organization |
| Keys created | 60 an hour for an organization |
| Request body | 10 MiB |
| Request headers | 32 KiB |
| Requests waiting when every replica is busy | 64 per service, for at most 30 seconds |
| `X-Legion-Wait` | At most 300 seconds |
| Request timeout | 600 seconds by default, at most 3600 |
| Silence while streaming | A connection that sends nothing for 60 seconds is closed |
| Failed key checks | 20 a minute from one address, then 5 minutes of `429` |
| Service kinds | `openai` and `http` |

A service's own key can also have a requests-per-minute limit that you set.

## What a caller can get

These come from the Service URL.

| Status | Code | What it means | What to do |
| --- | --- | --- | --- |
| 401 | `unauthorized` | The key is missing, unknown, expired, revoked, or for another service. The answer is the same for all of them. | Check the key and the URL. Create a new key if it expired or was revoked. |
| 402 | `spend_cap_reached` | This month's spend reached the cap. | The owner raises the cap or waits for the next month (UTC). Don't retry in a loop. |
| 402 | `insufficient_credit` | The organization is out of credit. | An owner adds credit. |
| 403 | `service_disabled` | Legion took the service down. | Contact [support](https://legion.ambient.xyz/docs/account/support). |
| 403 | `org_suspended` | The organization is suspended. | Contact [support](https://legion.ambient.xyz/docs/account/support). |
| 404 | `service_not_found` | No service has this name, or the URL is not a service. | Check the URL. |
| 413 | `request_too_large` | The request body is over 10 MiB. | Send less. |
| 429 | `rate_limited` | The key went over its requests-per-minute limit, or too many wrong keys came from one address. `Retry-After` says when. | Wait that long, then retry with a growing delay. |
| 503 | `service_starting` | No replica is ready yet. One is starting. `Retry-After` is the usual start time. | Retry after it, or send `X-Legion-Wait` to be held until one is ready. See [Call a service](https://legion.ambient.xyz/docs/renting/services/call#when-no-replica-is-ready). |
| 503 | `service_busy` | Every replica is at its limit and the queue is full or the wait ended. `Retry-After` is 5 seconds. | Retry with a growing delay. If it is common, raise the maximum replicas. |
| 503 | `service_paused` | The service is paused. | Resume it. |
| 503 | `router_unavailable` | Legion's router can't read its settings right now. | Retry after a moment. |

Requests the router refuses to forward (a path with `..`, an absolute-form target or `TRACE`) are 400, and a path outside `/v1/` on an `openai` service is refused. Anything else comes from your model: its status and body pass through unchanged, and a 3xx is never followed. A request that runs past the request timeout is ended.

When a replica fails before it answers, Legion retries the request once on another replica. It never retries after the first byte of the response.

## What the service API can answer

These come from the Legion API (`/v1/services`), for the app, the CLI, agents and your own code.

| Status | Code | What it means | What to do |
| --- | --- | --- | --- |
| 400 | [`invalid_request`](https://legion.ambient.xyz/docs/errors#invalid_request) | A field is missing or malformed. | Fix the request. |
| 400 | [`invalid_serve`](https://legion.ambient.xyz/docs/errors#invalid_serve) | The serve port, kind or model name is not valid. | Fix it. The kind is `openai` or `http`. |
| 400 | [`service_kind_unsupported`](https://legion.ambient.xyz/docs/errors#service_kind_unsupported) | The kind isn't supported yet, for example WebSocket services. | Use `openai` or `http`. |
| 400 | [`invalid_scaling`](https://legion.ambient.xyz/docs/errors#invalid_scaling) | A scaling setting breaks a rule: the maximum is below the minimum, warm replicas are above the minimum, or a value is out of range. | Fix the values. |
| 400 | [`invalid_hardware`](https://legion.ambient.xyz/docs/errors#invalid_hardware) | The hardware settings are not valid: for example a GPU count the model can't use, or no memory for your own image. | Fix them. |
| 400 | [`image_private`](https://legion.ambient.xyz/docs/errors#image_private), [`registry_sign_in_rejected`](https://legion.ambient.xyz/docs/errors#registry_sign_in_rejected) | Your own image is private, or the registry sign-in for it was refused. | Fix the registry sign-in in the app. |
| 400 | [`invalid_source`](https://legion.ambient.xyz/docs/errors#invalid_source) | An allowed address range isn't valid, or is private or reserved. | Fix the list (up to 16 CIDR ranges). |
| 400 | [`invalid_service_name`](https://legion.ambient.xyz/docs/errors#invalid_service_name) | The name isn't lower case letters, numbers and hyphens, 3 to 40 characters starting with a letter, or it is reserved. | Choose another. |
| 400 | [`invalid_spend_cap`](https://legion.ambient.xyz/docs/errors#invalid_spend_cap) | The cap is missing, zero or not valid. | Set a positive amount. |
| 400 | [`host_visibility_not_acknowledged`](https://legion.ambient.xyz/docs/errors#host_visibility_not_acknowledged) | The first service of an organization needs `acknowledge_host_visibility: true`. | Read [What a host can see](https://legion.ambient.xyz/docs/renting/services/keys-and-security#what-a-host-can-see), then send it. |
| 402 | [`insufficient_credit`](https://legion.ambient.xyz/docs/errors#insufficient_credit) | Creating a service that keeps replicas running needs credit. | An owner adds credit. |
| 403 | [`service_unavailable`](https://legion.ambient.xyz/docs/errors#service_unavailable) | Exposing a port is switched off for your organization, or paused by Legion. | An organization owner can switch it back on in the app, or contact [support](https://legion.ambient.xyz/docs/account/support). |
| 403 | [`custom_images_disabled`](https://legion.ambient.xyz/docs/errors#custom_images_disabled) | Your organization can't use its own images. | Choose a premade model. |
| 403 | [`step_up_required`](https://legion.ambient.xyz/docs/errors#step_up_required) | The change needs a fresh sign-in: a key with no expiry, a new or rotated key in the app, or deleting a service. | Sign in again and retry. |
| 403 | [`api_key_scope`](https://legion.ambient.xyz/docs/errors#api_key_scope) | The key can't use this operation. | Use a key with the right scope. |
| 404 | [`not_found`](https://legion.ambient.xyz/docs/errors#not_found) | The service, key or replica doesn't exist, or belongs to another organization. | Check the id. |
| 409 | [`service_name_taken`](https://legion.ambient.xyz/docs/errors#service_name_taken) | Another service of yours has this name. | To change that service's model, hardware or price ceiling, use its **Settings** tab instead: it keeps its name and URL. Or delete it first: once it is deleted, the name is free. Or choose another name. |
| 409 | [`rule_exists`](https://legion.ambient.xyz/docs/errors#rule_exists) | That port of the rental is already exposed. | Use the service that exists. |
| 409 | [`too_many_rules`](https://legion.ambient.xyz/docs/errors#too_many_rules) | The rental or the organization is at its limit of exposed ports. | Delete one you don't need. |
| 409 | [`rental_not_live`](https://legion.ambient.xyz/docs/errors#rental_not_live) | The rental must be starting or running to expose a port. | Wait for it, or start it. |
| 409 | [`rental_unnamed`](https://legion.ambient.xyz/docs/errors#rental_unnamed) | The rental has no name to build an address from. | Choose a name for the service. |
| 409 | [`host_unsupported`](https://legion.ambient.xyz/docs/errors#host_unsupported) | The rental's host can't expose ports yet. | Rent on another host. |
| 409 | [`too_many_services`](https://legion.ambient.xyz/docs/errors#too_many_services) | The organization has 10 services. | Delete one you don't use. |
| 409 | [`too_many_keys`](https://legion.ambient.xyz/docs/errors#too_many_keys) | The service has 10 active keys. | Revoke one. |
| 409 | [`no_matching_offers`](https://legion.ambient.xyz/docs/errors#no_matching_offers) | No hardware that fits the model is available at your price ceiling right now. | Raise the price ceiling, remove GPU models from your allow-list, or set an [offer alert](https://legion.ambient.xyz/docs/renting/offer-alerts). |
| 409 | [`catalog_entry_incompatible`](https://legion.ambient.xyz/docs/errors#catalog_entry_incompatible) | The GPUs can't run the model. | Choose at least the GPUs the model needs. |
| 409 | [`replica_limit_exceeded`](https://legion.ambient.xyz/docs/errors#replica_limit_exceeded) | The maximum replicas is over your organization's limit (10 per service, 20 in all). | Lower it, or ask support to raise the limit. |
| 409 | [`service_not_autoscaled`](https://legion.ambient.xyz/docs/errors#service_not_autoscaled) | The call needs autoscaling on, for example resume or scaling settings on a service on a rental. | Turn autoscaling on first. |
| 409 | [`invalid_mode_switch`](https://legion.ambient.xyz/docs/errors#invalid_mode_switch) | The switch between one rental and autoscaling isn't possible as asked. | Choose the strategy or the replica to keep, and retry. |
| 409 | [`service_deleting`](https://legion.ambient.xyz/docs/errors#service_deleting) | The service is being deleted. | Nothing to do. |
| 409 | [`service_capped`](https://legion.ambient.xyz/docs/errors#service_capped) | The service is at its monthly cap and can't resume. | Raise the cap first. |
| 409 | [`service_disabled`](https://legion.ambient.xyz/docs/errors#service_disabled) | Legion took the service down. | Contact [support](https://legion.ambient.xyz/docs/account/support). |
| 409 | [`revision_mismatch`](https://legion.ambient.xyz/docs/errors#revision_mismatch) | Someone changed the service after you read it. | Read it again and retry. |
| 409 | [`key_revoked`](https://legion.ambient.xyz/docs/errors#key_revoked) | The key to rotate is already revoked. | Create a new key. |
| 409 | [`rental_managed_by_service`](https://legion.ambient.xyz/docs/errors#rental_managed_by_service) | You tried to stop, start, delete or expose a replica of an autoscaled service. | Change the service's scaling instead. |
| 422 | [`idempotency_key_reused`](https://legion.ambient.xyz/docs/errors#idempotency_key_reused) | The `Idempotency-Key` was used for a different request. | Use a new key for a new action, and the same one only to retry the same request. |
| 429 | [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited) | Too many creates in the last hour. `Retry-After` says when. | Wait. |

Every other code of the Legion API is in [Errors](https://legion.ambient.xyz/docs/errors).
