Long Streams Without Blind Retries in Your Terminal
A terminal stream that stops halfway through a file
A completion prints half a migration, pauses, and throws a timeout. Your retry wrapper starts over, appending a second migration below the unfinished first one. The connection recovered; your output became unusable. For long streams, timeout settings and retry boundaries need to agree.
Fluxo gives you one key and one base URL, https://api.fluxorelay.com, across providers. It works with Claude Code, Codex, Gemini CLI, and any OpenAI SDK. This walkthrough uses Python directly so the timeout behavior stays visible. Create a Fluxo account if you want to run these commands against your own key.
Prepare Bash and select the exact model ID
Run these commands in Bash on Linux, with Python 3 and GNU timeout installed. Keep the key out of shell history by reading it interactively.
python3 -m venv .venv && source .venv/bin/activate && python -m pip install openai httpx
read -r -s -p 'Fluxo API key: ' FLUXO_API_KEY; export FLUXO_API_KEY; printf '\n'
export OPENAI_BASE_URL='https://api.fluxorelay.com/v1' — Fetch the model catalog before choosing a model. For example, locate Claude Sonnet 5.5, then copy its returned API identifier rather than guessing an identifier from its display name.
curl --fail-with-body --silent --show-error --connect-timeout 10 --max-time 60 -H "Authorization: Bearer $FLUXO_API_KEY" "$OPENAI_BASE_URL/models" | python -m json.tool
read -r -p 'Exact model ID from the catalog: ' FLUXO_MODEL; export FLUXO_MODEL
Separate connection timeouts from stream silence
Use a ten-second connection timeout and a 120-second read timeout initially. The read timeout limits how long the client waits for another network read; it does not cap the entire completion. A stream delivering data regularly can run much longer. Heartbeats can also keep the connection active without producing visible text.
Disable the SDK's automatic retries so this script owns the policy. Retry connection failures, timeouts, rate limits, and server errors only before the first stream chunk arrives. Once any chunk arrives, stop automatic retries. That conservative boundary also covers chunks containing metadata rather than text.
Run three attempts without replaying a partial stream
The following is one Bash command. Its literal backslash-n sequences become Python newlines through Bash's dollar-quoted string syntax. Standard output goes into completion.txt; diagnostics remain in your terminal. Each run replaces that file.
timeout --signal=TERM --kill-after=10s 15m python -u -c $'import os, random, sys, time import httpx from openai import OpenAI, APIConnectionError, APITimeoutError, APIStatusError client = OpenAI(api_key=os.environ["FLUXO_API_KEY"], base_url=os.environ["OPENAI_BASE_URL"], max_retries=0, timeout=httpx.Timeout(connect=10.0, read=120.0, write=30.0, pool=10.0)) for attempt in range(3): received = False stream = None try: stream = client.chat.completions.create(model=os.environ["FLUXO_MODEL"], messages=[{"role": "user", "content": "Write a detailed PostgreSQL migration plan for splitting a large events table into monthly partitions. Include SQL examples and rollback steps."}], stream=True) for chunk in stream: received = True for choice in chunk.choices: text = choice.delta.content if text: print(text, end="", flush=True) break except (APIConnectionError, APITimeoutError, APIStatusError) as exc: status = getattr(exc, "status_code", None) retryable = status is None or status == 429 or 500 <= status < 600 if received or not retryable or attempt == 2: print("Stream failed; saved output may be incomplete.", file=sys.stderr) raise if stream is not None: stream.close() stream = None delay = 2 ** attempt + random.random() print(f"Retrying before first chunk in {delay:.1f}s: {exc}", file=sys.stderr) time.sleep(delay) finally: if stream is not None: stream.close() ' > completion.txt
The outer timeout imposes a fifteen-minute wall-clock budget across all attempts and backoff delays. It normally exits with status 124 when that budget expires. Check the exit status immediately with the next command; a nonzero status means you should inspect the saved output before using it.
printf 'Exit status: %s\n' "$?"; wc -c completion.txt
Recover partial output without duplicating generated work
A retry starts a new generation; it does not resume the previous stream. Even a failure before the first received chunk can leave uncertainty about work already performed upstream. This policy prevents automatic replay after observed output, but cannot promise exactly-once generation.
Keep completion.txt after a failure. Review its ending, then make a deliberate follow-up request with the relevant partial text and a precise continuation instruction. Never execute partial generated SQL automatically.
Fluxo bills real tokens, including cache reads and writes, with no monthly fee, so repeated attempts deserve attention alongside latency. Invited users' payments also earn you a referral share. For production, record attempt counts and failure timing, and honor Retry-After when supplied instead of relying solely on this walkthrough's short exponential backoff.