FastAPI dependencies construct request-scoped values and enforce reusable boundary rules; lifespan owns resources shared for the application process. Good dependency design makes identity, authorization, database sessions, gateways, and cleanup explicit. It does not hide business logic in decorators or create a service locator that can return anything from anywhere.
The HTTP connection pool belongs to the process lifespan. A database session belongs to one request or unit of work. The repository and application service can be composed from the session dependency.
Authentication dependencies can establish a trusted principal. A reusable dependency may reject a missing organization membership. Resource-specific authorization still belongs near the use case because it needs both the principal and the resource. Avoid trusting organization_id from a header without binding it to verified membership.
Use fixtures so overrides cannot leak between tests.
One process-level provider client can reuse connections, while every call receives request-scoped identity, tenant, budget, trace, and cancellation context. Never store the current user in a global provider client. Tool execution dependencies must derive authority from trusted server context, not model arguments.
Create a ticket-service dependency backed by a fake repository. Override it for a test and prove cleanup occurs when the route raises.
Acceptance criteria: repeated tests do not share records, overrides are cleared, and cleanup evidence appears on both success and failure.