How many messages does the model see?
Take your time, follow the code and check your understanding.
The codeLLM API basics
const history = [{ role: "user", content: "My name is Asha." }];
// The app calls the API, then forgets to keep the history and starts fresh:
const request2 = [{ role: "user", content: "What is my name?" }];
console.log(request2.length);Your answer4 min · no timer
Show solution and explanation
Chat completion APIs are stateless. The second request contains one message, so the model cannot know the name. To keep context, resend earlier turns: [...history, assistantReply, newQuestion].
1