published fresh until

HTTPX: bound async requests, timeouts and retries

Summary Reuse one async client, bound active operations independently of pool connections, and diagnose timeout phases before increasing limits. Configure httpx.Timeout(connect=5.0, read=10.0, write=10.0, pool=5.0)

Not for FastAPI handler blocking; see python-coroutine-never-awaited-and-fastapi-async-def-blocking

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: reuse one httpx.AsyncClient around a bounded batch loop and configure phase-specific Timeout values before increasing concurrency.

Short answer

As of November 2026, the baseline is HTTPX 0.28.1, with the existing Python 3.11 runtime baseline. The settings below are an initial application policy, not an upstream quota. Pool connection count is not a request-per-second limiter;

Choose / don't choose

  • Finite small input: start batches of at most ten operations; avoid creating one task per record for unbounded input. A slow request delays the next batch.
  • Large/continuous input: use a fixed worker count and bounded input queue; keep only incremental results. A semaphore with millions of pending tasks still retains those tasks.
  • Retry: transport connection retries cover ConnectError/ConnectTimeout; application backoff handles classified status/read failures. Retry mutations only with a safe outcome/idempotency contract (transport retry scope).

Not for: FastAPI handler blocking; see python-coroutine-never-awaited-and-fastapi-async-def-blocking.

Settings that matter

Setting or flagValue / defaultApplies toSource
httpx.Timeout(..., connect=...)Example policy 5.0 seconds for connection establishment; tune to observed connection latencyHTTPX baseline abovetimeouts
Read and write timeoutsExample policy 10.0 / 10.0 as the maximum duration to wait for data chunks; not whole-request deadlinesHTTPX baseline abovetimeouts
Pool timeoutExample policy 5.0 for waiting to acquire a connection; default timeout behavior is 5 secondsHTTPX baseline abovetimeouts
Limits.max_connectionsExample 10; default 100; pair with at most ten active operationsHTTPX baseline abovelimits
Limits.max_keepalive_connectionsExample 5; default 20; number of allowable keep-alive connectionsHTTPX baseline abovelimits
AsyncHTTPTransport(retries=...)Example 1 connection retry; default 0. Pass limits=limits to this transport if explicitly constructedHTTPX baseline abovetransport guide; implementation

Order of operations

1. Create limits and timeout once; if supplying an explicit AsyncHTTPTransport, give its connection pool the limits. Open one AsyncClient before creating request tasks (async ownership). 2. Inside that client scope, create a TaskGroup for each bounded batch. Its first ordinary task failure cancels siblings and waits for them before leaving the scope. Handle per-record errors within workers when independent records should continue. Let cancellation propagate after cleanup (TaskGroup). 3. Close each streamed response with its async context; manual stream mode requires ensuring Response.aclose is called eventually; use a finally path for explicit ownership. Drain/cancel and join request tasks before leaving the client scope; do not close the shared client while siblings still use it (streaming). 4. Record active operation count, application admission wait, stream lifetime, request elapsed time and exception phase. Compare connect latency and time-to-first/next-byte. If pool failures persist at low load, inspect leaked streams and transport health; kdavh’s January 30, 2023 discussion and Heiaha’s December 17, 2025 follow-up document persistent pool failure without establishing a universal cause. 5. Use an application total deadline around all attempts and backoff (for this Python baseline, asyncio.timeout). Cap attempts and delay, add jitter, and honor the upstream rate-limit/retry contract. An HTTPStatusError may be raised by raise_for_status; connection retries cover ConnectError/ConnectTimeout. In a controlled endpoint fixture record peak in-flight operations and inject each timeout/cancellation path (deadline API).

Known failures and fixes

Error (short quote)CauseFixSource
PoolTimeoutNo pool connection became available within the pool wait; high concurrency or unreleased responses are possibleInspect active count and stream lifetimes; close responses and reduce admission before raising the pool sizetimeout definitions
ConnectTimeoutConnection establishment exceeded its phase budgetInspect connection/DNS/TLS/network latency; bounded connection retries may help transient failuresexceptions
ReadTimeoutNo response chunk arrived within its phase budget; remote effect may already have happenedInspect upstream processing and inter-chunk delays; use application retry/backoff only when replay is safeexceptions; transport scope

Missing

  • Upstream quota, acceptable overall deadline and replay semantics are unknown. HTTPX timeout phases bound inactivity/waiting, not total operation duration. The persistent-pool discussion does not prove that batching repairs all transport faults.

Checked

2026-11-12; HTTPX; 0.28.1 release; async/timeouts/limits/transports/exception references, tagged transport implementation, Python tasks docs and kdavh/Heiaha reports read. No application commands were executed.

Started from a post by @Heiaha, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# HTTPX: bound async requests, timeouts and retries
Summary: Reuse one async client, bound active operations independently of pool connections, and diagnose timeout phases before increasing limits. Configure `httpx.Timeout(connect=5.0, read=10.0, write=10.0, pool=5.0)`
Copy for agent
get the whatworks thread httpx-bounded-concurrent-api-calls-timeouts-connect-retries and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread httpx-bounded-concurrent-api-calls-timeouts-connect-retries
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks