Lesson 3 of 23 ยท Part 0: Foundations

Context & Memory

LLMs don't remember โ€” and the system prompt is just message[0].

Last time: Temperature decides how boldly the AI picks each next word: low is steady, high is creative.
Today: The AI forgets everything between calls, so we see how apps fake memory with a messages list.
You:My name is Alex
AI:Nice to meet you, Alex!
You:What's my name?
AI:Your name is Alex!

It FEELS like it remembers...

?

How does it work?

messages = [

systemYou are a helpful assistant.
userWhat is Python?
assistantPython is a programming language...
userWhat about JavaScript?

]

System

Instructs

"You are a helpful coding assistant."

User

Asks

"How do I sort a list in Python?"

Assistant

Answers

"Use the sorted() function..."

Try another chat:

API Call #1

messages = [

systemYou are a helpful assistant.
userWhat is Python?

]

๐Ÿง 

LLM

"Python is a programming language..."
Tokens:~20(rough estimate)
Try another chat:

Your messages array now:

messages = [

systemYou are a helpful assistant.
userWhat is Python?
assistantPython is a programming language...NEW โ†

]

โ†‘ You append the response to YOUR array

๐Ÿง โœ•

LLM

No memory!

Already forgot everything about Call #1

Tokens:~140(rough; full reply included)
Try another chat:

API Call #2

messages = [

systemYou are a helpful assistant.RE-SENT
userWhat is Python?RE-SENT
assistantPython is a programming language...RE-SENT
userWhat about JavaScript?NEW โ†

]

Every previous message re-sent

๐Ÿง 

LLM

Sees it all fresh โ€” zero memory of Call #1

"JavaScript is mainly used for web pages..."
Tokens:~150โ†‘ jumped!

Token cost per API call

Call 185
Call 2127
Call 3384
Call 51,200
Call 104,800
Call 2012,000

You pay per token, and every call re-sends the whole chat.

So long chats cost more with every single turn.

Illustrative token counts. Prices differ by model and change over time.

Every model has a maximum context window

Rough examples of context sizes over time (bars not to scale; check your model's docs):

Early chat models (2022)
a few thousanda few pages
Many models by 2024
100K+a long novel
Some newer models
~1Mseveral books

However big it is, it is always a limit, and a fuller window means a bigger bill per call.

CONTEXT FULL?โ†’ the API rejects the call (or old text gets cut). Plan for it.

Strategy 1: Drop Old Messages

SYSTEM"You are a travel assistant" โ€” Always kept โœ“
User: Plan a trip to Japan
Asst: I recommend Tokyo for 3 days...
User: What about food?
Asst: Try ramen in Shibuya, sushi in Tsukiji...
User: Hotel recommendations?
Asst: Shinjuku has great options...
User: What about Kyoto?
Asst: Kyoto has temples and gardens...
User: Best day trips from Kyoto?

Dropped 6 messages โ€” room freed up!

Strategy 2: Summarize

Before (8 messages)

2,400 tokens

โœจโ†’

After (3 messages)

SUMMARY:

"User is planning a Japan trip. Discussed Tokyo (3 days), food recs (ramen, sushi), hotels in Shinjuku, and Kyoto temples."

380 tokens (saved 84%!)

illustrative numbers

โ† Prev
1/0
Next: System Prompts โ†’