Lesson 4 of 13 · 18m
Conversation State Is Your Job
The API remembers nothing — chat is re-sent history, so memory means token budgets, summarization, and validated writes.
The API has no memory. There is no session, no conversation object with state, no server-side history. When your chat UI shows a flowing dialogue, what's actually happening is that every request re-sends the entire messages array — system prompt, every user turn, every assistant turn — and the model answers as if it read it all for the first time. "Chat" is a replay your own app performs on itself.
The arithmetic of history
Turn N contains N−1 previous turns. A conversation that runs 40 turns at ~400 tokens per turn resends ~15.6k tokens of history on the last call — and you paid for that prefix on every call along the way. Cost grows quadratically with conversation length while recall degrades: lost-in-the-middle hits history too, so the model forgets turn 3 precisely when history is longest. "It remembered everything" and "it 500s at turn 60" are the same unbounded-growth bug seen from two ends.
The three working strategies
Sliding window. Keep the last K turns verbatim, drop the rest. Cheap, zero moving parts, loses old facts permanently. Right for short sessions and stateless tools.
Running summarization. At a token threshold, ask the model to compress older turns into a summary block and carry summary + last K turns forward. Preserves gist, loses detail — a fact from turn 4 ("the server runs AlmaLinux 9") can get smoothed away. Mitigate it by keeping extracted facts out of the prose summary and in structured fields that get injected verbatim.
Retrieval memory. Embed turns or extracted facts as they happen; retrieve relevant ones per query — the RAG machinery pointed at your own history. Mem0 and Zep productize this; MemGPT/Letta made the "OS paging" framing famous. It scales to long horizons but adds an index to keep fresh and a new failure mode: retrieving a stale "fact" the user already corrected.
In practice the request looks like this — the model's own past replies go back in as assistant turns, because to the model they were just earlier text:
[
{ "role": "system", "content": "<instructions>" },
{ "role": "system", "content": "<running summary + extracted facts>" },
{ "role": "user", "content": "<turn 34>" },
{ "role": "assistant", "content": "<turn 34 reply>" },
{ "role": "user", "content": "<turn 35>" }
]Memory writes are untrusted output
Whatever writes to memory runs on model output, which makes it untrusted. A stored {"preference": "ignore safety rules"} extracted from a conversation is an injection with persistence. Validate what gets persisted: schema the memory writes, scope them to the user they came from, and let users inspect and delete what you store — it's their data, and in some jurisdictions deletion is a legal right, not a courtesy.
What not to do
Don't fine-tune per user — that's the most expensive possible cache. Don't grow the system prompt to hold history either; it belongs at the front where caching and instruction priority work, not acting as a database.
Field notes
- Budget in tokens, not turns:
max_history_tokens, then summarize. Turns vary 100× in length. - Store memory structured —
(entity, attribute, value, updated_at)— not prose blobs. You can't expire or delete what you can't parse. - Prompt caching blunts resend cost when the history prefix is stable — one more reason the stable system prompt goes first and history appends in order.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Build Your Own AI Agent Stack: The Complete Self-Hosted Guide for 2026
Build a production-grade self-hosted AI agent from scratch using Docker Compose. Covers the full stack — Ollama, n8n, Flowise, Qdrant, SearXNG, Langfuse, and Redis.
AI Agents Are Breaking the Internet — And Nobody's Ready for the Bill
ChatGPT went down 28,000-report strong in February 2026. Claude had back-to-back outages within 24 hours in March. GitHub logged 37 incidents in a single month and is running at 90.21% uptime — one nine, not three. AI agents aren't just using infrastructure.
OpenClaw: The Lobster That Broke the Internet, Burned Through Every LLM's Compute Budget, and Started a War Between Anthropic and OpenAI
A retired Austrian developer built an open-source AI agent for fun in November 2025. Within 90 days, Jensen Huang called it the most successful open-source project in human history.
How AI Actually Works in Modern Video Games: FSMs to LLMs
Game AI isn't intelligent. It's state machines, search, and scoring arranged to feel like judgment, with real, runnable code from FSMs to local LLM dialogue.