# Estimate what a service would cost

> POST /v1/services/estimate. What a service with this hardware and scaling would cost per hour at current prices, from the cheapest host that fits now: idle, at minimum and at…

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

`POST https://api.legion.ambient.xyz/v1/services/estimate`

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

What a service with this hardware and scaling would cost per hour at current prices, from
the cheapest host that fits now: idle, at minimum and at maximum replicas, how many hours a month the
cap pays for at maximum, and a suggested cap (8 hours a day at maximum for 30 days). `hosts_available`
is how many hosts fit; 0 means a create would be 409 `no_matching_offers`. Nothing is reserved. Needs `services.read`.

## Request body

- `target`, ServiceTargetInput: Mode `rental`: `rental_id` and `port` (for a model the rental serves, its serve port). Mode `autoscaled`: `catalog_id` or (while custom images are on) `image` with `serve`, then optional `hardware` constraints and `scaling`. A private image is read with the organization's registry sign-in exactly as `createRental` does.
  - `mode`, ServiceMode, required: How a service runs. `rental` ("On a rental"): a port on one of your rentals, one replica, your machine. `autoscaled` ("Autoscaled"): Legion rents GPUs and runs 0 to N replicas with traffic (serverless: scales to zero when idle). Whether a service serves a model is said by `target.template` and `catalog_id`, not by the mode.
  - `rental_id`, string (uuid)
  - `port`, integer
  - `catalog_id`, string
  - `image`, string
  - `registry_credential_id`, string (uuid): . With `image`: the registry credential to read it with instead of the one that matches by host and path prefix. It must be the organization's and for the image's registry (422 `registry_credential_not_found`, `registry_credential_host_mismatch`). Not with `catalog_id` (400 `invalid_serve`).
  - `serve`, ServiceServeInput
    - `port`, integer, required
    - `model`, string: The served model name; required with protocol openai.
  - `health_path`, string: Probed by the router. Default /v1/models for openai, / for http.
  - `hardware`, ServiceHardware: Cheapest that fits (decisions 40 to 46): for each replica the autoscaler evaluates every eligible offer and every GPU count the model can split across (powers of two up to 8, at least the catalog entry's min_gpus), keeps the configurations the catalog fit check accepts and rents the cheapest total price per hour; ties go to the host with the higher Legion score. Every field is an optional constraint. Replicas may differ in hardware; a replacement re-runs the choice.
    - `strategy`, string
    - `max_price_per_replica_hour`, Money: The most one replica may cost an hour, all GPUs together, all-in. No replica starts above it; for interruptible it is also the bid, per GPU-hour each GPU's share (rounded down). Changed by updateService, it applies in place: no replica rolls, the next placement uses it, and each interruptible replica's bid moves to its share of the new ceiling at once, within the rental bid rules (at least the host's interruptible floor, at most its on-demand price, not under a preemption notice, a few changes an hour); a bid those rules refuse keeps its old value.
    - `gpu_models`, array of string: Only these GPU models (unordered: price decides). Absent: any.
    - `tier`, RentalTier: The kind of rental. `on_demand`: the host's `price_per_gpu_hour`; never stopped for another rental. `interruptible`: a bid on the host's spot auction, above the host's floor `interruptible_price_per_gpu_hour`, on GPUs no on-demand rental needs: an on-demand rental on the same host that needs its GPUs stops it, with the stop of the container (after a 5-minute notice, `preempt_at`, on hosts whose agent supports it; the notice is also written to `/workspace/.legion/preemption.json`), and it ends `stopped` with `stop_reason` `preempted` and its workspace kept (billed as storage, and started again by itself with `auto_resume`), or deleted when it chose `interruption_behavior` `delete`. The on-demand price is fixed for the rental. An interruptible rental is a bid on the host's spot auction: it pays the clearing price, which moves every 2 minutes between the host's floor and the rental's `max_price_per_gpu_hour`, and a higher bid can also stop it (`stop_reason` `outbid`) with the same notice.
    - `placement`, ServicePlacement: Where replicas run. `eligible_hosts` (the default), any host passing the service eligibility floor; `own_hosts`, only the organization's own hosts. A host can see the traffic it serves.
    - `min_gpu_memory_gb`, number: Own images: the GPU memory one replica needs, all GPUs together.
    - `gpu_counts`, array of integer: Own images: the GPU counts the model can run on (tensor parallel). Default 1, 2, 4, 8.
  - `scaling`, ServiceScalingInput: Omitted fields keep their default (`getServiceDefaults`) on create and their value on update. New fields may be added.
    - `min_replicas`, integer
    - `max_replicas`, integer: Legion never runs more, a rolling change's surge replica included.
    - `scale_to_zero`, boolean: With min_replicas 0: creating, resuming or changing the template boots one replica; once it is ready and idle_timeout_seconds pass without a request, it scales to zero.
    - `idle_timeout_seconds`, integer
    - `scale_down_delay_seconds`, integer: Load must stay below capacity this long before a replica is removed.
    - `warm_replicas`, integer: Replicas kept stopped (workspace kept, storage billed) after scale-down, restarted first.
    - `target_concurrency`, integer: Requests in flight per replica above which a replica is added.
    - `max_concurrency`, integer: Requests in flight per replica at most; more wait in the queue.
    - `queue_wait_threshold_ms`, integer: A replica is added when requests wait longer than this.
    - `request_timeout_seconds`, integer: The longest a request may take, streaming included.
  - `env`, array of EnvVarInput: . mode `autoscaled`: environment variables of every replica. See `EnvVarInput`.
    - `name`, string, required
    - `value`, string: Required except on an update that keeps a stored secret (see above).
    - `secret`, boolean
  - `model_settings`, ModelSettings: . mode `autoscaled` with `catalog_id`: model settings of every replica; they count toward the hardware each replica needs and the estimate.
  - `command`, CommandOverride: . mode `autoscaled` with `image`: the entrypoint override of every replica.
  - `args`, CommandOverride: . mode `autoscaled` with `image`: the CMD override of every replica.
- `monthly_spend_cap`, Money
  - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
  - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.

Example:

```json
{
  "target": {
    "mode": "rental",
    "rental_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "port": 1,
    "catalog_id": "string",
    "image": "string",
    "registry_credential_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "serve": {
      "port": 1024,
      "model": "string"
    },
    "health_path": "string",
    "hardware": {
      "strategy": "cheapest_fit",
      "max_price_per_replica_hour": {
        "amount_minor": 250,
        "currency": "USD"
      },
      "gpu_models": [
        "string"
      ],
      "tier": "on_demand",
      "placement": "own_hosts",
      "min_gpu_memory_gb": 1,
      "gpu_counts": [
        1
      ]
    },
    "scaling": {
      "min_replicas": 0,
      "max_replicas": 1,
      "scale_to_zero": false,
      "idle_timeout_seconds": 60,
      "scale_down_delay_seconds": 60,
      "warm_replicas": 0,
      "target_concurrency": 1,
      "max_concurrency": 1,
      "queue_wait_threshold_ms": 100,
      "request_timeout_seconds": 10
    },
    "env": [
      {
        "name": "HF_TOKEN",
        "value": "string",
        "secret": false
      }
    ],
    "model_settings": {},
    "command": [
      "string"
    ],
    "args": [
      "string"
    ]
  },
  "monthly_spend_cap": {
    "amount_minor": 250,
    "currency": "USD"
  }
}
```

## Responses

### 200

The estimate.

- `hosts_available`, integer, required: Hosts with at least one configuration that fits now; 0 means a create is 409 no_matching_offers.
- `likely_hardware`, object, required: The cheapest configuration that fits now, what the next replica would rent.
  - `gpu_model`, string, required
  - `gpu_count`, integer, required
  - `price_per_hour`, Money, required
    - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
    - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `price_range_per_replica_hour`, object, required
  - `min`, Money, required
    - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
    - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
  - `max`, Money, required
    - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
    - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `idle_per_hour`, Money, required
  - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
  - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `at_min_per_hour`, Money, required
  - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
  - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `at_max_per_hour`, Money, required
  - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
  - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `suggested_monthly_spend_cap`, Money, required
  - `amount_minor`, integer (int64), required: Amount in minor units (cents for USD).
  - `currency`, MoneyCurrency, required: ISO 4217 currency code. Only USD is supported.
- `cap_hours_at_max`, integer, required: Hours a month the cap pays for at maximum; null without a cap or a price.
- `cold_start_seconds`, integer, required: The median time replicas of this catalog entry took to become ready on Legion over the last 30 days; null without data.

Example:

```json
{
  "hosts_available": 0,
  "likely_hardware": {
    "gpu_model": "string",
    "gpu_count": 0,
    "price_per_hour": {
      "amount_minor": 250,
      "currency": "USD"
    }
  },
  "price_range_per_replica_hour": {
    "min": {
      "amount_minor": 250,
      "currency": "USD"
    },
    "max": {
      "amount_minor": 250,
      "currency": "USD"
    }
  },
  "idle_per_hour": {
    "amount_minor": 250,
    "currency": "USD"
  },
  "at_min_per_hour": {
    "amount_minor": 250,
    "currency": "USD"
  },
  "at_max_per_hour": {
    "amount_minor": 250,
    "currency": "USD"
  },
  "suggested_monthly_spend_cap": {
    "amount_minor": 250,
    "currency": "USD"
  },
  "cap_hours_at_max": 0,
  "cold_start_seconds": 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).

### 500

An unexpected server error. Safe to retry.

## Errors

- 400 [`invalid_scaling`](https://legion.ambient.xyz/docs/errors#invalid_scaling)
- 400 [`invalid_hardware`](https://legion.ambient.xyz/docs/errors#invalid_hardware)
- 400 [`env_reserved`](https://legion.ambient.xyz/docs/errors#env_reserved)
- 400 [`invalid_env`](https://legion.ambient.xyz/docs/errors#invalid_env)
- 400 [`invalid_model_setting`](https://legion.ambient.xyz/docs/errors#invalid_model_setting)
- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/services/estimate" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "target": {
    "mode": "rental",
    "rental_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "port": 1,
    "catalog_id": "string",
    "image": "string",
    "registry_credential_id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "serve": {
      "port": 1024,
      "model": "string"
    },
    "health_path": "string",
    "hardware": {
      "strategy": "cheapest_fit",
      "max_price_per_replica_hour": {
        "amount_minor": 250,
        "currency": "USD"
      },
      "gpu_models": [
        "string"
      ],
      "tier": "on_demand",
      "placement": "own_hosts",
      "min_gpu_memory_gb": 1,
      "gpu_counts": [
        1
      ]
    },
    "scaling": {
      "min_replicas": 0,
      "max_replicas": 1,
      "scale_to_zero": false,
      "idle_timeout_seconds": 60,
      "scale_down_delay_seconds": 60,
      "warm_replicas": 0,
      "target_concurrency": 1,
      "max_concurrency": 1,
      "queue_wait_threshold_ms": 100,
      "request_timeout_seconds": 10
    },
    "env": [
      {
        "name": "HF_TOKEN",
        "value": "string",
        "secret": false
      }
    ],
    "model_settings": {},
    "command": [
      "string"
    ],
    "args": [
      "string"
    ]
  },
  "monthly_spend_cap": {
    "amount_minor": 250,
    "currency": "USD"
  }
}'
```
