StudyVisuallyStart studying
AI developers · Part 02

OpenAI API

Chat completions, the messages array, streaming and structured output.

Last updated

  1. The OpenAI SDKThe OpenAI API is a normal HTTPS API with an official Node package. The client reads your key from the OPENAI_API_KEY environment variable.
  2. Chat completionThe basic call: send a model name and a list of messages, get back the model's reply in choices[0].message.
  3. Messages arrayThe conversation you send on every call: an ordered list of { role, content } objects. The model sees nothing else.
  4. Usage and costEvery response includes a usage object: prompt_tokens (what you sent) and completion_tokens (what it wrote). That is your bill.
  5. Structured outputForce the reply to match a JSON schema, so your code gets typed data instead of a paragraph it has to guess at.
  6. Who reads the answer?If a human reads the reply, plain text is fine. If code reads it, use structured output so parsing never breaks.
  7. StreamingSet stream: true and the reply arrives in small pieces as it is generated, instead of all at once at the end.
  8. Picking the right featureChat completion when a human reads it, structured output when code reads it, tool calling when the model needs outside data or actions, embeddings for semantic search, streaming when a user is waiting.
  9. Responses API and Agents SDKOpenAI also has a newer Responses API and an Agents SDK for JS. They build on the same ideas as chat completions.