59API

← Retour aux guides

Stream Claude and GPT Tokens with 59API

API · EN · 2026-09-18

Why stream model responses?

Streaming sends generated text to your application as soon as the model produces it, instead of waiting for a complete answer. It makes chat interfaces feel faster, lets users stop long requests early, and allows your server to render partial results immediately. This is especially useful for coding assistants, document analysis, and interactive support tools.

59API is a practical option when you need both Claude and GPT access without maintaining separate integrations. Its relay uses the base URL https://api.59api.com, supports standard Claude and OpenAI-compatible tooling, and provides pay-as-you-go access to native official-quality models. That means the same streaming pattern can support Claude Opus, Sonnet, Haiku, Fable, and GPT models while keeping implementation costs predictable.

Set up server-side credentials

Install the SDKs in a Node.js project. Keep your 59API key on the server: never expose it in browser JavaScript or a mobile app.

Install packages

npm install @anthropic-ai/sdk openai

Create an environment variable before starting your application.

Environment variable

export API_59API_KEY="your_59api_key"

Stream a Claude response with the Anthropic SDK

The Anthropic SDK returns an async iterable when stream: true is set. Each event can represent metadata, a content delta, or completion status. For a terminal proof of concept, print only text deltas.

claude-stream.mjs

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ apiKey: process.env.API_59API_KEY, baseURL: "https://api.59api.com" });

const stream = await client.messages.create({ model: "claude-sonnet-4-5", max_tokens: 600, messages: [{ role: "user", content: "Explain server-sent events in three short paragraphs." }], stream: true });

let fullText = "";

for await (const event of stream) { if (event.type === "content_block_delta" && event.delta.type === "text_delta") { process.stdout.write(event.delta.text); fullText += event.delta.text; } }

console.log("\nFinished. Characters received:", fullText.length);

Claude emits several event types, so filtering for content_block_delta and text_delta avoids accidentally rendering message metadata. Accumulating fullText is useful if you need to save the final answer, run moderation after completion, or attach the result to a conversation record.

Stream a GPT response with the OpenAI SDK

For GPT models, point the OpenAI SDK at the 59API OpenAI-compatible base URL. Chat completion chunks place incremental text in choices[0].delta.content. Some chunks contain only role or finish metadata, so use an empty-string fallback.

gpt-stream.mjs

import OpenAI from "openai";

const openai = new OpenAI({ apiKey: process.env.API_59API_KEY, baseURL: "https://api.59api.com/v1" });

const stream = await openai.chat.completions.create({ model: "gpt-4.1-mini", stream: true, messages: [{ role: "user", content: "Give me a concise TypeScript retry strategy for HTTP 429 errors." }] });

let fullText = "";

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

console.log("\nCompleted:", fullText.length, "characters");

Use one model family consistently within a request. Although Claude and GPT streaming both expose async iterables in these examples, their event schemas differ. Keep provider-specific parsing inside a small adapter function rather than mixing delta formats throughout your application.

Send tokens from your server to the browser

Your application should call 59API from a backend route, then forward clean text tokens to the client. Set Content-Type to text/event-stream for Server-Sent Events, or use a readable HTTP response if your framework supports it. Write each token as it arrives and flush headers before model generation starts.

Key production rules

Choose the right streaming model

Use a fast, lower-cost model for routine chat and classification, then reserve stronger Claude or GPT models for complex reasoning, code review, and high-value writing. With 59API, you can make that routing decision while retaining familiar Claude Code, Codex, Anthropic, and OpenAI SDK workflows. If you are building a multi-model streaming feature, sign up for 59API and test the same prompt across suitable models before setting your production defaults.

Tarifs · Docs

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite