Skip to content

Lesson 3 of 9 · 23m

Context, State, and Memory Are System Design

Choose what enters the context window deliberately, persist application state outside the model, and avoid memory systems that become contamination systems.

Teams often treat the context window as a junk drawer: transcript, user profile, retrieved documents, tool results, system instructions, feature flags, and a prayer. The model does not experience these as neatly separated systems. It experiences one weighted stream of text. Good LLM applications decide what deserves attention, what belongs in durable state, and what must never be remembered.

Context is a cache with consequences

Every token competes for attention and money. Before adding data, ask what decision it changes. A customer plan may matter; a complete account history usually does not. Prefer compact derived state over raw history:

context = {
    "customer_plan": account.plan,
    "open_ticket_count": tickets.open_count(),
    "entitlements": sorted(account.entitlements),
    "recent_issue_summary": tickets.latest_summary(max_chars=500),
}

Notice that this is application state rendered into context. It is not the model's memory. Your database remains the source of truth.

Conversation state is not the transcript

Keep the transcript for display and audit, but do not assume the whole transcript belongs in every request. Build a state object for the workflow:

{
  "stage": "troubleshooting",
  "confirmed_order_id": "ord_8f42",
  "diagnostics_completed": ["power_cycle", "firmware_check"],
  "customer_frustration": "elevated",
  "requires_human": false
}

Update it deterministically where possible. Let the model propose state transitions only when language understanding is required. A workflow engine should still validate transitions, permissions, and preconditions.

Long-term memory is powerful and dangerous. A preference like prefers concise replies is different from once mentioned a medical issue. Use typed memory records:

{
  "kind": "preference",
  "key": "response_style",
  "value": "concise",
  "source": "explicit_user_setting",
  "expires_at": null,
  "confidence": 1.0
}

Do not let the model freely write to memory. Expose memory writes as reviewed application events or explicit user settings. Add deletion, export, retention, and tenant isolation from day one. Otherwise personalization quietly becomes a privacy incident.

Summaries drift; anchors do not

Rolling summaries compress cost, but they also accumulate mistakes. Preserve immutable anchors: IDs, confirmed decisions, dates, amounts, and policy constraints. A summary can say the customer is troubleshooting a router; it must not become the only place where ord_8f42 or a refund cap lives. A practical pattern is hybrid context:

system instructions
current workflow state (small, typed)
relevant retrieved facts (quoted or cited)
recent conversational turns (windowed)
current user message

Attention is not authorization

Putting a fact in context does not mean the model should act on it. If the user is not allowed to see a field, do not include it. If a document is retrieved only for citation, do not expose it to a write-capable agent loop. Context assembly is part of the authorization boundary, not merely a prompt optimization.

Field notes

  • More context can make answers worse by diluting the relevant evidence.
  • Dedupe repeated facts before sending them; repetition looks like emphasis.
  • Store token counts per context section so you can see what is eating the window.
  • Redact secrets before retrieval and logging, not after the model sees them.
  • Memory bugs are state bugs: test migration, deletion, expiry, and cross-user isolation.

Was this lesson helpful?

Sign in to rate