Managing State Across Interactions › Types of State by Lifecycle
Michael BrenndoerferJuly 11, 20252 min read
AI Agent HandbookAll chapters Part 7 of 15Agent State and Architecture - Putting It All Together
Managing State Across Interactions › Types of State by LifecycleOne section · Sections in this chapter3 / 13 ↓
Sections in this chapter13
Let's categorize state by how long it should live:
Ephemeral State (Seconds to Minutes)
This is working memory for the current task. It includes:
- Intermediate results from tool calls
- The current step in a multi-step plan
- Temporary calculations or data
Lifecycle: Created when a task starts, cleared when the task completes.
In[3]:
Codeclass Agent:
def __init__(self):
self.ephemeral_state = {}
def start_task(self, task_description):
# Clear previous task's working memory
self.ephemeral_state = {
"task": task_description,
"steps_completed": [],
"current_results": {}
}
def complete_task(self):
# Task done, clear ephemeral state
self.ephemeral_state = {}Session State (Minutes to Hours)
This is the conversation context for the current session. It includes:
- Recent conversation history
- The user's current goal or topic
- References that make sense "right now"
Lifecycle: Created when the user starts interacting, cleared when the session ends (app closes, timeout, explicit reset).
In[4]:
Codeclass Agent:
def __init__(self):
self.session_state = {
"conversation_history": [],
"session_start": None,
"current_topic": None
}
def start_session(self):
import datetime
self.session_state = {
"conversation_history": [],
"session_start": datetime.datetime.now(),
"current_topic": None
}
def end_session(self):
# Optionally save important parts before clearing
self.save_session_summary()
self.session_state = {
"conversation_history": [],
"session_start": None,
"current_topic": None
}Persistent State (Days to Forever)
This is long-term memory that survives sessions. It includes:
- User preferences and settings
- Important facts the user told you
- Historical data that might be relevant later
Lifecycle: Created when learned, persists indefinitely (or until explicitly deleted/updated).
In[5]:
Codeclass Agent:
def __init__(self):
self.persistent_state = self.load_from_storage()
def load_from_storage(self):
# Load from database, file, etc.
return {
"user_preferences": {},
"known_facts": {},
"history_summary": []
}
def save_to_storage(self):
# Persist to database, file, etc.
pass
def update_preference(self, key, value):
self.persistent_state["user_preferences"][key] = value
self.save_to_storage()