Chat completion
The basic call: send a model name and a list of messages, get back the model's reply in choices[0].message.
Last updated
Analogy
A POST endpoint that takes the whole chat log as the body and returns the next message. Stateless, like every good REST endpoint.
Code example
const res = await client.chat.completions.create({ model: "gpt-4o-mini", temperature: 0.2, messages: [ { role: "system", content: "You are a senior code reviewer. Be brief." }, { role: "user", content: "Review: const x = arr.map(a => { a * 2 })" }, ],}); console.log(res.choices[0].message.content);In an agent
An agent is this exact call, made over and over inside a loop.
Questions people ask
What does a chat completion request send and return?
You send a model name and an ordered list of messages, plus optional settings like temperature or tools. You get back a response whose choices[0].message holds the model's reply, and a usage object with token counts. The endpoint is stateless, so every request must carry the whole conversation.
How is an agent related to a chat completion?
An agent is the same chat completion call, made again and again inside a loop. Each round, your code adds the model's tool requests and the tool results to the messages, then calls the model again. When the model answers in plain text instead of asking for a tool, the loop stops.