Skip to main content

OpenAI Long Requests and Streaming Responses

:::tip Prefer the Responses API For long-form generation, code generation, and complex reasoning tasks, prefer the `/v1/responses` endpoint with streaming enabled.

The Responses API is better suited to long-running tasks because it emits structured events for text, reasoning state, and tool results. This also makes streaming increments, tool calls, and complex responses easier for clients to process. Use `/v1/chat/completions` when compatibility with an older OpenAI SDK or an existing application requires it. :::

When a request has a large context, expects a long output, or requires upstream failover and retries, a non-streaming request cannot return until the entire response is complete. A client with a 30–60 second timeout may disconnect first, while the server can still receive the upstream response and record usage later.

Enable streaming for /v1/chat/completions whenever possible:

{
"model": "gpt-5.5",
"stream": true,
"messages": [
{ "role": "user", "content": "Analyze this long document" }
]
}

Use a client timeout of at least 120 seconds for long requests. For code generation, long-document analysis, or batch summaries, 180 seconds or more is recommended.

Python SDK example​

from openai import OpenAI

client = OpenAI(
base_url="https://api.muskapi.cc/v1",
api_key="YOUR_API_KEY",
timeout=180,
)

stream = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "Analyze this long document"}],
stream=True,
)

for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)

Node.js SDK example​

import OpenAI from "openai";

const client = new OpenAI({
baseURL: "https://api.muskapi.cc/v1",
apiKey: "YOUR_API_KEY",
timeout: 180_000,
});

const stream = await client.chat.completions.create({
model: "gpt-5.5",
messages: [{ role: "user", content: "Analyze this long document" }],
stream: true,
});

for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) process.stdout.write(delta);
}

When to avoid non-streaming requests​

  • The input includes large logs, repository excerpts, or long documents.
  • The expected output is a report, substantial code, or a full translation.
  • The selected upstream account may require failover or retries.
  • The client, browser, proxy, or gateway has a 30–60 second idle timeout.

When a /v1/chat/completions entry in Usage Records shows “Streaming guide,” the request was non-streaming and took more than 30 seconds. It is close to common client timeout windows. Enable stream: true and increase the client timeout when appropriate.