Memory and context
Two kinds of memory
Because a model is stateless, agents use two different ways to "remember":
- Short-term memory (context). Everything in the current run: the instructions, the input, each tool call and its result. It lives in the context window and disappears when the run ends.
- Long-term memory. Notes the agent deliberately saves so a later run can find them, such as "the customer prefers short replies" or "last week's report mentioned the outage."
Long-term memory is not magic. It is text saved somewhere, searched when needed, and pasted back into the prompt. That is why it can be wrong, stale or too long, and why you want to be able to read and delete it.
Why context needs managing
The context window is finite, and every token costs money and time. Two habits help:
- Bring in only what matters. A tool that returns an entire web page when the agent needs one paragraph wastes context. Clear instructions about what to look for help the agent ask for less.
- Prefer a short, current summary to a long history. An agent that keeps only the facts that still matter is cheaper and less confused than one that drags everything along.
There is also a safety angle. Anything an agent reads becomes part of its prompt, including text written by strangers. You will see in lesson 7 why that is a risk.
What belongs in memory
Good candidates: stable preferences, decisions, names of recurring things, and a one-line summary of the last run. Poor candidates: secrets, personal data you do not need, and anything that goes out of date quickly. Treat memory like a shared notebook, not a vault.
How this looks in PGB Agents
The Memory tools, memory.save and memory.recall, let an agent keep notes between runs. The notes appear in the Memory panel on the agent page, and Forget removes any one of them. Recall works by keyword search, so notes should be written with clear, searchable words. Without the memory tools ticked, every run starts with a clean slate.
Try it yourself
- Open "Concept lab" and tick
memory.saveandmemory.recallunder Memory. - Add this line to the instructions: "If the input says 'remember', save the fact with memory.save. Otherwise, check memory.recall for relevant notes before answering." Save.
- Run it with the input "Remember: my favourite colour is green." Check the Memory panel for the new note.
- Run it again with the input "Suggest a colour for my notebook." See whether the answer uses the note, and find the
memory.recallstep in the trace. - Choose Forget on the note and run the second input again. The agent no longer knows.
Recap
- Context is short-term and lives inside one run; memory is saved notes brought back into later prompts.
- Both are just text in the prompt, so keep them small, relevant and free of secrets.
- In PGB Agents, notes live in the Memory panel and you can Forget any of them.
Open Access Course
Free
forever- 8 structured lessons across 4 modules
- Interactive knowledge checks with instant feedback
- Verifiable completion certificate & PDF