httpx is a modern, async-capable HTTP client for Python. It's what the official OpenAI SDK uses internally.
Production AsyncClient Setup
Key insight: Do NOT create a new AsyncClient per request. Connection setup is expensive. Create one client and reuse it throughout the application lifecycle. Use context managers for proper cleanup.
The two-class lifecycle
httpx exposes httpx.Client (sync) and httpx.AsyncClient (async). Both are context managers (with httpx.Client() as client, async with httpx.AsyncClient() as client). Both pool TCP connections. Constructing per-request defeats the pool and kills throughput.
For LLM streaming, configure timeouts explicitly: connect=10 (don't wait forever for a flaky DNS resolution), read=300 (long, because streamed responses can take minutes), write=10, pool=10. timeout=None is dangerous — a hanging response turns into a leaked connection that lives until the OS reclaims the socket.