typescriptintermediate

OpenAI Chat Completion with Streaming

Stream GPT responses token-by-token using the OpenAI SDK with async iteration.

typescript
import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function streamChat(userMessage: string) {
  const stream = await client.chat.completions.create({
    model: "gpt-4o",
    stream: true,
    messages: [{ role: "user", content: userMessage }],
  });

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

streamChat("Explain RAG in one paragraph.");

Sponsored

Build production AI apps faster with OpenAI

Use Cases

  • chatbot UI
  • real-time AI responses
  • token streaming

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.