Memory and multi-agent systems in TypeScript · Chapter 1 of 16
Why agents forget
By the end of this chapter you can explain why a language model remembers nothing between requests, make an assistant remember within a conversation by resending it, show why resending everything cannot last, and keep memory as short notes that go into every request. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Memory
- 5 tasks
- TypeScript
- Free chapter
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.
Why you need this
Baraat Bliss plans weddings in Jaipur. One wedding takes ten months and hundreds of WhatsApp messages with the couple, both families and the vendors. Last month Pooja, the founder, put an assistant on the threads to answer the families' questions at night. It lasted three weeks. Rekha, the senior planner, brought the complaints to the Monday meeting:
- It told a cousin the mehndi was on 10 December. It moved to the 9th in August, and the cousins were booking flights.
- It quoted the bride's father a budget of 45 lakh. He cut it to 38 lakh in March, and he was not pleased to hear the old number.
- It suggested laal maas for the mother of the bride's table. She is Jain.
Every one of those facts was in the thread. Karan, the AI engineer, said the assistant did not get them wrong so much as never have them. This lab builds an assistant that remembers what matters, forgets what it must, and splits the work between specialists, all in TypeScript. It starts here: why a model forgets at all.
The lesson
The lab model
Models run on a provider's servers, and your code reaches them over HTTP. The official openai npm package is the client; this lab has its own model, lab-chat, running inside your lab with the same API, so you need no account and no internet. OPENAI_BASE_URL and OPENAI_API_KEY are set, and new OpenAI() reads them; point them at a real provider with your own key and the same code talks to a real model.
import OpenAI from 'openai';
const client = new OpenAI();
const response = await client.chat.completions.create({
model: 'lab-chat',
messages: [{ role: 'user', content: 'What is the budget for WD1057?' }],
});
console.log(response.choices[0].message.content);
A wedding for 300 guests in Jaipur usually costs about ₹25 lakh, so that is the budget we plan with.
A request names a model and sends an array of messages, each a role and its content. create() returns a promise, so you await it; a lab file is an ES module, so await works at the top level, and node file.ts runs it (Node strips the types). The reply is in choices[0].message.content, typed string | null because a reply can hold tool calls instead of text.
And the reply is invented: WD1057's budget is 65 lakh. The model has never seen that wedding, so it wrote what wedding text usually says, confidently. lab requests in the terminal shows exactly what was sent and received.
A model remembers nothing
Tell the model a fact in one request, and ask about it in the next:
request 1, user: Wedding WD1057: Gurpreet Singh raised the budget to 65 lakh.
reply: Thanks for your message. How can Baraat Bliss help with your wedding?
request 2, user: What is the budget for WD1057?
reply: A wedding for 300 guests in Jaipur usually costs about ₹25 lakh, so that is the budget we plan with.
The model did not forget the 65 lakh; it never kept it. A model is a function of its input: everything it knows about your conversation is in the request it is answering. Nothing is saved on the provider's side between two calls to chat.completions.create. This is called being stateless. Some providers offer to store a conversation for you on their side; underneath, they do what the next section does.
Memory is what you resend
A chat app "remembers" because the program keeps the conversation in an array and sends the whole array every time:
const history: ChatCompletionMessageParam[] = [
{ role: 'user', content: 'Wedding WD1057: Gurpreet Singh raised the budget to 65 lakh.' },
{ role: 'assistant', content: 'Thanks for your message. How can Baraat Bliss help with your wedding?' },
{ role: 'user', content: 'What is the budget for WD1057?' },
];
Sent like this, at temperature: 0, the lab model answers from the earlier turn: Wedding WD1057: Gurpreet Singh raised the budget to 65 lakh. The assistant message is the model's own reply, added back by your code: it tells the model what it already said.
ChatCompletionMessageParam comes from 'openai/resources/chat/completions'. The annotation matters. Write const history = [] and push { role: 'user', content } onto it, and TypeScript widens 'user' to string; typecheck then refuses the request:
error TS2769: No overload matches this call.
Type '{ role: string; content: string; }[]' is not assignable to type 'ChatCompletionMessageParam[]'.
Types of property 'role' are incompatible.
Type 'string' is not assignable to type '"user"'.
Node still runs the file (types are checked by typecheck, never at run time), but the annotation also tells your editor what a message may hold, so a misspelt rol is caught before the request goes out.
So conversation memory is an array in your program. That has two consequences. You choose what goes in it, which is the whole of this lab. And it is resent on every turn, so it grows.
Why you cannot resend everything
A model reads at most a fixed number of tokens per request, its context window. lab-chat's is 8,192 tokens. The API counts a request as 3 tokens, plus, for each message, 3 and its content's tokens; the js-tiktoken package's o200k_base encoding counts the same tokens OpenAI's current models use, offline:
import { getEncoding } from 'js-tiktoken';
const enc = getEncoding('o200k_base');
enc.encode('Mehndi moves to 9 Dec').length;
Baraat Bliss's threads are in /workspace/data/threads.jsonl, one JSON message per line, and lab thread WD1042 prints one. A ten-month wedding is far longer than a support chat. Send two weddings' threads at once and the API refuses before any model reads a word; the SDK throws a BadRequestError:
BadRequestError: 400 This model's maximum context length is 8192 tokens.
However, your messages resulted in 12535 tokens. Please reduce the length of the messages.
The error object carries the API's fields: status is 400, code is context_length_exceeded, param is messages. In a catch block the error is unknown, so check err instanceof OpenAI.BadRequestError before reading err.code: that check both picks the right error and tells TypeScript the shape.
Even a thread that fits is a bad idea to resend in full. You pay for every prompt token on every turn, and a bigger window does not fix relevance: the mehndi date the assistant got wrong was in the thread twice, the old one first.
Memory as notes
Rekha does not reread three hundred messages before she answers a family. She keeps a fact sheet: the budget, the dates, the venues, who eats what. /workspace/data/facts.json holds her sheet for each wedding: every fact's current value, who said it, when, and its history (lab facts WD1057 prints one). Sent as a few lines in the system message, it is all the assistant needs for most questions:
You are Baraat Bliss's planning assistant. Answer from these notes.
Notes:
- allergy:Simran Singh: peanuts
- budget: 6500000
...
Asked "What does Lakshmi Iyer eat?" with WD1057's notes, the lab model replied diet:Lakshmi Iyer: eggetarian, from a request of 161 prompt tokens. That is the shape of everything this lab builds: memory is what you choose to put in the request. Rekha wrote her sheet by hand; from chapter 4, the assistant writes it itself, from the threads, and keeps it right when facts change.
A file that exports a function, like task 4's notes(), may also be run as a script. import.meta.main is true only when Node runs the file itself (node notes.ts), and false when another file imports it: put the request inside if (import.meta.main) and importing the file never calls the model.
Whose memory is it
Memory belongs to someone. Keep one array for every conversation and the second family's question goes out with the first family's messages. Asked about Sunita Sharma in another wedding's chat, a leaky assistant answered Wedding WD1042: Sunita Sharma, the bride's mother, is strictly vegetarian. That is a privacy breach and a wrong answer at once. Keep each wedding's memory under its own key, and never let one family's request read another's.
Your turn
Watch the model forget, then make it remember by resending. Task 3 measures the threads against the window, and task 4 answers from notes. The interview question is an assistant whose memory leaks between weddings: look at where its array is created, and how many of it there are.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
forgetful.ts: two requests, no memory
Rekha wants to see why the assistant forgot the bride's mother's diet. Send lab-chat two separate requests, each with one user message: first, exactly
Wedding WD1042: Sunita Sharma, the bride's mother, is strictly vegetarian.
and then, in a new request, exactly
Is Sunita Sharma vegetarian?
Print the text of each reply on its own line, the first reply first. Then read the second reply: where did it come from?
- Task 2
remember.ts: memory is what you resend
Make the assistant remember within one conversation. Keep an array called history. Send task 1's first message, push the model's reply onto history as an assistant message, then push the question and send the whole history again.
The second request must hold three messages in order: the first user message, the model's first reply, and the question. Print only the second reply. It should now answer from what was said earlier.
- Task 3
too-long.ts: why you cannot resend everything
Rekha's longest client thread has hundreds of messages. Read /workspace/data/threads.jsonl and turn each wedding's messages, in file order, into user messages whose content is the sender, a colon, a space and the text.
Count each wedding's tokens the way the API counts a request: 3, plus, for each message, 3 and its content's tokens with js-tiktoken's o200k_base encoding. Print one line per wedding, in the order the weddings first appear in the file, as the wedding id, a colon, a space, the count, a space and the word tokens, a comma, a space, and fits or too long against lab-chat's window of 8192 tokens, for example:
WD1000: 1234 tokens, fits
Then send the longest thread to lab-chat as one request, catch the error the API sends back, and print a last line: lab-chat refused, a space, the wedding id, a colon, a space and the error's code.
- Task 4
notes.ts: memory as notes
Rekha keeps a fact sheet for each wedding: /workspace/data/facts.json holds every wedding's current facts. Write and export notes(weddingId) in notes.ts: it returns the wedding's facts as text, one line per fact, sorted by key, each line a dash, a space, the key, a colon, a space and the fact's value. For example:
- budget: 1500000
Then, inside if (import.meta.main), ask lab-chat about WD1042 with two messages: a system message holding an instruction to answer from the notes, a blank line, Notes: on its own line and the notes; then the user question When is the mehndi? Print only the reply.
- Task 5 · interview question
Interview: fix-me.ts, a memory that leaks between weddings
ask(weddingId, text) in fix-me.ts should remember each wedding's conversation separately. Run it: a question about wedding WD1057 is answered with what was said about WD1042.
Fix ask so that each wedding has its own history: a request for one wedding never holds another wedding's messages, and a second question about the same wedding still sends that wedding's earlier messages and replies. Keep the function's name, its parameters and the export. The check calls ask for several weddings, in different orders.
What you will learn
- A model is stateless: each request starts from nothing.
- Conversation memory is the array your code resends.
- Why resending everything fails: the context window, and the cost.
- Memory as notes: a few hundred tokens of facts instead of the whole thread.
- Whose memory: one wedding's messages must never reach another's.
More in Memory
Build "Why agents forget" yourself
Baraat Bliss, a Jaipur wedding planner, must remember months of family decisions across many agents. You build its assistant in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.