Calling LLM APIs: An OpenAI Example End-to-End, Including Streaming

~14 min read

Putting the whole unit together: authenticate, send a JSON request with your prompt, check the status code, parse the JSON response — and handle the streaming variant that most chat UIs actually use.

Calling LLM APIs: An OpenAI Example End-to-End, Including Streaming is a Pro topic

Sign in, then upgrade to Pro or Power to unlock this topic and the full AI Engineering curriculum.

Key points

  • Calling an LLM API combines everything in this unit: POST request, Bearer token auth header, JSON body with your prompt/settings, status-code check, JSON response
  • Check the status code before trusting the response body — a non-2xx means the generated text is missing and the error is in the body instead
  • Streaming (stream: true) keeps the connection open and sends small chunks as they're generated, instead of one big response after a long wait
  • Streaming is what produces the familiar 'typing' effect in chat UIs, and it directly improves perceived responsiveness (time to first token)
  • LLM APIs commonly hit 429 rate-limit errors under load — production code typically retries with exponential backoff rather than failing immediately