Stream Claude and GPT with One OpenAI SDK
Why streaming matters for Claude and GPT
Streaming turns a slow, wait-and-see chat response into a live experience. Instead of waiting for the full answer, your app can display tokens as soon as they arrive. That makes support bots feel instant, coding assistants feel interactive, and long-form writing tools feel responsive.
If you are building with Claude or GPT, the good news is that you do not need two separate client stacks. With 59API, you can point an OpenAI-compatible SDK at https://api.59api.com and stream responses from Claude models like Opus, Sonnet, Haiku, or Fable, as well as GPT models, using the same integration style. Because 59API is a pay-as-you-go relay with native, official-quality models and no downgrade, it is also a smart way to keep streaming costs low while still getting production-grade output.
Step 1: Configure your SDK once
For most teams, the easiest path is the OpenAI SDK. Set your API key and override the base URL to the 59API endpoint. The rest of your code can stay familiar.
const client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: "https://api.59api.com/v1" });
Python: client = OpenAI(api_key=os.getenv("API_KEY"), base_url="https://api.59api.com/v1")
This matters because tools like Claude Code and Codex already speak the OpenAI-style language. If your tool can use an OpenAI SDK, it can usually stream through 59API with minimal or no code changes.
Step 2: Stream a GPT response
Here is a simple Node.js example that streams a GPT answer token by token. The key detail is stream: true.
const stream = await client.chat.completions.create({ model: "gpt-4.1-mini", messages: [{ role: "user", content: "Write a 3-step launch checklist for a SaaS app." }], stream: true });
for await (const chunk of stream) { const text = chunk.choices[0]?.delta?.content || ""; process.stdout.write(text); }
In a browser or server app, you would send each chunk to the UI instead of printing it. The pattern is the same: read the delta, append it to the response area, and keep the connection open until the model finishes.
Step 3: Stream a Claude response
Switching to Claude is just a matter of changing the model name to the Claude model you want to use in 59API. The streaming logic stays the same, which is exactly why a relay is helpful when you want one code path for multiple model families.
const claudeStream = await client.chat.completions.create({ model: "claude-sonnet", messages: [{ role: "user", content: "Explain streaming responses in one paragraph for a product manager." }], stream: true });
for await (const chunk of claudeStream) { const text = chunk.choices[0]?.delta?.content || ""; process.stdout.write(text); }
This approach works well for customer-facing chat because Claude often shines on longer, more polished explanations, while GPT is a strong fit for structured outputs, quick transforms, and coding workflows. With 59API, you can choose based on the task, not the SDK.
Practical tips for production streaming
- Ignore empty deltas. Streaming objects often include chunks with no text. Only append content when delta.content exists.
- Support cancellation. If a user clicks stop, use an abort signal so your app does not keep paying for tokens you no longer need.
- Handle reconnects carefully. For chat UIs, if a stream drops, restart only when it is safe to do so and avoid duplicating partial output.
- Show a typing state. Even the first chunk can take a moment, so a cursor or spinner improves perceived speed.
- Choose the right model for the job. Use cheaper, faster options for simple chat, and reserve heavier Claude or GPT models for complex reasoning or polished writing.
Why 59API is a strong choice for streaming apps
Streaming multiplies token usage quickly, so cost matters. 59API is attractive because it offers cheap, pay-as-you-go access without forcing you into a fixed monthly plan. You get the same native-quality model experience you expect from Claude and GPT, but with pricing that is easier to justify during prototyping and easier to scale when traffic grows.
It is also a practical relay choice if you want one API base for multiple tools and workflows. Whether you are building a live coding assistant, a customer support widget, or an internal knowledge bot, you can keep the client simple, stream responses fast, and control spend more tightly. The referral rebate is a nice extra if you are sharing the platform with teammates or clients.
Ready to try it?
If you want to test streaming without overpaying, sign up for 59API, grab your key, and point your OpenAI-compatible SDK to https://api.59api.com. In a few minutes, you can stream both Claude and GPT responses from the same codebase and ship a noticeably better user experience.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis