Module 4: State & Graph Workflows · 2.5h
01 · UNDERSTAND
Day 18 Theory — State, Sessions, Persistence and Memory
Why agents need explicit state
A model call is temporary. It receives context, produces output, and ends. An application that needs continuity must store the information required for the next step.
That stored information is application state.
For the Service Desk, useful state may include the user request, ticket ID, tool results, approval status, current workflow stage, and conversation messages.
State is not the same as memory
These words are often mixed together.
- State: the information required to continue a workflow correctly.
- Session: a boundary that groups related interactions.
- Persistence: storing state beyond the current process or request.
- Memory: information intentionally retained so future interactions can benefit from it.
A ticket ID required to resume a workflow is state. A user's preferred language remembered for next month is memory.
In-memory state versus durable state
A Python dictionary is fast and simple, but disappears if the process restarts.
process memory -> restart -> gone
Durable storage survives restarts:
request -> state store -> process restart -> reload state -> continue
Choose persistence based on the workflow's recovery requirements.
Session identity
A session ID ties incoming requests to the right state.
The application must not trust a user-supplied session identifier blindly. Session lookup is also an authorization problem: a user must not be able to guess another user's session and load its contents.
Conversation history is only one kind of state
Chat examples often treat state as a list of messages. Real agents need more structured fields:
messages
current_ticket
selected_policy
approval_required
tool_results
retry_count
Structured state makes the workflow easier to inspect and test.
Memory should be selective
Remembering everything is not automatically better.
Long-term memory can create:
- privacy risks,
- stale facts,
- incorrect personalization,
- larger retrieval/context cost.
Store only information with a clear product purpose, retention policy and deletion path.
Source of truth
Do not copy authoritative business facts into agent memory when a real source exists.
For example, an employee's current access role should come from the identity system, not from a memory record written weeks earlier.
Memory is useful for context. Authoritative systems remain authoritative.
Service Desk connection
Today the Service Desk becomes resumable. It can distinguish a new interaction from a continuation and persist the state needed to recover after process failure.
The principle is:
Persist the minimum state required to resume correctly, separate workflow state from long-term memory, and keep authoritative facts in authoritative systems.
02 · APPLY
Lesson Overview
This is the applied companion for Day 18. Read DAY_18_THEORY.md first for the beginner-first teaching of State, Sessions, Persistence and Memory. Then use the real service-desk-day-18/ project to trace, run, debug, and explain the concept.
Service Desk Alignment
Day 18 adds State, Sessions, Persistence and Memory to the running Service Desk. Start with session_manager.py, agent.py, storage.py, serializer.py, models.py, then follow imports and tests to identify the actual runtime path.
Why This Topic Matters
The theory chapter explains why State, Sessions, Persistence and Memory is needed. Here the goal is evidence: identify where the capability is implemented, what observable behavior changes, and how the repository proves both success and failure behavior.
System Architecture
This is a repository surface map, not a claim that modules call each other in the displayed order. The modules are ranked by relevance to today's theory.
graph LR
T[Day 18: State, Sessions, Persistence and Memory]
T --> M1[session_manager.py]
T --> M2[agent.py]
T --> M3[storage.py]
T --> M4[serializer.py]
T --> M5[models.py]
Follow imports and tests to discover the actual runtime flow.
Repository Implementation Map
Use the real Day 18 repository, not a fabricated sample, to connect theory to implementation.
Theory concepts to locate:
- Why agents need explicit state
- State is not the same as memory
- In-memory state versus durable state
- Session identity
Most relevant implementation modules first:
service_desk/session_manager.pyservice_desk/agent.pyservice_desk/storage.pyservice_desk/serializer.pyservice_desk/models.py
Follow imports/calls from the relevant module and confirm behavior in tests. Record input → mechanism → observable output/state → failure evidence.
Code Walkthrough & Mechanics
Read session_manager.py, agent.py, storage.py, serializer.py, models.py with these theory sections beside you:
- Why agents need explicit state — locate its implementation and evidence.
- State is not the same as memory — locate its implementation and evidence.
- In-memory state versus durable state — locate its implementation and evidence.
- Session identity — locate its implementation and evidence.
- Conversation history is only one kind of state — locate its implementation and evidence.
For each concept identify the real function/class/protocol boundary, its input/state, its observable result, and the assertion in tests/test_state_persistence.py that proves the behavior. If a concept has no implementation or evidence, record that as a gap rather than inventing one.
Common Mistakes & Debugging Guidance
Use the theory—not generic timeout or .env advice—to decide what can fail today.
Failure lens: revisit Session identity, Conversation history is only one kind of state.
- Reproduce the smallest case that violates one of those expectations.
- Trace the real Day 18 modules until you find the first incorrect state/output/decision.
- Use
tests/test_state_persistence.pyas executable evidence. - Add a regression test if the failure is not already represented.
- Fix the smallest responsible boundary and rerun the relevant test before the full suite.
Your debugging explanation must name the topic-specific invariant that failed, not merely say “an exception occurred.”
Practical Lab Instructions
- Summarize these theory ideas before opening the implementation:
- Why agents need explicit state
- State is not the same as memory
- In-memory state versus durable state
- Session identity
- Inspect the most relevant real Day 18 modules first:
service_desk/session_manager.pyservice_desk/agent.pyservice_desk/storage.pyservice_desk/serializer.pyservice_desk/models.py
- Inspect the automated evidence:
tests/test_state_persistence.py
- Establish the baseline:
cd service-desk-day-18 PYTHONPATH=. pytest tests/test_state_persistence.py -q - Trace one theory concept through the actual nested modules and tests.
- Run one success case and record input → mechanism → observable result.
- Exercise one topic-specific failure/boundary case and name the invariant that protects the system.
- Re-run the relevant tests and explain theory → implementation → evidence.
Done when: another student can reproduce your trace without relying on an invented sample.
Key Takeaways
- Be able to explain Why agents need explicit state and point to its implementation/evidence in Day 18.
- Be able to explain State is not the same as memory and point to its implementation/evidence in Day 18.
- Be able to explain In-memory state versus durable state and point to its implementation/evidence in Day 18.
Knowledge Check & Scenario Questions
- Concept: Using Why agents need explicit state, explain the engineering problem Day 18 is solving without naming a framework as the answer.
- Mechanism: How does State is not the same as memory appear in the real project? Start from
service_desk/session_manager.pyand name the observable state/output/event that changes. - Failure: For In-memory state versus durable state, describe one incorrect implementation or boundary condition and the evidence you would expect in
tests/test_state_persistence.py. - Design review: Which assumption in today's design would you verify before reusing this implementation in a different production system?
Official References
- SQLite Official Documentation: https://www.sqlite.org/docs.html
- LangGraph Persistence & Checkpointers: https://langchain-ai.github.io/langgraph/concepts/persistence/
03 · EXPLAIN
Interview checkpoint
Explain one design decision from this lesson, the alternative you rejected, and the failure mode or evidence that justified your choice.