Lesson 4 of 7
Agent memory
7 min read
Your agent nailed step 1. Forty steps later it's forgotten what it was even trying to do. Where did the goal go?
Short-term memory is the context window
An agent's working memory is just its context window — a fixed budget of tokens holding the goal, the history, and every tool result so far. Each step appends to it. Run long enough and the earliest turns — often the original goal — slide out to make room. The agent didn't "decide" to forget; it ran out of room.
Short-term memory is the context window. It's finite, it fills as the agent works, and the oldest turns fall out first. "Forgetting" is eviction, not a choice.
Long-term memory lives outside the window
So durable memory has to live outside the window. The pattern: write facts, results, and summaries to an external store, then retrieve only the relevant few back into context when needed. Compact old turns into summaries. Re-inject the goal each step. The window is RAM; your store is disk.
Real agent memory is engineering, not a bigger window: persist outside, retrieve the relevant slice, summarize the rest, and keep the goal pinned in view.
A cheap, high-leverage fix: restate the goal and key constraints at the end of the context every step. Models attend most to the start and end, so a pinned goal survives even as the middle history compacts.
The shape of it
- —Short-term memory is the context window — finite and filled by every step.
- —When it overflows, the oldest turns (often the goal) are evicted.
- —Long-term memory is an external store you write to and retrieve from.
- —Summarize old turns and re-pin the goal so it never scrolls away.
Your agent loses track of its objective on long tasks. Which fix actually addresses the cause?
Continue in the app
Take the whole AI Agents in Production course — tracked
Get your personalized path, progress and streaks in the app — this lesson and every next one, in order.