Generative AIby Dhruv Narang

1. An LLM Has No Memory

Definition

An LLM is stateless. Every call is new, and the model does not remember earlier calls.

My name is Riya.
Nice to meet you, Riya!
Next call: the model starts from zero
What is my name?
I don't know your name.

2. Memory Is a List You Resend

The model forgets, so you keep the memory. Store every message in a list and send the whole list on every call. The reply goes into the list too.

Your list (the memory)
systemrules
humanquestion 1
aianswer 1
humanquestion 2
send the whole list→
LLM
reply→
aianswer 2

Added to the list, ready for the next turn.


3. What Is a Context Window?

Definition

A context window is the maximum number of tokens a model can read in one call.

The list grows every turn, so one day it no longer fits.

Context window sizesGPT-4o: 128K tokensMistral Nemo: 128K tokensGemini 2.5 Flash: 1M tokens

Code
Chatbot with historychat_history.py · 35 linesShow code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from dotenv import load_dotenv
load_dotenv()


from langchain_core.messages import AIMessage,SystemMessage,HumanMessage
from langchain.chat_models import init_chat_model
from rich import print


model = init_chat_model("mistralai:mistral-small-2603", temperature=0.7)

chat_history = [
    SystemMessage(content="You are a helpful coding tutor. Keep answers brief.")
]


print(SystemMessage(content="You are a helpful coding tutor. Keep answers brief."))
print("welcome to chatbot type exit to quit")

while True:
    user_input = input("You : ask any think: ")
    if user_input == "exit":
        break
    chat_history.append(HumanMessage(content=user_input))
    
    response = model.invoke(chat_history)
    
    chat_history.append(AIMessage(content=response.content))
    
    print(f"Bot: {response.content}")

print(chat_history)
    
        

4. MessagesPlaceholder

Definition

A MessagesPlaceholder is a slot in a ChatPromptTemplate that you fill with a list of messages.

A normal blank like {state} holds one piece of text. History is a list, so it needs a slot of its own. There are two ways to write it. Pick a tab.

from langchain_core.prompts import ChatPromptTemplatefrom langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("placeholder", "{chat_history}"),    MessagesPlaceholder("chat_history"),
    ("human", "{question}"),
])
The short form. No extra import.The class. The import has one more name, MessagesPlaceholder. The result is the same.

Now fill the slot with the history.

template.invoke({"chat_history": chat_history, "question": "What is Python?"})
Before: the slot is empty
systemYou are a helpful assistant.
placeholder{chat_history}
human{question}
→
After: the list is unrolled
systemYou are a helpful assistant.
humanHello
aiHi!
humanWhat is Java?
humanWhat is Python?

The first word of each tuple is a fixed keyword, not a name you choose. "system", "human" and "ai" are the roles from the lesson Writing Messages. "placeholder" is one more keyword, and it is not a role. It is the short form of MessagesPlaceholder.


Code
Chatbot with MessagesPlaceholderchat_history_placeholder.py · 41 linesShow code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from dotenv import load_dotenv
load_dotenv()

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import HumanMessage, AIMessage
from langchain.chat_models import init_chat_model

model = init_chat_model("mistralai:mistral-small-2603", temperature=0.7)

# 1. Define Template with placeholder in the middle
template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful and concise AI tutor."),
    ("placeholder", "{chat_history}"),  # Dynamic message list slot
    ("human", "{question}")             # Current user question
])

# 2. History Storage
chat_history = []

print("welcome to chatbot - Type 'exit' to quit")

while True:
    user_query = input("You: ")
    if user_query.lower() == "exit":
        break

    # Step A: Format template with history and current question
    formatted_prompt = template.invoke({
        "chat_history": chat_history,
        "question": user_query
    })

    # Step B: Call the model
    response = model.invoke(formatted_prompt)

    print("AI:", response.content)
    print()

    # Step C: Update history for the next iteration
    chat_history.append(HumanMessage(content=user_query))
    chat_history.append(AIMessage(content=response.content))