Build Your First AI Agent from Scratch · 35 min · Python · Anthropic Python SDK
Memory & Context Management
An agent doesn't 'forget' by choice — it forgets because you didn't manage what stays in the context window.
Hiring signal: Context management is the difference between a demo agent that falls apart on real, longer tasks and one that actually survives a full research session or coding task — this is one of the most asked-about production agent problems.
What you will learn
- Explain why every loop iteration grows the message history and why that eventually breaks the agent
- Implement a simple summarization step that runs before the context gets too large
- Decide what's worth keeping verbatim vs. what's safe to compress
Introduction
Run Lesson 3's agent on a task that takes 6-7 tool calls to finish (try something like "search for 3 different explanations of transformer attention and summarize the differences") and watch the messages list in your terminal output. Every single step appends the full tool result — including a fetch_url result you capped at 5000 characters — to a list that never shrinks. That's today's problem.
Why agents "forget" — they don't, you overflow
There's no forgetting mechanism in the API. What actually happens: every model has a maximum context window (how many tokens of conversation it can process at once). Once your accumulated messages list exceeds that, the API call fails outright — or, on some setups, older messages silently get truncated by whatever's calling the API, and the agent behaves as if it "forgot" something it clearly said three steps ago. Neither is graceful.
Tokens, not characters, and not "messages"
Context limits are measured in tokens (roughly 4 characters of English per token, but code and non-English text vary a lot). A single fetch_url result you didn't truncate can be tens of thousands of tokens by itself. This is exactly why Lesson 2's fetch_url caps output at 5000 characters — that wasn't an arbitrary limit, it was context management you'd already started doing without a name for it yet.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The fix: summarize before you overflow, not after you crash, What actually gets compressed, What You're Building — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy