Skip to chapter content
navan✳THE BUILD LABAI ENGINEERING / GUIDE
0/21 read
CHAPTER 14 / 21 · KNOWLEDGE AND ACTION

Memory and conversation state.

Distinguish recent turns, durable preferences, retrieved facts, and the risks of carrying context forward.

BY NAVAN SAMILONG READTHEORY FIRST
01 / FROM THE BEGINNING

Start with the meaning.

A chat interface makes an AI system feel continuous, but a model call only sees what the application supplies for that call. The system may send recent messages, a summary of older messages, saved preferences, and retrieved documents. These are different kinds of context with different lifetimes and trust levels.

Recent turns help resolve references such as “that document.” Durable memory might store a user's preferred language. Retrieved facts come from a source outside the conversation. Mixing them without labels can cause mistakes: a temporary instruction may become a permanent preference, or an old answer may be treated as a current fact.

02 / THE SYSTEM

How the idea works.

Design memory around a purpose and retention rule. Which fact is needed next time? Who can edit or delete it? How long is it valid? A summary is useful for keeping context short, but it can distort a qualification. Keep important exact values, permissions, and source links as structured data rather than relying only on a prose summary.

Conversation history can contain malicious or accidental instructions. A user can quote a document that says “ignore previous rules”; the application must represent that as quoted content. A stored memory can also be wrong. Allow users to see and correct durable preferences, and keep an audit trail for consequential changes.

Context selection is an information retrieval problem. Do not send every old turn merely because it exists. Choose recent relevant turns, approved durable facts, and current evidence. When uncertain, ask the user to clarify rather than inventing a reference.

Memory is easy to overpromise. If a user says “remember that I prefer short answers,” a saved preference may help later. If they say “the invoice was paid yesterday,” that is a claim about a changing external fact and should not quietly become permanent truth. Store a preference with a clear scope and allow correction; verify changing facts from their source when they matter. A summary of older turns is useful for continuity, but it can lose uncertainty or context.

A strong state design keeps categories separate: recent conversation for references, durable preferences for recurring presentation choices, retrieved records for current facts, and task state for work in progress. Each category has a different owner and retention period. When the system assembles a new call, it chooses only what this task needs. That choice improves privacy, cost, and clarity at the same time.

01PlanProposed step02ValidateTool and access03ExecuteBounded action04ObserveResult or stop
Tool calls need validation, permissions, budgets, and a stopping rule.
03 / PYTHON, EXPLAINED

Follow the code.

conversation-state.py
def build_turn(current, recent, preferences):
    allowed = {k: v for k, v in preferences.items() if k in {"language", "format"}}
    return {
        "preferences": allowed,
        "recent_messages": recent[-4:],
        "current_request": current,
    }

The function includes only two approved preference fields and the last four messages. That is a simple context selection policy. It keeps current_request separate so the caller knows which text is the new instruction. The code does not claim four turns is universally correct; it is a visible choice that can be evaluated.

A real system would enforce user ownership of preferences and decide how to handle long recent messages. It would also avoid storing secrets in a summary without a clear reason and retention policy.

04 / THE BOUNDARY

What the result does not say.

Memory should improve the user's task, not make the application feel mysteriously omniscient. Explain what is retained and offer control over it.

When a past fact affects a consequential answer, recover the original source or ask again. A conversational summary is not a reliable source of record.

05 / KEEP THIS

One clear idea.

Memory is selected, governed context with a purpose, not an unlimited transcript.

THE CODE IN CONTEXT

The AI cookbook

Complete, explained local workflows sit alongside this reading course.

Browse the cookbook
Finished reading?
Built with curiosity. By Navan Sami. ↗AI engineering · From model calls to reliable systems.