> ## Documentation Index
> Fetch the complete documentation index at: https://prefect-bd373955-pytest-markdown.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Limit concurrent task runs

> Prevent too many tasks from running simultaneously.

Task run concurrency limits help prevent too many tasks from running simultaneously.
For example, if many tasks across multiple flows are designed to interact with a database that only allows 10 connections.

Task run concurrency limits use [task tags](#tags). You can specify an optional concurrency limit as the maximum number of concurrent
task runs in a `Running` state for tasks with a given tag.

If a task has multiple tags, it will run only if *all* tags have available concurrency.

Tags without explicit limits are considered to have unlimited concurrency.

<Note>
  **0 concurrency limit aborts task runs**

  If the concurrency limit is set to 0 for a tag, any attempt to run a task with that tag will abort instead of delay.
</Note>

### Execution behavior

Task tag limits are checked whenever a task run attempts to enter a [`Running` state](/3.0rc/develop/manage-states/).

If there are no concurrency slots available for any one of your task's tags, it delays the transition to a `Running` state
and instructs the client to try entering a `Running` state again in 30 seconds
(or the value specified by the `PREFECT_TASK_RUN_TAG_CONCURRENCY_SLOT_WAIT_SECONDS` setting).

### Configure concurrency limits

<Tip>
  **Flow run concurrency limits are set at a work pool and/or work queue level**
  While task run concurrency limits are configured through tags (as shown below),
  [flow run concurrency limits](https://docs.prefect.io/latest/deploy/dynamic-infra/control-runs/#work-pool-concurrency)
  are configured through work pools and/or work queues.
</Tip>

You can set concurrency limits on as few or as many tags as you wish. You can set limits through:

* Prefect [CLI](#cli)
* Prefect API by using `PrefectClient` [Python client](#python-client)
* Prefect server UI or Prefect Cloud

#### CLI

You can create, list, and remove concurrency limits with Prefect CLI `concurrency-limit` commands.

```bash
prefect concurrency-limit [command] [arguments]
```

| Command | Description                                                      |
| ------- | ---------------------------------------------------------------- |
| create  | Create a concurrency limit by specifying a tag and limit.        |
| delete  | Delete the concurrency limit set on the specified tag.           |
| inspect | View details about a concurrency limit set on the specified tag. |
| ls      | View all defined concurrency limits.                             |

For example, to set a concurrency limit of 10 on the 'small\_instance' tag:

```bash
prefect concurrency-limit create small_instance 10
```

To delete the concurrency limit on the 'small\_instance' tag:

```bash
prefect concurrency-limit delete small_instance
```

To view details about the concurrency limit on the 'small\_instance' tag:

```bash
prefect concurrency-limit inspect small_instance
```

#### Python client

To update your tag concurrency limits programmatically, use
[`PrefectClient.orchestration.create_concurrency_limit`](../../api-ref/prefect/client/orchestration/#prefect.client.orchestration.PrefectClient.create_concurrency_limit).

`create_concurrency_limit` takes two arguments:

* `tag` specifies the task tag on which you're setting a limit.
* `concurrency_limit` specifies the maximum number of concurrent task runs for that tag.

For example, to set a concurrency limit of 10 on the 'small\_instance' tag:

```python
from prefect import get_client

async with get_client() as client:
    # set a concurrency limit of 10 on the 'small_instance' tag
    limit_id = await client.create_concurrency_limit(
        tag="small_instance", 
        concurrency_limit=10
        )
```

To remove all concurrency limits on a tag, use [`PrefectClient.delete_concurrency_limit_by_tag`](/3.0rc/api-ref/prefect/client/orchestration/#prefect.client.orchestration.PrefectClient.delete_concurrency_limit_by_tag/), passing the tag:

```python
async with get_client() as client:
    # remove a concurrency limit on the 'small_instance' tag
    await client.delete_concurrency_limit_by_tag(tag="small_instance")
```

If you wish to query for the current limit on a tag, use [`PrefectClient.read_concurrency_limit_by_tag`](/3.0rc/api-ref/prefect/client/orchestration/#prefect.client.orchestration.PrefectClient.read_concurrency_limit_by_tag), passing the tag:

To see *all* of your limits across all of your tags, use [`PrefectClient.read_concurrency_limits`](/3.0rc/api-ref/prefect/client/orchestration/#prefect.client.orchestration.PrefectClient.read_concurrency_limits).

```python
async with get_client() as client:
    # query the concurrency limit on the 'small_instance' tag
    limit = await client.read_concurrency_limit_by_tag(tag="small_instance")
```
