Why LiteLLM hits too many clients before PostgreSQL CPU saturates
When callers start failing and the LiteLLM logs show FATAL: sorry, too many clients already while the PostgreSQL server's CPU sits low, the most likely cause is connection count, not query load. LiteLLM's own database sizing guidance makes the same point: the connection pool limit applies per worker process, so the database can run out of connection slots long before it runs out of processing capacity. The first thing to establish is how many connections the gateway is trying to hold open at peak, and whether that number is far above what max_connections on the database allows. A quiet CPU graph does not rule out the database as the bottleneck, and it should not send the team looking first at model providers or at request volume.
This runbook is for the moment a platform or database owner sees that error, or for the planning step before a LiteLLM gateway is scaled out. It covers why the per-worker pool limit multiplies across Uvicorn workers and Kubernetes replicas, how cloud defaults for max_connections on managed PostgreSQL services set the ceiling, and how to cap autoscaling so the gateway cannot request more connections than the database can serve. It also covers the checks to run before changing settings: the configured pool size, the worker count per pod, the replica ceiling, and the live connection count on the server. Confirm the current default max_connections for your specific managed PostgreSQL instance class in your cloud console, since the value depends on instance memory. Decide now who owns the database limit and who owns the gateway's replica ceiling, because a fix usually touches both.
How pools, workers, pods and autoscaling multiply connections
The diagram starts with LiteLLM replicas, each running several worker processes, and each worker holding its own connection pool to PostgreSQL. Those pool sizes add up across every worker and every replica, so the total is what reaches max_connections. Autoscaling raises the replica count, which multiplies the total again. Check the sum against the database limit after reserving slots for admin sessions and other clients.
Pool connections multiply across workers and replicas before reaching PostgreSQL's limit.
Working out the connection count from workers and maxReplicas
The connection count you need to plan for is the peak, not the average: total connections are roughly pool size multiplied by workers per pod multiplied by replicas, and the replica count at peak is maxReplicas, not the number of pods you run on a quiet afternoon. Write the formula with the values from your own configuration: pool size x workers x maxReplicas, plus headroom for administrative sessions, migrations, monitoring agents and any other service that shares the same database. LiteLLM's sizing guidance treats the pool limit as a per-worker-process setting, which is why the multiplication matters. Confirm the exact pool setting your version reads, and whether the proxy holds its pool open idle, in the LiteLLM database sizing page before you trust any number you compute.
Work through the formula with illustrative values, clearly labeled as such. Suppose a pool of 10 connections per worker, 4 workers per pod and an autoscaler ceiling of 12 pods. The worst case is 10 x 4 x 12, or 480 connections, before anything else connects. If the database's max_connections is set to 200, the cluster can reach the limit well before CPU is busy, and the error appears as FATAL: sorry, too many clients already. The average load in that same cluster might be 3 pods, which produces only 120 connections and hides the problem until a traffic spike triggers scale-out. Ask your platform team for the real maxReplicas value from the autoscaler and the worker count from the container start command, because those two numbers, not the typical replica count, set the ceiling you must fit under.
Cloud default max_connections and what it means for sizing
On a server you run yourself, max_connections is a configuration value in postgresql.conf that someone on your team set, and PostgreSQL reserves some of those slots for superusers and background processes. Nothing changes the number unless an operator edits the file and restarts the server, so the ceiling is stable until a deliberate change. Your sizing work therefore starts from the configured value and the connection budget you give LiteLLM.
- Read the configured value: Run
SHOW max_connections;andSHOW superuser_reserved_connections;to see the usable ceiling for application roles. - Trace the source: Check whether the value came from a configuration management template, a Helm chart or a manual edit, since each one can drift.
- Plan the budget: Subtract headroom for administrators, monitoring, backups and other applications before assigning anything to LiteLLM.
- Change with a restart: Raising the value requires a restart, so schedule it with the same window you use for other database changes.
- Confirm the memory cost: Higher limits increase per-connection memory use, so confirm that the host has room for the new number.
Before you size LiteLLM, record the live value from the running server, not the value in a document.
On RDS and Aurora, AWS sets the default max_connections through the parameter group, and the default is derived from instance memory. LiteLLM's database sizing page gives the formula as LEAST({DBInstanceClassMemory/9531392}, 5000). Under that formula, the page cites about 860 connections for an 8 GB instance and about 3600 for a 32 GB instance. Those values are defaults, so the parameter group in your account is what governs the real ceiling.
- Know the formula: The default is the lesser of memory divided by 9531392 and 5000, so the cap of 5000 limits the largest instance classes.
- Compare two sizes: The cited values are roughly 860 on 8 GB and 3600 on 32 GB, so a resize changes the limit without any change to your application.
- Expect the change on resize: A larger instance class can raise the default ceiling, and a smaller one lowers it, which can break a deployment that worked before.
- Check the parameter group: Confirm the value in the parameter group attached to the instance, because a custom group can override the default.
- Verify from the session: Run
SHOW max_connections;on the instance after each change to read the value the server actually uses.
Before you approve a sizing plan, confirm the live max_connections on the exact instance class and parameter group you will run in production.
Whatever the platform default, the ceiling is a hard limit that the LiteLLM pool must fit inside. Because each worker process holds its own pool, the number of connections LiteLLM can open is a product of pool size, workers and replicas, and that product has to stay below the database limit with room left for everything else. Autoscaling makes this worse because replica count is not fixed.
- Multiply the budget: Count pool size times workers times maximum replicas, then compare that total with the usable ceiling.
- Reserve headroom: Keep capacity for migrations, administrators, monitoring and other applications that share the database.
- Cap autoscaling: Set the maximum replica count so the worst-case connection total stays within the limit.
- Document the assumption: Record which instance class and parameter value the plan depends on, so a resize triggers a review.
- Test the edge: Run a load test near maximum replicas and watch for the FATAL error before production traffic does.
Before you finalize replica limits, check the live ceiling and recalculate the worst-case total.
Options for capping connections and when each fits
This table compares five ways to keep LiteLLM's total connections under what PostgreSQL will accept, with the throughput, latency, memory and restart trade-offs of each. Read the rows as a sequence: start with the cheapest change that fits your load, and use the database budget to confirm the result before rolling it out.
| Option | What it changes | Trade-off | When it fits |
|---|---|---|---|
| Lower per-worker pool size | Maximum connections each worker process may hold open | Fewer connections per worker can queue requests under peak load, raising latency; memory use drops slightly | Connections are the limit but per-worker concurrency is light, and the database has headroom once the pool is reduced |
| Reduce workers per pod | Number of Uvicorn worker processes in each pod, each with its own pool | Less throughput per pod, so more pods are needed for the same load; memory per pod falls | Your pods run several workers and the pool multiplier is the main driver; LiteLLM recommends one Uvicorn worker per pod on Kubernetes |
| Cap maxReplicas to the database budget | Upper bound on how many pods autoscaling can start | Autoscaling stops early, so sustained spikes may queue or fail once the cap is reached | Traffic is bursty and the database budget, divided by pool size per pod, is the real ceiling |
| Raise max_connections on a resized instance | Server-side connection limit on the database | Each connection uses database memory and adds overhead; a resize or parameter change may need a restart and a maintenance window | Load is legitimately higher and the instance can be resized; confirm the default for your engine and class with the provider |
| Place PgBouncer in front of PostgreSQL | Multiplexes many client connections onto fewer server connections | Adds a hop and a component to operate; pooling mode affects session features, so application behavior must be tested | Many replicas or workers must share a limited database and you can test LiteLLM against the chosen pooling mode |
Steps to cap LiteLLM connections to what PostgreSQL can serve
This sequence takes you from the database's actual connection limit to a tested configuration in which the worst-case LiteLLM connection count, at full autoscaling, stays under what PostgreSQL can serve with headroom for administrators and other applications.
Read the live connection limit and reserved slots
A platform or database engineer connects to the production PostgreSQL instance and reads the effective
max_connectionsvalue, along withsuperuser_reserved_connectionsand any role or database connection limits. On managed services, confirm the value from the instance parameter group rather than assuming the cloud default, because the default depends on instance memory. Record the usable number as available slots after reservations. The step is done when you have a single documented figure that the rest of the sequence uses.Count current connections by client
The database owner runs a query against
pg_stat_activitygrouped byapplication_name,usenameandstate, during a normal traffic window and again during a peak. This shows how many connections LiteLLM holds compared with other applications, migration jobs, monitoring agents and idle sessions. Idle connections that never close are a common cause of exhaustion, so note them separately. The step is done when you can state how much of the connection budget LiteLLM uses today and which other clients share it.Calculate the worst-case LiteLLM connection budget
The platform engineer multiplies the pool size per worker by the number of worker processes per pod, then by the maximum replica count the autoscaler can reach. LiteLLM's sizing guidance notes that the pool limit applies per worker process, so the multiplication matters more than the single pool setting. Add the connections used by any other LiteLLM component that reaches the database, and subtract the reserved slots and the connections other clients need. The step is done when the worst-case total is written down with each term labeled, and you can see whether it fits under the available figure from step one.
Set pool size and worker count
The platform engineer lowers the per-worker pool limit and the worker count in the deployment until the worst-case total fits, keeping the pool large enough for normal concurrency. LiteLLM's production guidance recommends one Uvicorn worker per pod on Kubernetes and scaling horizontally, so the worker count is usually the first value to review. Keep these settings in version-controlled configuration so the change is reviewable. The step is done when a rendered deployment manifest shows the final values and the arithmetic from step three still holds with those values.
Cap autoscaling to what the database can serve
The platform engineer sets the maximum replica count in the autoscaler to the highest number that keeps the worst-case total within budget, instead of the number the cluster could schedule. Any scaling policy that raises replicas past that ceiling will bring back the error under load, so the cap belongs in the autoscaler configuration itself. Review any other scaler, job or cron that starts LiteLLM pods. The step is done when the autoscaler's maximum, the connection arithmetic and the configuration in the repository all agree.
Decide whether PgBouncer belongs in the path
The platform team decides whether a connection pooler such as PgBouncer should sit between LiteLLM and PostgreSQL. If the needed worker and replica count cannot fit within the database limit even after the caps above, or if you plan to raise replicas beyond what the database can hold, a pooler lets many client connections share fewer server connections. Confirm with your database owner that the pooler mode and the LiteLLM database driver settings are compatible before adding it. Pooling adds a component to monitor and patch. The step is done when you have recorded a yes or no decision and the reason.
Verify under load and watch for FATAL errors
The platform engineer runs a load test at the maximum replica count, watches the connection count in
pg_stat_activityduring the test, and searches the LiteLLM and database logs forFATAL: sorry, too many clients already. Repeat the check after each rollout and after any change to autoscaling or pool settings. The step is done when the peak connection count stays below the ceiling with margin, no FATAL errors appear during the test or a normal rollout, and the results and the final configuration are filed with the change record.
Security and operational considerations for connection limits
Raising or capping connection limits changes who can log in, how credentials travel and what your audit and monitoring show. The cards below cover the controls to review before you change pool sizes, replica counts or database limits, so the fix for too many clients does not open a new security gap.
Keep the database URL in a secret manager
The connection string carries the database password, and it is often copied into Helm values, ConfigMaps or shell history during incident work. Store the full URL in your secret manager and inject it into pods at runtime. Confirm that the credential is rotated on the same schedule as other database secrets, and that changing pool settings does not require anyone to paste the URL again. Keep LITELLM_MASTER_KEY, which must start with sk-, in the same managed store.
Use a least-privilege database role
The gateway role needs to read and write its own schema, not act as a superuser or database owner. Create a dedicated login role for LiteLLM, grant only the schema privileges its migrations require, and keep migration rights in a separate role that runs only during upgrades. Confirm the role's CONNECT limits and ownership with your DBA, since a shared application role makes it hard to tell which workload opened a given connection.
Prefer SCRAM over MD5 passwords
PostgreSQL 18 deprecated MD5 password authentication in favor of SCRAM. When you raise connection counts, every new client session performs authentication, so a weak method matters more at scale. Check password_encryption on the server, confirm that pg_hba.conf entries for the LiteLLM role use scram-sha-256, and ask your DBA whether certificate or other methods listed in the PostgreSQL authentication docs fit your identity model better.
Keep TLS on for every connection
Connections from pods to the database cross the cluster network and, with a managed service, often a load balancer or pooler. Require TLS in the connection string with sslmode=verify-full where your certificate chain allows it, and confirm that any PgBouncer or proxy layer also terminates TLS on both sides. Turning TLS off to chase a connection error is a common shortcut during incidents, so add a check that the setting survives the next Helm upgrade.
Track how limit changes affect audit and monitoring
Raising pool sizes or replica counts changes the number of sessions in pg_stat_activity, the connection counts in your metrics and the rows your audit trail records. Before you change limits, record a baseline of sessions per application name and per role, and confirm that your logs can attribute each session to a LiteLLM workload. Alerts on connection usage should be retuned along with the new ceiling, or they will fire on normal load or stay silent during real exhaustion.
When a pooler or a replica changes the picture
A connection pooler can raise the number of clients the application side can serve, but it does not remove the need to size LiteLLM's own database usage. The pool limit in LiteLLM applies per worker process, so the number that reaches PostgreSQL is still the per-worker pool multiplied by workers and replicas. Place a pooler such as PgBouncer in front of the database only after you have counted that total, and confirm with the pooler's documentation and your own tests that its pooling mode works with the way LiteLLM uses prepared statements and sessions. Then check that the pooler's server-side connections to PostgreSQL stay below max_connections, and that the database still reports FATAL: sorry, too many clients already under your peak load test.
A read replica does not add write connection capacity. PostgreSQL hot standby servers serve read-only queries, while writes such as spend updates and key changes still go to the primary, so a replica adds nothing to the limit that the primary enforces on writes. Do not point LiteLLM at a replica for the write path, and do not assume that splitting read traffic onto a replica lowers the primary's connection count unless you have confirmed which queries LiteLLM sends where. Decide whether synchronous or asynchronous replication fits your recovery and latency needs, because a synchronous commit waits for standbys.
PostgreSQL does not detect a primary failure or promote a standby on its own, so automatic failover requires external tools that you select, deploy and test. A failover changes connection counts during the switch: clients reconnect at once, old connections to the former primary fail, and the pool on each worker may briefly open more connections than normal while retries run. Include failover in your load test, watch the connection count through the switch, and confirm that the replacement primary's max_connections still covers the worker and replica total that you calculated for the steady state.
Questions teams ask before fixing LiteLLM connection limits
How much effort does fixing LiteLLM connection limits take?
The effort depends on how many LiteLLM proxy instances, workers and replicas you run, and on whether the database limit is set by a managed service default or by your own configuration. A small change to pool sizes and worker counts is usually quick to make, but the sizing work, load testing and autoscaling changes take longer. The main drivers are the number of pods, the workers per pod, the pool size per worker, the database engine and its connection ceiling, and whether anything else already shares that database. Ask for the effort broken into discovery, sizing, configuration change, load test and handover, so you can see which parts are one-time and which need repeating after an upgrade or traffic change.
Can our internal team fix LiteLLM connection limits without a partner?
Often yes, if the team can read Kubernetes manifests, reach the database metrics and run a controlled load test. The work is mostly arithmetic and configuration: count the connections each worker can open, multiply by workers and maximum replicas, compare the total with the database's connection ceiling, and then cap autoscaling or add pooling where needed. Internal teams usually struggle with the load test window, with agreeing a safe autoscaling cap with the application owners, and with confirming the database's real ceiling in their environment. A partner adds value when several environments need the same change or when the team has no one who can run the load test. Confirm your own ownership split before deciding.
What access and inputs does the work need from our team?
The work needs read access to the database settings and current connection counts, read access to the Kubernetes manifests or Helm values that set replicas, workers and pool sizes, and a window for a load test in a non-production copy that matches production as closely as possible. It also needs the team that owns the database to confirm maintenance rules, and the team that owns the gateway to confirm acceptable change windows. Ask for a list of the exact inputs before the engagement starts, and confirm which changes need approval under your change management process. Access should be time-limited and recorded, and production changes should follow your normal rollout path.
How long does the fix take, and what changes the timeline?
The timeline depends on scope, not on a fixed number of days. A single gateway deployment with one database and existing metrics is a smaller job than several regions sharing a database, or a database that is also used by other applications. Timelines lengthen when the database ceiling is unclear, when a managed service needs a parameter group change and a restart, when the load test requires a scheduled window, or when autoscaling policy needs sign-off from several teams. Ask for a timeline that separates diagnosis, the configuration change, the load test and the post-change monitoring period, and ask which of those steps depend on your change calendar.
Is raising max_connections on the database enough?
Usually not on its own. Raising the database's maximum connection setting only moves the ceiling, and each LiteLLM worker still opens its own pool, so a fast autoscaling event can push the total past the new ceiling again. Each connection also consumes server memory, so a higher ceiling can hurt the database under load. A durable fix usually combines a realistic per-worker pool size, a cap on maximum replicas that fits the database, and, where the workload needs it, a connection pooler such as PgBouncer. Ask for the arithmetic that shows the worst case total connections at peak scale, not only the new setting.
What should a quote for this work include?
A useful quote states the scope in writing: which gateway deployments and databases are covered, which environments are included, and which changes are in scope, such as pool sizing, worker layout, autoscaling caps, pooler setup or database parameter changes. It should list the deliverables, including the connection budget worksheet, the configuration change set, the load test results, the monitoring changes and a short runbook. It should name the access and inputs you must provide, the assumptions about the database engine and hosting, and what is excluded, such as database major upgrades or capacity purchases. Ask whether ongoing monitoring or retesting after upgrades is a one-time item or a recurring retainer. DeployOpen does not publish a price list, and a quote follows from the written scope.
Does the fix change how LiteLLM handles user requests?
A well-run change should not change request semantics, but it does change how many database connections each worker can hold, and that can affect latency and error behavior under load. Smaller pools can queue requests that previously ran against an open connection, so the load test must check latency, error rates and spend-write behavior at peak. Confirm with your gateway team how virtual key checks and spend updates use the database in your version, and ask for test results that compare behavior before and after the change.
How DeployOpen works on LiteLLM database capacity
DeployOpen plans and implements self-hosted LiteLLM gateways inside your own infrastructure, including the PostgreSQL database behind virtual keys and spend tracking. For connection exhaustion, we start from your worker, replica and autoscaling layout, then size the connection budget so the gateway and the database agree before any change goes live.
How we scope
- We inventory proxy workers per pod, replica counts, autoscaling maximums and the current PostgreSQL connection limit.
- The written scope names the database platform, connection pooling approach and the load tests that will confirm the limits.
- Engagements are fixed-scope, with deliverables and timelines agreed in writing before any configuration changes begin.
How we build
- We calculate a connection budget per worker and per replica, then cap autoscaling so the total stays within what PostgreSQL can serve.
- We configure and test any connection pooler, such as PgBouncer, and confirm Redis is in place wherever more than one proxy instance runs.
- We test under realistic concurrency in a mirror environment, including failover and scale-out events, and record the results.
How we hand over
- You receive the connection budget worksheet, the autoscaling caps and the configuration files used in the environment.
- The runbook covers how to read FATAL: sorry, too many clients already errors, what to check first and when to escalate.
- Your platform and database teams receive a walkthrough so they can change limits without breaking the calculation.
How we support
- Ongoing support, upgrades and continuous testing are available as a retainer, which can cover re-checking limits after scaling or upgrades.
- We can review the connection budget again when you change worker counts, add a model provider or move database platforms.
- Confirm with us during scoping which support hours, response targets and escalation paths apply to your retainer.

