Agentic AI Engineering · Module 04 of 16

State & Memory

A multi-step agent must remember what it is doing right now. That task state is different from conversation history, and both are different from information intentionally retained for future sessions.

Engineering focus: Working state, conversation context and durable memory

In plain English

What problem are we solving?

A multi-step agent must remember what it is doing right now. That task state is different from conversation history, and both are different from information intentionally retained for future sessions.

Framework names come later. First understand what the application is doing and where its control boundaries belong.

Running case · Operations Investigator

Across the course, imagine an assistant that investigates operational problems using narrow tools. The design progressively adds plans, specialist agents, approvals, reliability controls and MCP while keeping model output separate from execution authority.

Learning objectives

By the end of this module, you should be able to:

  • Separate working state, conversation history and durable memory.
  • Explain why model context is not the same as application state.
  • Preserve task facts across a multi-step interaction.

Working state is task-scoped

Facts such as the current goal, host, service, plan and recent tool result belong to the active task. Losing them causes the agent to repeat questions or act on the wrong target.

Conversation history is not a complete state model

Dialogue can contain useful facts, but production applications often need explicit structured state that can be validated, updated and passed to tools without re-parsing prose.

Durable memory has a lifecycle

Long-lived information should be retained deliberately, not because it happened to appear in a chat. Durable memory raises questions about purpose, access, correction, expiry and deletion.

Context is selected, not dumped

The application should choose the information needed for the current model decision. Sending every stored fact increases cost and can distract the model.

Worked examples

Task state

state = {
  "goal": "investigate service",
  "host": "web01",
  "service": "nginx"
}
  • These facts are needed for the current investigation.
  • They do not automatically belong in permanent memory.

Context selection

stored information
      ↓
select relevant facts
      ↓
model context for this decision
  • Application state can be larger than model context.
  • Selection should be deliberate and traceable.
Try it · core lab · account-free

Preserve and deliberately break working state

Goal: See a multi-step task succeed with explicit state and fail when a required fact is removed.

Before you start

  • Python 3 only. No model or database is required.
STEP 1

Run the state lab

python3 public/labs/agentic-ai/04_state_and_memory.py
STEP 2

Follow the successful path

The application retains host=web01 and adds service=nginx.

STEP 3

Break the state

The lab removes host and demonstrates why the next step has to ask again.

Verify

  • The complete state produces CHECK_SERVICE host=web01 service=nginx.
  • Deleting host reproduces the expected ASK_HOST failure.

Expected outcome

  • You can distinguish task state from durable memory.
  • You understand why state should be explicit rather than inferred from arbitrary conversation text.

Break / fix · diagnose

  • If the failure does not reproduce, verify that host was actually removed from the copied state.
  • If the successful path asks for the host again, inspect when state is updated between turns.
Production view

Production agent memory should be purpose-limited, scoped and observable. State stores, session stores and durable memory have different retention and security requirements.

Avoid these traps

Common mistakes

  • Calling all conversation history “memory”.
  • Persisting every task fact forever.
  • Sending the entire database into model context.
  • Adding a vector database before defining what information actually needs to persist.
Knowledge check

Explain the control decision, not just the vocabulary.

Open every answer only after committing to your own answer. Opening all four answers records knowledge-check activity in My Learning.

1The current host and service being investigated belong primarily to what?

Working state — They are task-scoped facts needed during the current run.

2Why is conversation history not the complete state model?

Because useful state can include structured task facts and tool results not represented well as dialogue — Applications often need explicit structured state.

3Which statement is correct?

The application should select relevant context for the current decision — Context selection should be deliberate and task-relevant.

4What failure is demonstrated when the agent forgets web01 and asks for it again?

Loss of required working state — The task cannot continue correctly because a required task fact was lost.

My Learning

Record your progress for this module.

Progress is saved in this browser immediately. When you sign in to My Learning, it can also sync across devices.

Not started

Attempt the core lab and knowledge checks before marking the module complete.