New to Rust? Grab our free Rust for Beginners eBook Get it free →
Add AI Chatbot Memory for Free: Chat History and Long-Term Memory in AI Chatbots

When we make API calls to AI chatbots, we ask a question, and they give us an answer. But if we ask another question, the AI has no idea what we were talking about before. It forgets everything from the previous message because most free APIs or hosted AI models work on a one-time request system.
That means if you are building a chatbot for your website or a small project, your bot will respond like it is meeting the user for the first time every single time. No memory, no context, just a new blank start with every question.
In this guide, I will show you how to fix that. How to add chatbot memory using only free AI APIs and JavaScript, without any database or backend setup. It is lightweight, works anywhere, and makes your chatbot feel way smarter.
What Is Chatbot Memory?
When people talk to a chatbot, they expect it to remember the topic. That memory is called chatbot memory.
For example:
User: What is the weather in Delhi
Bot: It is sunny and 28 degrees
User: What about tomorrow
If your chatbot forgets context, it will reply something like:
Bot: Sorry, which city
That is what happens when your chatbot has no memory stores. If you are building a conversational chatbot like OpenAI ChatGPT, memory storage can become a big problem and make the user experience worse.
Why Chatbots Forget Everything
Most AI APIs, like Gemini or GPT, work like vending machines. You put in one prompt and you get one reply. Once it is done, the machine does not remember you ever came there.
So if you send this:
Message 1: What is Rust
You get
Bot: Rust is a programming language
Then you send:
Message 2: Who created it
Now your chatbot goes blank and says:
Bot: I do not know what you mean by it
That is because your chatbot does not remember that you were talking about Rust.
Real-World Applications Of Chatbot Memory
Chatbot memory is useful in many real world use cases:
- Customer support chatbots can remember previous issues and will not ask the same questions over and over again.
- AI learning assistants can track the progress of students and give explanations that are personalised.
- E-commerce chatbots can remember the products that a customer has previously viewed and offer better recommendations.
- Personal AI assistants are able to remember preferences, favourite topics and past conversations in order to give more useful answers.
Without memory, every conversation begins from scratch. Memory provides a more human, natural interaction.
Architecture for Chatbot Memory
Here is a simple architecture that allows an AI chatbot to remember previous conversations:
- Store every user message and AI response in one place. This can be an in-memory array, a file, or a database such as MongoDB or PostgreSQL.
- When there are too many old messages, generate a summary.
- Save that summary separately as long-term memory.
- When sending a new request to the LLM, include:
- The long-term memory summary
- Recent conversation messages
- The user’s latest input
- The model can then use both recent context and historical information to generate more relevant responses.

This approach reduces token usage while allowing the chatbot to maintain awareness of past interactions over long conversations.
Implementing Chatbot Memory and Chat History using JavaScript
Now let’s add memory to our chatbot using JavaScript. We will store messages, summarize old conversations, and provide context to the AI model.
Step 1: Store Messages
First, create a small array that will hold all the chat messages.
let conversation = [];
Each message will have a role and content.
{
role: "user" | "assistant" | "system",
content: "The message text"
}
Whenever a message comes, we will store it.
function addMessage(role, content) {
conversation.push({ role, content });
}
Step 2: Summarise Old Messages Using Gemini Free Model
Now comes the fun part. We will use Gemini to summarize older parts of the chat so we do not overload the tokens.
async function summarizeContextWithGemini(conversation) {
const text = conversation.map(m => `${m.role}: ${m.content}`).join("\n");
const apiKey = process.env.GEMINI_API_KEY;
const model = "gemini-2.5-flash";
const body = {
contents: [
{
parts: [
{ text: "Summarize this conversation briefly, keeping only important details" },
{ text: text }
]
}
],
model: model,
temperature: 0.7,
max_output_tokens: 100
};
const response = await fetch(
"https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-goog-api-key": apiKey
},
body: JSON.stringify(body)
}
);
if (!response.ok) {
console.log("Gemini summarization failed", await response.text());
return "";
}
const data = await response.json();
const summary = data.candidates?.[0]?.content?.parts?.[0]?.text || "";
return summary;
}
This small function will send your chat history to Gemini and return a short summary. If something goes wrong, it will return an empty string, so your chatbot never breaks.
Step 3: Build the Final Context for the Next Request
Now we make a function that combines recent messages and the summary.
async function buildContext() {
const MAX_MESSAGES = 6;
let recentMessages = conversation.slice(-MAX_MESSAGES);
if (conversation.length > MAX_MESSAGES) {
const summary = await summarizeContextWithGemini(conversation.slice(0, -MAX_MESSAGES));
recentMessages = [
{ role: "system", content: "Summary of previous conversation: " + summary },
...recentMessages
];
}
return recentMessages;
}
This function makes sure the AI always gets the right amount of context without sending the entire history every time.
Step 4: Send the Message to the AI Model
Now we send both the summary and the latest messages to the model.
async function getAIResponse(userMessage) {
addMessage("user", userMessage);
const contextMessages = await buildContext();
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`,
},
body: JSON.stringify({
model: "gpt-4o",
messages: contextMessages,
max_tokens: 200
})
});
const data = await response.json();
const reply = data.choices?.[0]?.message?.content || "No response";
addMessage("assistant", reply);
return reply;
}
You can use Gemini, Claude, or any other model here. The process is the same.
Step 5: Test the Chatbot Flow
async function chatExample() {
console.log(await getAIResponse("Hey, who are you"));
console.log(await getAIResponse("What can you do"));
console.log(await getAIResponse("Remind me what I asked earlier"));
}
Here is what happens behind the scenes:
User message is stored, older messages are summarized if needed, the next request is sent with the summary plus latest messages, the model replies, and everything repeats.
Save Tokens with Progressive Summarization
Memory management is also important. If your chatbot has long conversations, you can summarise older parts regularly.
if (conversation.length > 20) {
const summary = await summarizeContextWithGemini(conversation.slice(0, 15));
conversation = [
{ role: "system", content: "Previous summary: " + summary },
...conversation.slice(15)
];
}
This trick keeps the bot smart without wasting tokens.
Challenges In Implementing Chatbot Memory
Adding memory to a chatbot sounds simple, but there are a few challenges as well:
- The first challenge is token limits. AI models can only read a certain amount of text at one time (known as the context window). If your conversation becomes very long, you cannot keep sending the entire chat history.
- The second challenge is memory quality. If your summaries are too short, the chatbot may forget important details. If they are too long, you waste tokens.
- Another challenge for implementing memory is memory capabilities. For small projects, an array may be enough. But for real applications, you may need a database to save conversations permanently.
- Finally, there is cost. Every time you send large amounts of context, you use more API tokens. Good memory systems try to balance context quality and token usage.
Best Practices For Chatbot Memory
- Keep only the relevant information that is important for future conversations.
- Regularly summarize the conversation to minimise token usage.
- Store recent chats separately from long-term memory summaries.
- Avoid sending the entire conversation history with every request.
- Use clear system messages when injecting summaries so the AI understands they represent previous context.
- If possible, test your chatbot with long conversations to make sure important details are not lost during summarisation.
- A good memory system should make the chatbot smarter without increasing API costs.
Key Takeaway
Most AI chatbots forget what we said two seconds ago. But with a little effort, you can give them a working memory and make user interaction better.
All you need is to store messages, summarize prior conversations, and send both the summary and the recent messages each time.
It works with any AI model, free or paid, and it makes your chatbot feel smarter and more natural.
It’s like giving a chatbot a tiny brain that can remember just enough to keep a good conversation going.



