Building Chatbots
A chatbot seems to remember what you said a minute ago. In most cases the model remembers nothing: the app sends the whole conversation again with every new message. This page shows how that works, and where it breaks.
6 min read6 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 6 sources6টি সোর্স · updated Oct 2026আপডেট: Oct 2026
A chat history that growsএকটা চ্যাট ইতিহাস যা বাড়তে থাকে
Watch each turn resend all earlier turns, then switch trimming on and off to see the window.দেখুন প্রতিটা পালা আগের সব পালা আবার পাঠায়, তারপর ছাঁটাই চালু-বন্ধ করে উইন্ডোটা দেখুন।
You’ll learnআপনি শিখবেন
- Why the app resends every turnঅ্যাপ কেন প্রতি পালা আবার পাঠায়
- How the context window fillsকনটেক্সট উইন্ডো কীভাবে ভরে
- What trim and summary changetrim আর সারাংশ কী বদলায়
- Why a thread ID mattersthread ID কেন জরুরি
- How a fact gets lostকীভাবে একটা তথ্য হারায়
In plain words
A chatbot is an app that talks with a user over many turns. The model behind it does not keep the conversation between requests. The Claude Messages API is stateless: each request must carry the full conversation history Source 1সোর্স 1. OpenAI says the same of its text generation requests: each one is independent, so the app sends the earlier messages again Source 4সোর্স 4.
So the chatbot app has one main job: keep the list of messages, add each new turn to it, and send it with the next request. This list is the bot's short-term memory. LangChain calls one conversation a thread, like one email thread Source 5সোর্স 5.
সহজ English
A chatbot is an app that talks with a user over many turns. The model behind it does not keep the conversation between requests. The Claude Messages API is stateless. Each request must carry the full conversation history Source 1সোর্স 1. OpenAI says the same of its requests. Each one is independent, so the app sends the earlier messages again Source 4সোর্স 4.
So the app has one main job. It keeps the list of messages. It adds each new turn to the list. It sends the list with the next request. This list is the bot's short-term memory. LangChain calls one conversation a thread, like one email thread Source 5সোর্স 5.
বাংলা ব্যাখ্যা
চ্যাটবট হলো এমন একটা অ্যাপ, যা অনেকগুলো পালায় (turn) ইউজারের সাথে কথা বলে। এর পেছনের মডেল দুটো রিকুয়েস্টের মাঝে কথোপকথন মনে রাখে না। Claude Messages API stateless: প্রতিটা রিকুয়েস্টে পুরো কথোপকথনের ইতিহাস পাঠাতে হয় Source 1সোর্স 1। OpenAI-ও তার রিকুয়েস্ট সম্পর্কে একই কথা বলে। প্রতিটা রিকুয়েস্ট আলাদা, তাই অ্যাপ আগের মেসেজগুলো আবার পাঠায় Source 4সোর্স 4।
তাই অ্যাপের একটা মূল কাজ আছে। এটা মেসেজের তালিকা রাখে। প্রতিটা নতুন পালা তালিকায় যোগ করে। পরের রিকুয়েস্টের সাথে তালিকাটা পাঠায়। এই তালিকাই বটের শর্ট-টার্ম মেমরি (short-term memory)। LangChain একটা কথোপকথনকে বলে thread, একটা ইমেইল থ্রেডের মতো Source 5সোর্স 5।
How it works
One turn after another
The messages list alternates between user and assistant turns. Each item has a role and content Source 2সোর্স 2. The bot's rules go in the top-level system parameter, not in the list Source 2সোর্স 2.
from anthropic import Anthropic
client = Anthropic()
history = [] # the conversation so far
def chat(user_text):
history.append({"role": "user", "content": user_text})
reply = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system="You are the help desk bot of an online shop. Answer only shop questions.",
messages=history,
)
text = "".join(b.text for b in reply.content if b.type == "text")
history.append({"role": "assistant", "content": text})
return textThe earlier assistant turns do not have to come from the model. The app can add synthetic assistant messages, for example a fixed greeting Source 1সোর্স 1.
The context window fills up
The context window is all the text the model can use for one answer, including the answer itself. It is the model's working memory Source 3সোর্স 3. Each user message and each reply stays in it, so the input grows with every turn Source 3সোর্স 3. The system prompt, every message, and the output all count Source 3সোর্স 3.
What happens at the limit Source 3সোর্স 3:
| Case | Result |
|---|---|
| The input alone is larger than the window | HTTP 400, invalid_request_error, "prompt is too long" |
| The answer reaches the limit (Claude 4.5 models and newer) | The model stops with stop_reason: "model_context_window_exceeded" |
A large window does not mean "send everything". As the token count grows, accuracy and recall drop. Anthropic calls this context rot Source 3সোর্স 3.
Ways to keep the history small
LangChain lists these common patterns Source 5সোর্স 5:
- Trim: remove the first or last N messages before the call.
- Delete: remove messages from the stored state for good.
- Summarize: replace older messages with a summary.
Trimming can lose information, which is why some apps summarize Source 5সোর্স 5. Anthropic also offers compaction, which summarizes older parts of the conversation on the server Source 3সোর্স 3.
Server-side state
OpenAI can also keep the state for you: previous_response_id chains responses, and the Conversations API stores a conversation with a lasting ID Source 4সোর্স 4. The earlier input tokens in a chain are still billed as input Source 4সোর্স 4.
সহজ English
The messages list takes turns: user, then assistant, then user again. Each item has a role and content Source 2সোর্স 2. The bot's rules go in a separate system field, not in the list Source 2সোর্স 2. The Technical text has a short Python example. It adds the user message to the list, sends the list, and adds the reply to the list.
The earlier assistant turns do not have to come from the model. The app can add made-up assistant messages, for example a fixed greeting Source 1সোর্স 1.
The context window is all the text the model can use for one answer. The answer itself counts too. It is the model's working memory Source 3সোর্স 3. Each message and each reply stays in it, so the input grows with every turn Source 3সোর্স 3. The system prompt, every message, and the output all count Source 3সোর্স 3.
At the limit, 2 things can happen Source 3সোর্স 3. If the input alone is too large, the API returns HTTP 400 with "prompt is too long". If the answer reaches the limit (on Claude 4.5 models and newer), the model stops with stop_reason: "model_context_window_exceeded".
A large window does not mean "send everything". When the text grows, accuracy and recall drop. Anthropic calls this context rot Source 3সোর্স 3.
LangChain lists common ways to keep the history small Source 5সোর্স 5. Trim removes the first or last N messages before the call. Delete removes messages from the stored state for good. Summarize replaces older messages with a summary. Trimming can lose information, so some apps summarize Source 5সোর্স 5. Anthropic also offers compaction. It summarizes older parts of the conversation on the server Source 3সোর্স 3.
OpenAI can also keep the state for you. previous_response_id links one response to the next. The Conversations API stores a conversation with a lasting ID Source 4সোর্স 4. You still pay for the earlier input tokens in the chain Source 4সোর্স 4.
বাংলা ব্যাখ্যা
মেসেজের তালিকা পালা করে চলে: user, তারপর assistant, তারপর আবার user। প্রতিটা আইটেমে থাকে একটা role আর content Source 2সোর্স 2। বটের নিয়মগুলো যায় আলাদা system প্যারামিটারে, তালিকায় নয় Source 2সোর্স 2। Technical লেখায় একটা ছোট Python উদাহরণ আছে। এটা ইউজারের মেসেজ তালিকায় যোগ করে, তালিকাটা পাঠায়, আর উত্তরটা তালিকায় যোগ করে।
আগের assistant পালাগুলো মডেল থেকেই আসতে হবে এমন নয়। অ্যাপ নিজে বানানো (synthetic) assistant মেসেজ যোগ করতে পারে, যেমন একটা নির্দিষ্ট শুভেচ্ছা বার্তা Source 1সোর্স 1।
কনটেক্সট উইন্ডো (context window) হলো একটা উত্তরের জন্য মডেল যত টেক্সট ব্যবহার করতে পারে, তার সবটা। উত্তরটাও এর মধ্যে পড়ে। এটা মডেলের ওয়ার্কিং মেমরি Source 3সোর্স 3। প্রতিটা মেসেজ আর উত্তর এতে থেকে যায়, তাই প্রতি পালায় ইনপুট বাড়ে Source 3সোর্স 3। সিস্টেম প্রম্পট, প্রতিটা মেসেজ, আর আউটপুট, সবই গোনা হয় Source 3সোর্স 3।
সীমায় পৌঁছালে ২টা জিনিস হতে পারে Source 3সোর্স 3। শুধু ইনপুটই খুব বড় হলে API HTTP 400 ফেরত দেয়, সাথে "prompt is too long"। উত্তর সীমায় পৌঁছালে (Claude 4.5 ও নতুন মডেলে) মডেল stop_reason: "model_context_window_exceeded" দিয়ে থামে।
বড় উইন্ডো মানে "সব পাঠিয়ে দাও" নয়। টেক্সট বাড়লে নির্ভুলতা আর মনে রাখার ক্ষমতা কমে। Anthropic একে বলে context rot Source 3সোর্স 3।
ইতিহাস ছোট রাখার কিছু সাধারণ উপায় LangChain দেয় Source 5সোর্স 5। Trim কলের আগে প্রথম বা শেষ N-টা মেসেজ সরায়। Delete সেভ করা স্টেট থেকে মেসেজ স্থায়ীভাবে মুছে দেয়। Summarize পুরোনো মেসেজের জায়গায় একটা সারাংশ রাখে। Trim করলে তথ্য হারাতে পারে, তাই কিছু অ্যাপ সারাংশ করে Source 5সোর্স 5। Anthropic compaction-ও দেয়। এটা সার্ভারে কথোপকথনের পুরোনো অংশের সারাংশ করে Source 3সোর্স 3।
OpenAI আপনার হয়ে স্টেটও রাখতে পারে। previous_response_id একটা রেসপন্সকে পরেরটার সাথে যুক্ত করে। Conversations API একটা স্থায়ী ID সহ কথোপকথন সেভ করে Source 4সোর্স 4। চেইনের আগের ইনপুট টোকেনের জন্য তবুও আপনাকে টাকা দিতে হয় Source 4সোর্স 4।
A real-life example
An online shop runs a help desk bot. A user writes: "My order 4471 has not arrived." Then: "Can you change the address?" The second message makes sense only with the first one. Because the API is stateless, the app sends both turns in the second request Source 1সোর্স 1.
After a long chat (40 turns, product lists, and pasted error text), the bot starts to forget the order number from turn 1 and gives slower answers. This matches what LangChain warns about: long histories distract the model and cost more time and money Source 5সোর্স 5. The team adds a rule: keep the last 20 messages, and replace older ones with a short summary that keeps the order number and the address Source 5সোর্স 5.
The team also stores each conversation under its own thread ID Source 5সোর্স 5. In LangGraph, this short-term memory is scoped to a single thread Source 6সোর্স 6. In the tutorial's own view, this is also what keeps the histories of two users apart.
সহজ English
An online shop runs a help desk bot. A user writes: "My order 4471 has not arrived." Then the user writes: "Can you change the address?" The second message makes sense only with the first one. The API is stateless, so the app sends both turns in the second request Source 1সোর্স 1.
After a long chat, the bot starts to forget the order number from turn 1. The chat has 40 turns, product lists, and pasted error text. The answers also get slower. LangChain warns about this. Long histories distract the model and cost more time and money Source 5সোর্স 5. The team adds a rule. Keep the last 20 messages. Replace older ones with a short summary that keeps the order number and the address Source 5সোর্স 5.
The team also stores each conversation under its own thread ID Source 5সোর্স 5. In LangGraph, this short-term memory belongs to one thread only Source 6সোর্স 6. In the tutorial's own view, this also keeps the histories of two users apart.
বাংলা ব্যাখ্যা
একটা অনলাইন শপ একটা হেল্প ডেস্ক বট চালায়। একজন ইউজার লেখেন: "My order 4471 has not arrived." তারপর লেখেন: "Can you change the address?" দ্বিতীয় মেসেজটা প্রথমটা ছাড়া অর্থহীন। API stateless, তাই অ্যাপ দ্বিতীয় রিকুয়েস্টে দুটো পালাই পাঠায় Source 1সোর্স 1।
একটা লম্বা চ্যাটের পর বট প্রথম পালার অর্ডার নম্বর ভুলে যেতে শুরু করে। চ্যাটে আছে ৪০টা পালা, প্রোডাক্টের তালিকা, আর পেস্ট করা এরর টেক্সট। উত্তরও ধীর হয়ে যায়। LangChain এ বিষয়ে সতর্ক করে। লম্বা ইতিহাস মডেলকে বিভ্রান্ত করে, আর বেশি সময় ও টাকা লাগে Source 5সোর্স 5। টিম একটা নিয়ম যোগ করে। শেষ ২০টা মেসেজ রাখা হবে। পুরোনোগুলোর জায়গায় একটা ছোট সারাংশ রাখা হবে, যাতে অর্ডার নম্বর আর ঠিকানা থাকে Source 5সোর্স 5।
টিম প্রতিটা কথোপকথন তার নিজের thread ID-তে সেভ করে Source 5সোর্স 5। LangGraph-এ এই শর্ট-টার্ম মেমরি শুধু একটা thread-এর মধ্যে থাকে Source 6সোর্স 6। টিউটোরিয়ালের নিজের মতে, এটাই দুজন ইউজারের ইতিহাস আলাদা রাখে।
Common mistakes
- Expecting the model to remember. Without the history in the request, the model has no earlier turns. The app must send them Source 1সোর্স 1.
- Unlimited history. The history grows with every turn Source 3সোর্স 3. A long chat ends with a 400 error or a cut answer, and it gets slower and costs more on the way Source 3সোর্স 3 Source 5সোর্স 5.
- Blind trimming. Cutting old messages can drop a fact that the user gave at the start, such as a name or an order number Source 5সোর্স 5.
- Rules inside the user turn. Put the bot's role and rules in the
systemparameter Source 2সোর্স 2, not in a user message that a trim can remove. - Thinking server-side state is free. With
previous_response_id, all earlier input tokens of the chain are billed again Source 4সোর্স 4.
সহজ English
- Expecting the model to remember. If the history is not in the request, the model has no earlier turns. The app must send them Source 1সোর্স 1.
- Unlimited history. The history grows with every turn Source 3সোর্স 3. A long chat ends with a 400 error or a cut answer. On the way, it gets slower and costs more Source 3সোর্স 3 Source 5সোর্স 5.
- Blind trimming. Cutting old messages can drop a fact from the start, such as a name or an order number Source 5সোর্স 5.
- Rules inside the user turn. Put the bot's role and rules in the
systemfield Source 2সোর্স 2. A trim can remove a user message. - Thinking server-side state is free. With
previous_response_id, you pay again for all earlier input tokens of the chain Source 4সোর্স 4.
বাংলা ব্যাখ্যা
- মডেল মনে রাখবে ভাবা। রিকুয়েস্টে ইতিহাস না থাকলে মডেলের কাছে আগের কোনো পালা নেই। অ্যাপকেই সেগুলো পাঠাতে হবে Source 1সোর্স 1।
- সীমাহীন ইতিহাস। প্রতি পালায় ইতিহাস বাড়ে Source 3সোর্স 3। লম্বা চ্যাট শেষ হয় 400 এরর বা কাটা উত্তরে। পথে এটা ধীর হয় আর খরচ বাড়ে Source 3সোর্স 3 Source 5সোর্স 5।
- না দেখে ছাঁটাই। পুরোনো মেসেজ কাটলে শুরুর কোনো তথ্য হারাতে পারে, যেমন একটা নাম বা অর্ডার নম্বর Source 5সোর্স 5।
- ইউজার পালার ভেতরে নিয়ম। বটের রোল আর নিয়ম
systemপ্যারামিটারে রাখুন Source 2সোর্স 2। Trim একটা ইউজার মেসেজ সরিয়ে দিতে পারে। - সার্ভার-সাইড স্টেট ফ্রি ভাবা।
previous_response_idদিলে চেইনের আগের সব ইনপুট টোকেনের জন্য আবার টাকা দিতে হয় Source 4সোর্স 4।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Memory test. Give a fact in turn 1 (a name, an order number). Ask about it in turn 10, turn 30, and after a summary or trim.
- Long chat test. Script a chat that grows until the limit. The bot must not crash, and it must tell the user what happened.
- Isolation test. Open two sessions as two users. A fact from user A must never show up for user B.
- Reload test. Reload the page or reconnect. Check whether the history is kept, as the requirements say.
- Rule test. After 50 turns, ask the bot to break its rules. The system rules must still apply.
- Cost check. Log the input tokens per turn. They should not grow without limit.
সহজ English
These test ideas are the tutorial's own advice.
- Memory test. Give a fact in turn 1, such as a name or an order number. Ask about it in turn 10, turn 30, and after a summary or trim.
- Long chat test. Script a chat that grows until the limit. The bot must not crash. It must tell the user what happened.
- Isolation test. Open two sessions as two users. A fact from user A must never show up for user B.
- Reload test. Reload the page or reconnect. Check that the history behaves as the requirements say.
- Rule test. After 50 turns, ask the bot to break its rules. The rules must still apply.
- Cost check. Log the input tokens of each turn. They should not grow without limit.
বাংলা ব্যাখ্যা
এই টেস্ট আইডিয়াগুলো টিউটোরিয়ালের নিজের পরামর্শ।
- মেমরি টেস্ট। প্রথম পালায় একটা তথ্য দিন, যেমন একটা নাম বা অর্ডার নম্বর। ১০ম পালায়, ৩০তম পালায়, আর একটা সারাংশ বা trim-এর পরে সেটা জিজ্ঞেস করুন।
- লম্বা চ্যাট টেস্ট। এমন একটা চ্যাট স্ক্রিপ্ট করুন যা সীমা পর্যন্ত বাড়ে। বট ক্র্যাশ করবে না। কী হয়েছে তা ইউজারকে জানাবে।
- আলাদা রাখার টেস্ট। দুজন ইউজার হিসেবে দুটো সেশন খুলুন। ইউজার A-এর কোনো তথ্য কখনো ইউজার B-এর কাছে দেখা যাবে না।
- রিলোড টেস্ট। পেজ রিলোড বা রিকানেক্ট করুন। রিকোয়ারমেন্ট অনুযায়ী ইতিহাস থাকে কিনা চেক করুন।
- নিয়ম টেস্ট। ৫০ পালার পর বটকে তার নিয়ম ভাঙতে বলুন। নিয়মগুলো তখনো কাজ করবে।
- খরচ চেক। প্রতি পালার ইনপুট টোকেন লগ করুন। এগুলো সীমাহীনভাবে বাড়া উচিত নয়।
Key words
| Term | Meaning |
|---|---|
| Stateless API | An API that keeps nothing between requests, so each request sends the full history Source 1সোর্স 1 |
| Conversation history | The list of user and assistant messages so far Source 2সোর্স 2 |
| System prompt | The role and rules of the bot, in the system parameter Source 2সোর্স 2 |
| Context window | All the text the model can use for one answer, including the answer Source 3সোর্স 3 |
| Context rot | The drop in accuracy and recall as the token count grows Source 3সোর্স 3 |
| Thread | One conversation, stored under its own ID Source 5সোর্স 5 |
| Compaction | A server-side summary of older parts of a conversation Source 3সোর্স 3 |