StudyVisuallyStart studying
Part 02 · OpenAI API · Card 3 of 9

Messages array

The conversation you send on every call: an ordered list of { role, content } objects. The model sees nothing else.

Last updated

Analogy

Redux state for a chat. You never mutate the model; you append to the array and send the whole thing again.

Code example

messages.tsTypeScript
const messages = [
{ role: "system", content: "You are a helpful tutor." },
];
async function ask(question: string) {
messages.push({ role: "user", content: question });
const res = await client.chat.completions.create({ model: "gpt-4o-mini", messages });
messages.push(res.choices[0].message);
return res.choices[0].message.content;
}

In an agent

In an agent this array grows with tool calls and tool results. It is the agent's short-term memory.

Questions people ask

Why do I have to send the whole messages array every time?

The model is stateless and sees nothing except the request in front of it. The messages array is the conversation, so you append the new user message and send the full list again. If you drop earlier messages, the model forgets them, which is exactly how trimming and summarising work.

What does the messages array look like inside an agent?

It starts with the system prompt and the user's goal. Then each loop step adds an assistant message with tool calls and one tool message per result. By the final answer it can hold dozens of entries. It is the agent's short-term memory, so it is also what you trim when it gets too long.