Managing State Across Interactions › State Size Management

Michael BrenndoerferJuly 11, 20251 min read
AI Agent HandbookAll chapters
Part 7 of 15Agent State and Architecture - Putting It All Together
Chapter 20Managing State Across Interactions › State Size Management
Reading section 7 of 13 · State Size Management · 1 minSection 7 of 13 · 1 min

Book contents

Section 7 of 13 · 1 min read

Managing State Across Interactions › State Size ManagementOne section · Sections in this chapter7 / 13 ↓

As your agent runs over time, state can grow unbounded. A user who's had 1,000 conversations will have a massive conversation history. You need strategies to keep state manageable:

For Session State: Sliding Window

Keep only the most recent N messages:

In[11]:
Code
def add_to_history(self, role: str, content: str):
    """Add message with automatic pruning"""
    self.session_state["conversation_history"].append({
        "role": role,
        "content": content,
        "timestamp": datetime.datetime.now().isoformat()
    })
    
    # Keep only last 20 messages
    if len(self.session_state["conversation_history"]) > 20:
        self.session_state["conversation_history"] = \
            self.session_state["conversation_history"][-20:]

For Persistent State: Summarization

Instead of storing every conversation, store summaries:

In[12]:
Code
def summarize_session(self) -> str:
    """Create a summary of the session for persistent storage"""
    # In a real system, you'd use the LLM to generate this
    messages = self.session_state["conversation_history"]
    return f"Session with {len(messages)} messages about {self.session_state.get('current_goal', 'various topics')}"

For Ephemeral State: Automatic Cleanup

Clear ephemeral state aggressively since it's only needed for the current task:

In[13]:
Code
def cleanup_ephemeral_state(self):
    """Remove old ephemeral data"""
    # If a task has been running for more than 10 minutes, something's wrong
    if "task_start" in self.ephemeral_state:
        elapsed = datetime.datetime.now() - self.ephemeral_state["task_start"]
        if elapsed.total_seconds() > 600:
            self.ephemeral_state = {}