TL;DR: Agents blame Neon; it is pool config. ## The rule (from Neon's SQLAlchemy guide) Set both of these on your engine: - # SQLAlchemy + Neon: make the pool survive idle suspends ## The trap Neon puts computes into an idle state and closes connections after 5 minutes of inactivity. SQLAlchemy keeps pooled connections open longer than that by default, so the first query after a quiet period fails on a dead connection.

## Fix
1. Agents blame Neon; it is pool config. ## The rule (from Neon's SQLAlchemy guide) Set both of these on your engine: -.
   Expected: this specific failure stops.
2. : set to a value at or below your compute's scale-to-zero idle timeout, so connections recycle before Neon kills them.
   Expected: this specific failure stops.
3. is the single highest-value setting; add it first. -.
   Expected: this specific failure stops.
4. in seconds, at or under your scale-to-zero setting. - If you changed the scale-to-zero timeout in the Console, update.
   Expected: this specific failure stops.
5. to match. - Schema migrations: run them on the direct connection string, same pooled-vs-direct rule as every other framework.
   Expected: this specific failure stops.

## Details
SQLAlchemy keeps pooled connections open longer than that by default, so the first query after a quiet period fails on a dead connection. `pool_pre_ping=True` : checks the connection is alive before each checkout, transparently reconnecting if Neon closed it. - `pool_recycle` ## Checklist - `pool_pre_ping=True` `pool_recycle` `pool_recycle`

## When to use
You hit exactly this: SQLAlchemy + Neon: pool_pre_ping and pool_recycle beat idle disconnects in SQLAlchemy + Neon.

## When not to use
A different error, or the same symptom in a different tool. This page only covers the failure above.

## Compatibility
SQLAlchemy + Neon.

## Root cause
```python
from sqlalchemy import create_engine
engine = create_engine(
    DATABASE_URL,
    pool_pre_ping=True,
    pool_recycle=280,  # below the 5-minute (300s) default idle timeout
)
```

## Edge cases
- # SQLAlchemy + Neon: make the pool survive idle suspends ## The trap Neon puts computes into an idle state and closes connections after 5 minutes of inactivity.