Every FastAPI developer eventually hits the same wall: traffic spikes, every request opens a database session, and the connection pool runs dry. FastAPI 0.163.0, released on 14 November 2026, adds a simple fix right where you declare dependencies: Depends(max_concurrency=...). This tutorial explains how the new option works, how to share one limit across routes, how it interacts with yield dependencies, and the deadlock traps you should avoid.

What’s new in FastAPI 0.163.0

The 0.163.0 release has one headline feature, added in PR #16746: dependency concurrency limits. A follow-up, 0.163.1 on 15 November, fixes OpenAPI generation for response models with nested computed fields, so install the latest patch:

pip install --upgrade "fastapi[standard]"
# or with uv
uv add "fastapi[standard]>=0.163.1"

The official docs describe the feature in the Advanced Dependencies guide.

The basic idea

max_concurrency limits how many requests can be using a dependency at the same time. Extra requests wait until a slot frees up. The documentation highlights a key detail: while a request waits, it doesn’t occupy a worker thread, so those threads stay available for other work.

The value must be a positive integer. The default, None, means no limit, which is how FastAPI has always behaved.

A working example with SQLModel

Here is a small app based on the SQL databases tutorial pattern, with the session dependency limited to five at a time:

from typing import Annotated

from fastapi import Depends, FastAPI
from sqlmodel import Field, Session, SQLModel, create_engine, select


class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    name: str


engine = create_engine("sqlite:///heroes.db")
SQLModel.metadata.create_all(engine)


def get_session():
    with Session(engine) as session:
        yield session


# One shared Depends() object = one shared limit of 5
SessionDep = Annotated[Session, Depends(get_session, max_concurrency=5)]

app = FastAPI()


@app.post("/heroes/")
def create_hero(hero: Hero, session: SessionDep) -> Hero:
    session.add(hero)
    session.commit()
    session.refresh(hero)
    return hero


@app.get("/heroes/")
def list_heroes(session: SessionDep) -> list[Hero]:
    return list(session.exec(select(Hero)).all())

Run it with fastapi dev main.py. Both routes use SessionDep, so together they can never have more than five sessions open through this dependency in one worker process.

Sharing a limit (and accidentally not sharing it)

This is the part most likely to trip people up. The limit belongs to the Depends() object, not to the function.

  • Reuse the same Annotated alias (like SessionDep) everywhere you want a shared limit.
  • If you write Depends(get_session, max_concurrency=5) separately in each route, each route gets its own limit of five, even though they call the same function.
# Shared: both routes compete for the same 5 slots
SessionDep = Annotated[Session, Depends(get_session, max_concurrency=5)]

# NOT shared: each route has its own separate limit of 5
@app.get("/a")
def a(session: Annotated[Session, Depends(get_session, max_concurrency=5)]): ...

@app.get("/b")
def b(session: Annotated[Session, Depends(get_session, max_concurrency=5)]): ...

The docs add that reusing the same alias in another application (including mounted apps) also shares the limit, as long as both run in the same event loop. If two apps use separate connection pools, give each one its own Depends() object.

When does a slot get released?

For a dependency with yield, the slot is held until the exit code (the part after yield) finishes. When that happens depends on the dependency’s scope:

  • scope="request" (the default): after the response is sent.
  • scope="function": after the path operation function finishes, before the response is sent.

If you want sessions returned to the pool as early as possible, scope="function" helps:

SessionDep = Annotated[
    Session,
    Depends(get_session, max_concurrency=5, scope="function"),
]

For a dependency without yield, the slot is released as soon as the function returns. For WebSocket handlers with a yield dependency, the slot is held until the handler exits and cleanup finishes.

One more rule: reusing a cached dependency value in the same request doesn’t count again toward the limit.

Choosing the right number

FastAPI doesn’t know the size of your database pool, so you have to pick the number yourself:

  1. Start from the pool’s capacity (for SQLAlchemy, pool_size plus max_overflow).
  2. Leave room for other code that uses the same pool, such as background tasks or health checks.
  3. Divide by the number of worker processes. Each worker has its own limit. Two workers with max_concurrency=5 can have up to ten sessions open between them.

For example, with a Postgres pool of 20 connections, 4 Uvicorn workers and some headroom for background work, max_concurrency=4 per worker is a sensible starting point. Load test and adjust.

Avoid deadlocks with multiple resources

The documentation is honest about one risk: a request can get stuck if it needs a second slot while holding the first. Imagine a shared Depends(get_session, max_concurrency=1, use_cache=False) used twice in the same request. The first call takes the only slot. The second call waits for it, but the first session only closes at the end of the request, which is waiting on the second call. Nothing moves.

Guidelines that avoid this:

  • Put max_concurrency on the dependency that creates the resource (get_session), not on things that depend on it. FastAPI runs sub-dependencies before applying a dependency’s limit.
  • Don’t combine use_cache=False with a tight limit on the same dependency in one request.
  • If a request needs several limited resources, acquire them in the same order everywhere.

Testing the limit locally

You can watch the behaviour with a slow endpoint and a quick load test:

import time

def slow_resource():
    time.sleep(1)
    yield "ok"

SlowDep = Annotated[str, Depends(slow_resource, max_concurrency=2)]

@app.get("/slow")
def slow(value: SlowDep):
    return {"value": value}

Send ten concurrent requests (for example with hey -n 10 -c 10 http://127.0.0.1:8000/slow). With a limit of 2 and a one-second dependency, the requests complete in waves of two instead of all at once.

Takeaway

Depends(max_concurrency=...) gives FastAPI a built-in, per-dependency throttle that protects connection pools and other scarce resources without blocking threads. Upgrade to 0.163.1, define one shared alias per resource, choose a limit per worker process, and keep the deadlock rules in mind. The full explanation lives in the Advanced Dependencies docs.