DEV Community

Sangmin Lee
Sangmin Lee

Posted on • Originally published at claudeguide.io

Memory and State in Claude Agents: Patterns That Scale

Originally published at claudeguide.io/claude-agent-memory-patterns

Memory and State in Claude Agents: Patterns That Scale

Claude agents don't have persistent memory between API calls — each call starts fresh. Adding memory means deciding what to store, where to store it, and how much to bring back into context on the next call. The four patterns that cover 90% of production needs are: conversation history (in-context), summary compression (compressed context), external memory (vector search), and explicit state (structured data). This guide covers when to use each and how to implement them.


The Memory Problem

# Call 1
client.messages.create(messages=[{"role": "user", "content": "My name is Alex"}])
# Claude: "Hi Alex!"

# Call 2 — Claude has no memory of call 1
client.messages.create(messages=[{"role": "user", "content": "What's my name?"}])
# Claude: "I don't know your name."
Enter fullscreen mode Exit fullscreen mode

Every conversation must carry its own context. The question is how much and in what form.


Pattern 1: Full Conversation History (In-Context)

The simplest approach — append every turn to a running messages list.


python
import anthropic

client = anthropic.Anthropic()


class ConversationAgent:
    """Agent that maintains full conversation history in context."""

    def __init__(self, system: str, max_turns: int = 50):
        self.system = system
        self.messages = []
        self.max_turns = max_turns

    def chat(self, user_message: str) -

[→ Get the Agent SDK Cookbook — $49](https://shoutfirst.gumroad.com/l/ogxhmy?utm_source=claudeguide&utm_medium=article&utm_campaign=claude-agent-memory-patterns)

*30-day money-back guarantee. Instant download.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)