# Limit concurrent generations

Source: [Limit concurrent generations](<https://docs.gptunnel.ru/en/access/inflight/>)

<a id="_top"></a>
> **Note: Organization API key**
>
> Use an API key for your organization with the Full dashboard access permission. See the roles and restrictions below. [Get an API key](<https://docs.gptunnel.ru/en/>).

Concurrent generation limits protect the organization from bursts of requests, such as an integration starting hundreds of tasks by mistake. They correspond to the concurrent generation settings in the dashboard:

- `inflightCountLimit` limits the number of generations running at once.
- `inflightRubLimit` limits their combined cost in rubles.
- `maxCostLimit` sets the maximum cost of one generation in rubles. More expensive generations are rejected.

Once a limit is reached, new generations are blocked until enough in-flight tasks finish. The employee sees the rejection without the limit values. Organization, team, and employee limits all apply at the same time. The strictest limit wins.

> **Tip: 0 means unlimited**
>
> Supply at least one of the three fields. Values must be whole numbers, including amounts in rubles. A fractional value returns HTTP `400`. Use `0` to remove a limit. Omitted fields keep their current values.

<a id="who-can-make-changes"></a>
## Who can make changes

Only keys whose creator is an **owner** or **admin** can change concurrent generation limits. A **manager** cannot change these limits for their own teams or team members. Each such item returns `forbidden`. An organization-level request from a manager returns HTTP `403`. A personal account key or a key whose creator has the member role receives HTTP `403` for the entire request.

A key creator cannot change the limits on their own account or on an employee with a higher role. For example, an admin cannot change an owner’s limits.

<a id="staff-members"></a>
## Staff members

`POST https://gptunnel.ru/api/v1/access/setInflightLimits`

Send 1 to 25 employee entries in `items`. If a login appears more than once, only its first entry is applied. An error for one employee does not stop the request from processing the others.

### Request headers

| Parameter | Type | Description |
| --- | --- | --- |
| `Authorization` (required) | `string` | API key |

### Request parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `items` (required) | `object[]` | Array of 1–25 employees. Each item has this shape: { login, inflightCountLimit?, inflightRubLimit?, maxCostLimit? } |
| `items[].login` (required) | `string` | Employee login. Matching is case-insensitive. |
| `items[].inflightCountLimit` | `number` | Maximum number of concurrent generations. Use an integer from 0 to 1,000,000. Use 0 for no limit. |
| `items[].inflightRubLimit` | `number` | Maximum combined cost of running generations, in whole rubles (RUB). Use 0 for no limit. |
| `items[].maxCostLimit` | `number` | Maximum cost of a single generation, in whole rubles (RUB). Use 0 for no limit. |

The response counts successful employee updates in `updated` and failures in `failed`. Each entry in `failedLogins` identifies the login and explains the error.

### Error reasons

| Parameter | Type | Description |
| --- | --- | --- |
| `not_found` | `string` | No employee in the organization has this login |
| `forbidden` | `string` | The caller is a manager, the target is the key creator, or the employee has a higher role than the caller. |
| `error` | `string` | Unexpected error processing this employee. Retry the request. |

<a id="example-request"></a>
### Example request

Set `GPTUNNEL_API_KEY` and replace the placeholders with values for your organization.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

key = os.environ.get("GPTUNNEL_API_KEY")
if not key:
    raise SystemExit("Set GPTUNNEL_API_KEY")

body = {
  "items": [
    {
      "login": "ivanov@company.com",
      "inflightCountLimit": 2,
      "inflightRubLimit": 500,
      "maxCostLimit": 150
    },
    {
      "login": "petrov@company.com",
      "maxCostLimit": 0
    }
  ]
}
request = Request(
    "https://gptunnel.ru/api/v1/access/setInflightLimits",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
    },
    data=json.dumps(body).encode("utf-8"),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --request POST \
  --url 'https://gptunnel.ru/api/v1/access/setInflightLimits' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "items": [
    {
      "login": "ivanov@company.com",
      "inflightCountLimit": 2,
      "inflightRubLimit": 500,
      "maxCostLimit": 150
    },
    {
      "login": "petrov@company.com",
      "maxCostLimit": 0
    }
  ]
}'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error("Set GPTUNNEL_API_KEY")

const body = {
  "items": [
    {
      "login": "ivanov@company.com",
      "inflightCountLimit": 2,
      "inflightRubLimit": 500,
      "maxCostLimit": 150
    },
    {
      "login": "petrov@company.com",
      "maxCostLimit": 0
    }
  ]
}
const response = await fetch("https://gptunnel.ru/api/v1/access/setInflightLimits", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

<a id="example-response"></a>
### Example response

<a id="tab-panel-1-0"></a>
**200 OK**

```json
{
  "code": 0,
  "updated": 1,
  "failed": 1,
  "failedLogins": [
    {
      "login": "petrov@company.com",
      "reason": "not_found",
      "message": "Employee not found in the organization."
    }
  ]
}
```

<a id="tab-panel-1-1"></a>
**400 Invalid input**

```json
{
  "code": 3,
  "message": "Invalid input, check schema issues.",
  "details": [
    {
      "code": "invalid_type",
      "expected": "integer",
      "received": "float",
      "message": "Limit must be a whole number",
      "path": ["items", 0, "inflightCountLimit"]
    }
  ]
}
```

<a id="tab-panel-1-2"></a>
**403 Forbidden**

```json
{
  "code": 7,
  "message": "Not allowed to perform this action for the organization."
}
```

<a id="teams"></a>
## Teams

`POST https://gptunnel.ru/api/v1/access/setTeamInflightLimits`

The limits apply to the entire team. Send up to 25 entries in `items`, each with `{ teamId, inflightCountLimit?, inflightRubLimit?, maxCostLimit? }`. Get `teamId` from [`POST /api/v1/employees/getTeams`](<https://docs.gptunnel.ru/en/employees/teams-list/>). The response counts successes and failures in `updated` and `failed`. Each entry in `failedTeams` has a reason: `not_found` for a team outside the organization, `forbidden` for a manager’s key, or `error`.

<a id="tab-panel-2-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

key = os.environ.get("GPTUNNEL_API_KEY")
if not key:
    raise SystemExit("Set GPTUNNEL_API_KEY")

body = {
  "items": [
    {
      "teamId": "664f0a1b2c3d4e5f60718293",
      "inflightCountLimit": 3,
      "inflightRubLimit": 1000
    }
  ]
}
request = Request(
    "https://gptunnel.ru/api/v1/access/setTeamInflightLimits",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
    },
    data=json.dumps(body).encode("utf-8"),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-2-1"></a>
**cURL**

```bash
curl --request POST \
  --url 'https://gptunnel.ru/api/v1/access/setTeamInflightLimits' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "items": [
    {
      "teamId": "664f0a1b2c3d4e5f60718293",
      "inflightCountLimit": 3,
      "inflightRubLimit": 1000
    }
  ]
}'
```

<a id="tab-panel-2-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error("Set GPTUNNEL_API_KEY")

const body = {
  "items": [
    {
      "teamId": "664f0a1b2c3d4e5f60718293",
      "inflightCountLimit": 3,
      "inflightRubLimit": 1000
    }
  ]
}
const response = await fetch("https://gptunnel.ru/api/v1/access/setTeamInflightLimits", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

```json
{
  "code": 0,
  "updated": 1,
  "failed": 0,
  "failedTeams": []
}
```

<a id="organizations"></a>
## Organizations

`POST https://gptunnel.ru/api/v1/access/setOrgInflightLimits`

Send a single object, `{ inflightCountLimit?, inflightRubLimit?, maxCostLimit? }`, as the request body. A manager’s key receives HTTP `403`.

<a id="tab-panel-3-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

key = os.environ.get("GPTUNNEL_API_KEY")
if not key:
    raise SystemExit("Set GPTUNNEL_API_KEY")

body = {
  "maxCostLimit": 5000
}
request = Request(
    "https://gptunnel.ru/api/v1/access/setOrgInflightLimits",
    method="POST",
    headers={
        "Authorization": key,
        "Content-Type": "application/json",
    },
    data=json.dumps(body).encode("utf-8"),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-3-1"></a>
**cURL**

```bash
curl --request POST \
  --url 'https://gptunnel.ru/api/v1/access/setOrgInflightLimits' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --header 'Content-Type: application/json' \
  --data '{
  "maxCostLimit": 5000
}'
```

<a id="tab-panel-3-2"></a>
**JavaScript (Node.js)**

```javascript
const key = process.env.GPTUNNEL_API_KEY
if (!key) throw new Error("Set GPTUNNEL_API_KEY")

const body = {
  "maxCostLimit": 5000
}
const response = await fetch("https://gptunnel.ru/api/v1/access/setOrgInflightLimits", {
  method: "POST",
  headers: {
    Authorization: key,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(body),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

```json
{
  "code": 0,
  "updated": true
}
```

Read the current values in the `inflight` object returned by the [access settings methods](<https://docs.gptunnel.ru/en/access/read-settings/>).
