liveBook · Manning

Chapter 5: Memory and state in agentic systems

What you will learn in chapter 5 of Context Engineering

The introduction identifies memory and state as context sources an LLM-based system maintains across turns, distinguishing retained information from current status.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction identifies memory and state as context sources an LLM-based system maintains across turns, distinguishing retained information from current status.

Key terms: memory, short-term session context, long-term persistent records, state, dynamic status

5.1 Memory

This section explains how memory gives agents continuity, distinguishes model-embedded from external memory, and maps external storage to the context window.

Key terms: memory, tools, continuity, parametric memory, non-parametric memory, context window, persistent storage, ephemeral storage, load, promotion

5.1.1 Short-term memory

This section defines short-term memory as a single session’s history and explains how the agent application replays, limits, and eventually discards that history.

Key terms: short-term memory, working memory, contextual memory, session, agent harness, stateless, context window, token budget, session object, thread

5.1.2 Long-term memory

Explains how long-term memory gives agents continuity across sessions, what its three types store, and how entries change over time.

Key terms: long-term memory, persistent memory, context window, explicit retrieval, semantic memory, episodic memory, procedural memory, active lifecycle, superseded, consolidated

5.1.3 Memory vs state

This section distinguishes short-term memory, long-term memory, and state by their roles, then shows how agent frameworks represent those concepts differently.

Key terms: short-term memory, long-term memory, state, operational variables, thread state, session events, searchable memory service, dynamic session state

5.2 Memory management

Defines memory management as the part of context management that controls what an agent retains and reuses, emphasizing selection over storage capacity.

Key terms: memory management, context management, earlier turns, earlier sessions, selection, storage capacity, token budget, expanding contexts

5.2.1 Memory selection

This section explains how an agent selects stored memories for the context window using recency, semantic relevance, and explicit keys.

Key terms: memory selection, context window, recency, similarity, explicit key, sliding window, short-term memory, conversational turns, tool calls, observations

5.2.2 Memory compression

This section explains how agents condense interaction history, the summary patterns they use, the risk of drift, and compaction in coding agents.

Key terms: memory compression, summarization, verbatim transcripts, distilled summaries, rolling summaries, hierarchical summaries, hybrid summaries, key-value attributes, context drift, editable artifacts

5.2.3 Memory writing

This section compares when agents write extracted information to memory and explains why stored memories need periodic consolidation.

Key terms: memory writing, hot path, background, response latency, consolidation, entity-resolution models, confidence scores, superseded facts, retrieval accuracy

5.2.4 Failure modes and safeguards

This section explains how long-term memory can fail when untrusted or outdated information is retained, and the governance safeguards that reduce those risks.

Key terms: memory poisoning, untrusted input, long-term storage, provenance verification, scoped namespaces, database partition keys, write filters, extraction guardrails, provenance tracking, verification mechanisms

5.2.5 Memory management tools

This subsection explains why pre-built memory modules can simplify agent development and briefly distinguishes memory from workflow state.

Key terms: memory pipelines, pre-built memory modules, storage, retrieval, summarization, memory, state, historical data, active variables

5.3 State

Defines state as externally maintained working data about an agentic system’s current operational status and introduces the state object that organizes it.

Key terms: state, short-term memory, agent harness, state object, context orchestrator, context manager

5.3.1 Categories of state

This section classifies the variables in an agent’s state object and explains why the categories help developers design state schemas.

Key terms: user state, system state, dialogue state, environmental state, multi-agent state, explicit schemas, state updates, predictable, auditable

5.3.2 State scoping

This section explains how to divide workflow state into private local state and shared global state so collaborating agents see and update only what they need.

Key terms: state scoping, local state, global state, shared state, schemas, shared workflow deliverables, agent-specific data, transient scratchpad, prompt assembly, blackboard architecture

5.3.3 Multi-agent state architectures

Explains two ways to arrange state across multiple agents and the trade-offs between them.

Key terms: agent runtime, shared state object, global state, local state, common state contract, isolated state object, handoff payload, visibility, consistency, tight coupling

5.4 State management

Introduces five state-management operations that the following subsections will explain.

5.4.1 State representation

This section explains three ways agent systems represent operational state in code: typed schemas, structured scratchpads, and append-only event streams.

Key terms: state representation, typed schemas, structured scratchpads, append-only event streams

5.4.2 State update

This section explains why agent state updates must be deterministic and describes three ways to apply them.

Key terms: state update, deterministic, race conditions, invalid state transitions, reducers, channel handlers, application-level post-processing, model-generated state patches, harness

5.4.3 State propagation

This section explains two ways information moves between agents: through shared state or through a handoff payload.

Key terms: state propagation, shared state document, context payloads, agent handoffs

5.4.4 State selection and projection

This section explains how to select task-relevant state for a prompt and tailor that selection to an agent’s role.

Key terms: state selection, state projection, global state, prompt projection, role-scoped views, planner, executor, reviewer

5.4.5 State persistence and checkpointing

This section explains how workflow state is checkpointed and how saved state supports recovery, human approval, debugging, and coordination.

Key terms: state persistence, checkpointing, checkpointer, graph state, thread identifier, checkpoint identifier, human-in-the-loop execution, agent harness, time-travel debugging, persistent checkpoints

5.5 Hands-on

5.5.1 Long-term memory with Mem0 and Qdrant

This section illustrates how a stateless chat application uses Mem0 and Qdrant to retrieve and persist long-term conversational memory.

Key terms: stateless, memory layer, Mem0, Qdrant, instructions, long-term memory, retrieval, memory ingestion, tokens

5.5.2 Graph-based long-term memory with Cognee

This section uses a minimal Cognee exercise to show how raw text becomes graph-based long-term memory that can be searched alongside a vector index.

Key terms: cognify(), search(), chunks, embeds, entities, relations, structured memory representation, corpus, prompt, fragment matching the query

5.5.3 Short-term memory curation with the OpenAI Agents SDK

This section uses a schematic OpenAI Agents SDK session to show how a short-term memory policy curates interaction history for each agent run.

Key terms: short-term memory, curation, last N user turns, running summary, Session, Runner.run(), interaction history, curated context

5.5.4 Session state with a structured scratchpad

Shows how application code maintains a structured, session-local state snapshot and passes it to a single agent on each turn.

Key terms: build_system_prompt(), update_state(), system message, session state, local state, dataclass, /state

5.5.5 Shared workflow state between planner and executor

This section illustrates how a planner and executor coordinate through shared, persistent workflow state.

Key terms: global state, planner agent, executor agent, JSON-backed workflow state, handoff

5.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Tools in AI agents Chapter 6: User prompts for LLMs →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.