SDK 는 두 client class 를 줘 — OpenAI() (sync) 와 AsyncOpenAI() (async). 둘 다 내부에서 httpx connection 을 pool 해. App startup 에 한 개 만들어서 끝까지 재사용. Per-call 로 새로 만드는 건 silent performance bug.
왜 1 process 당 1 client?
매 OpenAI() 호출은 underlying httpx client 를 새로 구성해. httpx client 는 connection pool 을 들고 있어. Per-call construction = 매 request 마다 connection pool 만들고 부숨 = TLS handshake 매번, no keep-alive, no pooling. 부하 들어오면 inexplicable latency tail 과 connection-cap 경고로 만나게 돼.
FastAPI / async framework 안에선 AsyncOpenAI
Sync client 는 event loop 를 block 해. FastAPI, Starlette, aiohttp 안에선 항상 AsyncOpenAI. Async handler 에서 sync OpenAI 섞는 게 'why is my server slow' 버그의 단골.
Per-call override
특정 호출만 다른 timeout 이나 retry 가 필요하면 client.with_options(max_retries=5, timeout=120.0).chat.completions.create(...) — pool 새로 안 만들고 그 호출만 override.