How to build · Lesson 4 of 5 · 6 minutes

Manage context and memory

The model can use only the context it receives. Choose that context deliberately, and treat persistence as application state with a lifecycle.

Working context is not long-term memory

Working context is the information sent with a particular model turn: instructions, recent messages, tool results, and retrieved records. Persistent memory is application data retained across turns or sessions. A database is not automatically useful memory; the harness still needs a policy for what to write, retrieve, update, and forget.

Choose the right place for information
InformationLikely placeReason
Current test outputWorking contextNeeded for the next decision; usually short-lived
Approved user preferenceScoped persistent memoryUseful across sessions if the user consents
Large catalogExternal store + search toolRetrieve a small relevant subset instead of sending all rows
API secretSecret manager, never model contextCredentials should not be exposed to model input or ordinary traces

Use a retrieval policy

When the conversation grows, do not blindly append everything forever. Keep the task goal, constraints, latest relevant observations, and decisions needed to continue. Summarize or archive older details carefully, and retrieve external records when the current task calls for them. A summary is a lossy transformation: keep links or source IDs so important claims can be checked against the original.

Tool outputs should be concise and structured. A search tool should return relevant matches, not the whole index. A file tool can return a targeted range. This reduces noise and leaves context budget for the model’s next decision. Anthropic’s guidance on effective context engineering and effective agent tools explains why selection and response design matter.

Make memory inspectable and forgettable

For each persistent record, define its owner, scope, source, write rule, expiration, and deletion path. Prefer an explicit schema such as {"key": "preferred_language", "value": "Python", "source": "user-confirmed", "updated_at": "…"} over opaque narrative blobs. Do not store secrets or sensitive data just because the model encountered them.

Memory can be stale or wrong. Include provenance and timestamps, ask for confirmation when an important preference conflicts with new evidence, and allow the user to inspect and correct stored state. If a task does not need cross-session persistence, do not add it.

Design prompt: If the agent forgets this information at the end of the run, will the next run fail? If not, keep it in run-local context. If yes, identify a specific, user-understood record and its retention rule.
What is a sound default for persistent memory?

Primary source: Effective Context Engineering for AI Agents — Anthropic.

Ask your teacher to help design a memory schema and retention policy for your use case.