Agent State and Memory
What You'll Learn
You'll learn how AI agents maintain internal state to track conversation history, observations, and intermediate results across multiple steps.
Why Agents Need Memory
Unlike a simple LLM call that processes one input and returns one output, an agent operates over multiple turns. It needs to remember:
- What it's already tried
- What information it has gathered
- The conversation history with the user
- Intermediate reasoning steps
- Which tools it has called and their results
Without memory, an agent would repeat itself, forget context, and fail to build on previous observations — like a detective who forgets every clue immediately after finding it.
Types of Agent State
Short-term (working) memory stores the current episode:
- The original user query
- Retrieved documents from this session
- Tool outputs from recent actions
- The chain of reasoning so far
Long-term memory persists across sessions:
- User preferences learned over time
- Summaries of past conversations
- Historical success/failure patterns
Think of it like a doctor's appointment: short-term memory is everything discussed today, while long-term memory is your medical history file.
Implementation Patterns
Agent state is typically stored as a structured object or dictionary that gets updated after each action. For example:
conversation_history: list of messagesobservations: facts gathered this sessiontools_used: which tools were calledcurrent_goal: what the agent is trying to accomplish
The agent reads this state before deciding its next action and writes to it after executing each step.
Key Takeaway: Agent state and memory enable multi-step reasoning by preserving context, observations, and history across the perception-action loop — transforming stateless LLM calls into stateful, goal-directed behavior.