Chat completion means sending conversation input to an AI model and receiving generated text as the response.

Basic request flow

flowchart LR
    A["Messages and instructions"] --> B["AI model"]
    B --> C["Generated response"]

A request usually contains:

Roles in a conversation

Role Purpose
System or developer Defines behaviour and important rules
User Contains the user's request
Assistant Contains earlier model responses

Example:

const response = await ai.generate({
  instructions: "Explain concepts to a beginner backend developer.",
  input: "What is a load balancer?",
  maxOutputTokens: 500
});

console.log(response.text);

Exact SDK syntax differs between providers.

AI APIs are usually stateless

The API normally does not automatically remember every previous request.

If the conversation is:

User: My name is Aman.
AI: Nice to meet you.

User: What is my name?

The second request must also provide the relevant earlier information, or use a provider feature that preserves conversation state.

Conceptually:

const messages = [
  { role: "user", content: "My name is Aman." },
  { role: "assistant", content: "Nice to meet you." },
  { role: "user", content: "What is my name?" }
];

Sending more history uses more input tokens and context-window space.

Important settings

Not every model exposes every setting.

What your backend should handle

Common mistakes

Final mental model

A chat API call is simply: instructions + current input + relevant context → generated output.