Add Chatbot Memory Without Database (Just Free AI & Simple JavaScript)

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 Context Means in a Chatbot

When people talk to a chatbot, they expect it to remember the topic. That memory is called context.

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. It forgets every previous message because AI APIs treat every request as new.


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.


The Simple Plan to Fix It

Here is how to make your chatbot remember like a pro.

  1. Store every message that you and the bot send in one place. It can be a variable or a small database.
  2. When there are too many old messages, make a short summary of them.
  3. When you send the next request to the AI, include that summary along with the latest few messages.

That way, your chatbot always knows what has been happening in the conversation.

You can even use a free model like Gemini to make that summary automatically.


Let Us Code This in JavaScript

We will do this step by step and keep it simple.

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

If your chatbot has long conversations, you can summarize 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.


Where You Can Use This

This simple context system can be used in

  • Website chatbots
  • Customer support bots
  • AI helpers in apps
  • Personal side projects

Even if your API does not have built-in memory, you can easily simulate it.


Final Thoughts

Most AI chatbots forget what we said two seconds ago. But with a little effort, you can give them a working memory.

All you need is to store messages, summarize older parts, 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.

I like to think of it as giving my chatbot a tiny brain that can remember just enough to keep a good conversation going.

Aditya Gupta
Aditya Gupta
Articles: 434
Review Your Cart
0
Add Coupon Code
Subtotal