Practised 0 / 16Next →
13 / 16 · MODULE 04 · STATELESS LLM, PROMPT & MEMORY

Demystifying chat & memory: stateless models, Context, and compression

How is a completely stateless next-token model wrapped into an assistant that supports multi-turn follow-ups and memory?

TAKE AWAYCompare Stateless, Full-History Concat, and Summary Memory in the payload inspector to view the actual raw JSON sent to the model

Now we enter system engineering outside the model. First, bust the biggest illusion: **an LLM has zero built-in conversation state or memory**. Every API call hits a completely stateless function that reads one input, emits one completion, and immediately forgets everything.

How does multi-turn chat work then? Pure illusion managed by the wrapper app: before sending your Nth question, the client silently concatenates turns 1 through N-1 into the Context payload and resends the entire transcript from scratch! When resending full history gets too expensive, the app summarizes older turns into a compact string called “Memory”.

DIAGRAM · STATELESS API & THE ILLUSION OF MEMORY

Inference weights are frozen; "memory" is the client resending the entire messages[] array every turn

Every turn re-bills the entire conversation history; once truncated past the window limit, early turns vanish.

01 FROZEN STATELESS WEIGHTSModel Weights Never Update After an API Call CompletesSend only the latest user message and the model has zero recollection of Turn 1.→02 QUADRATIC BILLING ACCUMULATIONTurn N Re-Transmits and Re-Bills Turns 1 Through NCumulative prompt tokens grow quadratically across long multi-turn sessions.→03 TRUNCATION & EXTERNAL MEMORYPin System Rules, Compact Old Turns, and Persist Key Facts ExternallyLong-term memory is an application-layer database pattern, not weight mutation.
01 FROZEN STATELESS WEIGHTS

Model Weights Never Update After an API Call Completes

Send only the latest user message and the model has zero recollection of Turn 1.

02 QUADRATIC BILLING ACCUMULATION

Turn N Re-Transmits and Re-Bills Turns 1 Through N

Cumulative prompt tokens grow quadratically across long multi-turn sessions.

03 TRUNCATION & EXTERNAL MEMORY

Pin System Rules, Compact Old Turns, and Persist Key Facts Externally

Long-term memory is an application-layer database pattern, not weight mutation.

First-principles breakdown: three steps
  1. Recognize Statelessness: Every Call Starts Fresh

    Model weights are frozen read-only during inference; without resending prior turns in the request, it remembers nothing.

  2. Full Context Concatenation: Resending the Transcript

    Prepending prior User/Assistant turns creates multi-turn continuity, at the cost of re-billing all earlier tokens on every new turn.

  3. Memory Compression: Rolling Summaries & Fact Slots

    When transcripts grow long, compact older turns into a short summary block injected at the top of the prompt.

Drag the conversation turn slider (Turn 1 → 12) and switch between [Stateless Raw], [Full History Resend], and [Summary Memory] to inspect the live HTTP payload and cumulative token bill.

TRY IT YOURSELF · LIVE CODE SANDBOXLIVE CALCULATION
RAW HTTP REQUEST PAYLOAD
[POST /v1/completions]
{"messages": [
  {"role": "system", "content": "【压缩记忆摘要 Memory】用户正在评估 DeepSeek MLA 架构,已确认 128K 场景需求。"},
  // 仅保留最近 2 轮原文 + 当前第 8 轮问题
  {"role": "user", "content": "第 8 轮追问:那它的显存占用大吗?"}
]}
Cumulative Billed Tokens2,720
Context Retention✓ Context Retained

At Turn 20, resending full history burns 50,400 cumulative tokens due to quadratic re-reading, whereas summary memory uses 6,800.

JARGON BUSTER · STRIP AWAY THE HYPE

Prompt vs. Context

Both go into the same input buffer: instructions are called Prompt, while background materials and chat history are called Context.

Under the hood, the model receives both as a single contiguous sequence of Token IDs.

LLM / Agent Memory

Not neural weight updates—just an external database or summary text pasted back into the prompt prefix on the next turn.

Once you see this, you know “memory poisoning” is fixed simply by editing or clearing that stored text.

Prompt / Prefix Caching

Since the system prompt and prior turns stay identical across turns, servers reuse their precomputed KV Cache at a discount.

Always place static instructions first and dynamic inputs last to maximize prefix cache hits.
ONE IDEA TO KEEP

Prompt is the instruction, Context is supplied background, and Memory is compressed history—to the stateless model, they are all just one flat string of input tokens.

CHECK YOUR UNDERSTANDING

When using a standard LLM API on Turn 10 of a conversation, why does the assistant still remember the name you told it on Turn 1?

When using a standard LLM API on Turn 10 of a conversation, why does the assistant still remember the name you told it on Turn 1?
REFERENCES & FURTHER LEARNING

Explanations, cases, and exercises are authored by Reallier. Links provide further learning. The workplace activity and people are fictional.