Skip to main content
Time for a lab. In this lesson you’ll add long-term persistence to an agent so preferences survive after the terminal closes. The pattern is simple:
  • Save user preferences to a JSON file on disk.
  • Load that JSON at startup.
  • Inject the loaded preferences into the system prompt so the model has awareness of previously saved state.
Environment & assumptions
  • Python and a virtual environment are available.
  • The OpenAI Python SDK is installed.
  • Your working directory is /root/code.
  • This walkthrough creates a single file: persistent_agent.py.
A retro, pixel-art UI screen that says "ADD PERSISTENT MEMORY" with the subtitle "Give your agent long-term memory" and a small "HANDS‑ON LAB" badge. It shows neon buttons labeled SAVE, LOAD, and INJECT on a dark grid background.
What you’ll build
  • A safe-agent pattern with a simple check_calendar tool and a try_call_tool wrapper.
  • A persistent memory layer backed by agent_memory.json.
  • save_preference and forget_preference handlers.
  • System prompt injection of loaded preferences at startup.
  • A small main loop demonstrating the flow.
This lab focuses on the memory pattern and safe tool execution. In production, replace the local JSON file with a secure database and ensure sensitive information is encrypted.
Step-by-step implementation
  1. File and client setup Create persistent_agent.py and start with imports, client initialization, and constants:
  1. Safe tool pattern Define a simple calendar “tool” and a try_call_tool helper that catches exceptions and returns a safe string. This keeps the agent robust when tools fail.
  1. Persistent memory layer Add the functions to load/save JSON and the two preference handlers: save_preference and forget_preference. These read/write agent_memory.json and return human-readable status strings for the agent.
  1. Optional: expose tool metadata If your agent framework expects a tool registry, provide metadata for save_preference and forget_preference. This is optional but helpful for structured agent orchestrators.
  1. Inject memory into the system prompt Load the memory at startup and append known preferences to the system prompt. This ensures the model is aware of persistent state without requiring the user to repeat preferences.
  1. Main loop: demonstration of flow A minimal main loop shows the overall pattern: build the system prompt (with memory injected), process messages, call tools safely, and save/forget preferences as appropriate. This example simulates agent behavior and is intentionally straightforward to illustrate the pattern.
Running the script
  • Run with: python3 persistent_agent.py
  • Observe the printed system prompt (memory is injected) and the simulated session.
Example session output:
Persisted memory file After the first run, check agent_memory.json to confirm persistence:
Run the script again (or change the simulated messages) to see the system prompt print the loaded preferences:
Final notes and production guidance
Storing sensitive user data or secrets in plaintext JSON on disk is not secure. For production systems, use an encrypted store or a database with proper access controls and encryption at rest.
  • The pattern shown here is portable: swap agent_memory.json for a database or key-value store, and replace the simulated check_calendar tool with a real calendar API.
  • Consider schema validation for preferences and limits on memory size to avoid letting the system prompt become too large.
Quick reference table Links and references Save, load, forget — that’s the persistent-memory layer. Adapt this pattern to your infrastructure and production-grade storage for robust, long-term agent memory.

Watch Video