{"id":36065,"date":"2025-10-30T00:47:56","date_gmt":"2025-10-29T19:17:56","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=36065"},"modified":"2025-10-30T00:48:01","modified_gmt":"2025-10-29T19:18:01","slug":"add-chatbot-memory-without-database","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/add-chatbot-memory-without-database\/","title":{"rendered":"Add Chatbot Memory Without Database (Just Free AI &amp; Simple JavaScript)"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What Context Means in a Chatbot<\/h2>\n\n\n\n<p>When people talk to a chatbot, they expect it to remember the topic. That memory is called <strong>context<\/strong>.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nUser: What is the weather in Delhi\nBot: It is sunny and 28 degrees\nUser: What about tomorrow\n<\/pre><\/div>\n\n\n<p>If your chatbot forgets context, it will reply something like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nBot: Sorry, which city\n<\/pre><\/div>\n\n\n<p>That is what happens when your chatbot has no memory. It forgets every previous message because AI APIs treat every request as new.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why Chatbots Forget Everything<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>So if you send this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMessage 1: What is Rust\nYou get\nBot: Rust is a programming language\n<\/pre><\/div>\n\n\n<p>Then you send<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMessage 2: Who created it\n<\/pre><\/div>\n\n\n<p>Now your chatbot goes blank and says<strong>:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nBot: I do not know what you mean by it\n<\/pre><\/div>\n\n\n<p>That is because your chatbot does not remember that you were talking about Rust.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The Simple Plan to Fix It<\/h2>\n\n\n\n<p>Here is how to make your chatbot remember like a pro.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Store every message that you and the bot send in one place. It can be a variable or a small database.<\/li>\n\n\n\n<li>When there are too many old messages, make a short summary of them.<\/li>\n\n\n\n<li>When you send the next request to the AI, include that summary along with the latest few messages.<\/li>\n<\/ol>\n\n\n\n<p>That way, your chatbot always knows what has been happening in the conversation.<\/p>\n\n\n\n<p>You can even use a free model like Gemini to make that summary automatically.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Let Us Code This in JavaScript<\/h2>\n\n\n\n<p>We will do this step by step and keep it simple.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Store Messages<\/h3>\n\n\n\n<p>First, create a small array that will hold all the chat messages.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlet conversation = &#x5B;];\n<\/pre><\/div>\n\n\n<p>Each message will have a role and content.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  role: &quot;user&quot; | &quot;assistant&quot; | &quot;system&quot;,\n  content: &quot;The message text&quot;\n}\n<\/pre><\/div>\n\n\n<p>Whenever a message comes, we will store it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction addMessage(role, content) {\n  conversation.push({ role, content });\n}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Summarise Old Messages Using Gemini Free Model<\/h3>\n\n\n\n<p>Now comes the fun part. We will use Gemini to summarize older parts of the chat so we do not overload the tokens.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nasync function summarizeContextWithGemini(conversation) {\n  const text = conversation.map(m =&gt; `${m.role}: ${m.content}`).join(&quot;\\n&quot;);\n\n  const apiKey = process.env.GEMINI_API_KEY;\n  const model = &quot;gemini-2.5-flash&quot;;\n\n  const body = {\n    contents: &#x5B;\n      {\n        parts: &#x5B;\n          { text: &quot;Summarize this conversation briefly, keeping only important details&quot; },\n          { text: text }\n        ]\n      }\n    ],\n    model: model,\n    temperature: 0.7,\n    max_output_tokens: 100\n  };\n\n  const response = await fetch(\n    &quot;https:\/\/generativelanguage.googleapis.com\/v1beta\/models\/&quot; + model + &quot;:generateContent&quot;,\n    {\n      method: &quot;POST&quot;,\n      headers: {\n        &quot;Content-Type&quot;: &quot;application\/json&quot;,\n        &quot;x-goog-api-key&quot;: apiKey\n      },\n      body: JSON.stringify(body)\n    }\n  );\n\n  if (!response.ok) {\n    console.log(&quot;Gemini summarization failed&quot;, await response.text());\n    return &quot;&quot;;\n  }\n\n  const data = await response.json();\n  const summary = data.candidates?.&#x5B;0]?.content?.parts?.&#x5B;0]?.text || &quot;&quot;;\n  return summary;\n}\n<\/pre><\/div>\n\n\n<p>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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Build the Final Context for the Next Request<\/h3>\n\n\n\n<p>Now we make a function that combines recent messages and the summary.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nasync function buildContext() {\n  const MAX_MESSAGES = 6;\n  let recentMessages = conversation.slice(-MAX_MESSAGES);\n\n  if (conversation.length &gt; MAX_MESSAGES) {\n    const summary = await summarizeContextWithGemini(conversation.slice(0, -MAX_MESSAGES));\n    recentMessages = &#x5B;\n      { role: &quot;system&quot;, content: &quot;Summary of previous conversation: &quot; + summary },\n      ...recentMessages\n    ];\n  }\n\n  return recentMessages;\n}\n<\/pre><\/div>\n\n\n<p>This function makes sure the AI always gets the right amount of context without sending the entire history every time.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Send the Message to the AI Model<\/h3>\n\n\n\n<p>Now we send both the summary and the latest messages to the model.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nasync function getAIResponse(userMessage) {\n  addMessage(&quot;user&quot;, userMessage);\n\n  const contextMessages = await buildContext();\n\n  const response = await fetch(&quot;https:\/\/api.openai.com\/v1\/chat\/completions&quot;, {\n    method: &quot;POST&quot;,\n    headers: {\n      &quot;Content-Type&quot;: &quot;application\/json&quot;,\n      &quot;Authorization&quot;: `Bearer ${process.env.OPENAI_API_KEY}`,\n    },\n    body: JSON.stringify({\n      model: &quot;gpt-4o&quot;,\n      messages: contextMessages,\n      max_tokens: 200\n    })\n  });\n\n  const data = await response.json();\n  const reply = data.choices?.&#x5B;0]?.message?.content || &quot;No response&quot;;\n\n  addMessage(&quot;assistant&quot;, reply);\n  return reply;\n}\n<\/pre><\/div>\n\n\n<p>You can use Gemini, Claude, or any other model here. The process is the same.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the Chatbot Flow<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nasync function chatExample() {\n  console.log(await getAIResponse(&quot;Hey, who are you&quot;));\n  console.log(await getAIResponse(&quot;What can you do&quot;));\n  console.log(await getAIResponse(&quot;Remind me what I asked earlier&quot;));\n}\n<\/pre><\/div>\n\n\n<p>Here is what happens behind the scenes:<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Save Tokens with Progressive Summarization<\/h2>\n\n\n\n<p>If your chatbot has long conversations, you can summarize older parts regularly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (conversation.length &gt; 20) {\n  const summary = await summarizeContextWithGemini(conversation.slice(0, 15));\n  conversation = &#x5B;\n    { role: &quot;system&quot;, content: &quot;Previous summary: &quot; + summary },\n    ...conversation.slice(15)\n  ];\n}\n<\/pre><\/div>\n\n\n<p>This trick keeps the bot smart without wasting tokens.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Where You Can Use This<\/h2>\n\n\n\n<p>This simple context system can be used in<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Website chatbots<\/li>\n\n\n\n<li>Customer support bots<\/li>\n\n\n\n<li>AI helpers in apps<\/li>\n\n\n\n<li>Personal side projects<\/li>\n<\/ul>\n\n\n\n<p>Even if your API does not have built-in memory, you can easily simulate it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>Most AI chatbots forget what we said two seconds ago. But with a little effort, you can give them a working memory.<\/p>\n\n\n\n<p>All you need is to store messages, summarize older parts, and send both the summary and the recent messages each time.<\/p>\n\n\n\n<p>It works with any AI model, free or paid, and it makes your chatbot feel smarter and more natural.<\/p>\n\n\n\n<p>I like to think of it as giving my chatbot a tiny brain that can remember just enough to keep a good conversation going.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":36070,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[9],"tags":[],"class_list":["post-36065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-js"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database.png",1024,608,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database-300x178.png",300,178,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database-768x456.png",768,456,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database.png",1024,608,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database.png",1024,608,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2025\/10\/Add-Chatbot-Memory-Without-Database.png",1024,608,false]},"uagb_author_info":{"display_name":"Aditya Gupta","author_link":"https:\/\/codeforgeek.com\/author\/aditya\/"},"uagb_comment_info":0,"uagb_excerpt":"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&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/36065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=36065"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/36065\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/36070"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=36065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=36065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=36065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}