FastAPI 0.163 adds Depends(max_concurrency=...) to cap concurrent use of a dependency
FastAPI 0.163.0, released on 14 November, adds a concurrency limit for dependencies: Depends(max_concurrency=...). The documentation added in the same pull request, #16746, uses a database session as the example. With SessionDep = Annotated[Session, Depends(get_session, max_concurrency=5)], at most five requests can hold a session through that dependency at the same time; a sixth waits until one is closed, and while it waits it does not occupy a worker thread. The value must be a positive integer, and the default, None, means no limit.
The limit belongs to the Depends() object, not to the function. Routes that reuse the same alias share one limit; writing a new Depends(get_session, max_concurrency=5) in each route gives each its own five, even though they call the same function. Two applications that reuse the same object share the limit if they run in the same event loop, including mounted applications. Each worker process has its own limit, so two workers can hold ten sessions, and applications in different event loops never share one.
For a dependency with yield, the session counts until its exit code has finished. With the default scope="request" that happens after the response is sent; with scope="function", after the path operation function returns. A cached dependency value used again does not count a second time. FastAPI resolves sub-dependencies before applying a dependency's limit, so the documentation says to put the limit where the session is created, and to choose a value that leaves room for other users of the same connection pool, whose size FastAPI does not know.
The documentation also describes how requests can get stuck. A shared Depends(get_session, max_concurrency=1, use_cache=False) used twice in one request waits for its own first session, which closes only when the request ends. Two requests can block each other if each holds one resource while waiting for the other. FastAPI applies each limit separately and does not reserve everything a request will need, so the advice is to acquire several resources in the same order. The pull request states that it was made with the help of Codex and reviewed manually. Version 0.163.1, released on 15 November, fixes the OpenAPI output for a response model with a nested computed field.

Why it matters
A limit that lives on an object rather than a function is easy to share by accident and easy to fail to share. The documentation spells out both cases, and the per-process and per-event-loop boundaries, before anyone has to find them in production.