Back to course

Managing Conversation History (State)

Generative AI for Web & Mobile Apps

Memory for your AI

LLMs are stateless. They don't remember previous messages unless you send the whole history back with every new request.

How to Manage Context:

  1. Maintain an array of messages: [{ role: 'user', content: 'Hi' }, { role: 'assistant', content: 'Hello!' }].
  2. When the user sends a new message, push it to the array.
  3. Send the entire array to the API.

We will explore how to manage this state efficiently in React using useState or in Vanilla JS with global variables.