User as Code: Executable Memory for Personalized Agents
This paper introduces User as Code (UaC), a paradigm that represents personalized agent memory as executable Python code rather than unstructured text, enabling superior performance in aggregating historical data, resolving contradictions, and proactively enforcing safety rules through deterministic computation.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Big Idea: From a "Shoebox of Notes" to a "Living Software App"
Imagine you are trying to remember everything about a friend, Jessica.
The Old Way (Current AI Memory):
Right now, most AI agents store memories like a shoebox full of sticky notes.
- You write down facts: "Jessica likes cilantro," "Jessica hates cilantro," "Jessica is allergic to penicillin."
- When you ask the AI, "What is Jessica allergic to?" it rummages through the box, finds the note about penicillin, and reads it back.
- The Problem: If you ask, "How many international trips did Jessica take last year?" the AI has to read every single note, count them one by one, and do math in its head. It often gets lost, forgets a note, or misses the fact that "Amoxicillin" is a type of "Penicillin" because those words never appeared together on the same note. It's like trying to do complex accounting by just reading a pile of receipts.
The New Way (User as Code):
The authors propose a new system called User as Code (UaC). Instead of a shoebox of notes, the AI builds a living software app specifically for Jessica.
- The State (The Database): Instead of notes, the AI creates typed "objects" (like digital folders). There is a
Passportfolder, aTripsfolder, and aMedicalfolder. Inside the Medical folder, it doesn't just write "Penicillin allergy"; it creates a structured entry that says:Drug Class: Penicillin,Severity: Severe. - The Rules (The Logic): The AI also writes little computer programs (functions) that run automatically. One program might say: "If a new medicine is prescribed, check if its drug class matches any existing allergies."
How It Works: The Two-Phase Factory
The paper describes a two-step process to turn messy conversations into this clean software:
Phase 1: The Scribe (Memorize)
Every time Jessica talks to the AI, a "Scribe" listens and writes down every single fact as a raw, unedited list. It never deletes anything. If Jessica says, "I saw Dr. Park for allergies," the Scribe writes:[Date] User saw Dr. Park for allergies.- Analogy: This is like a court reporter typing out a trial. Nothing is lost, nothing is summarized yet.
Phase 2: The Architect (Structure)
Periodically, an "Architect" (a smart AI) reads the entire list of raw facts and rewrites them into the clean software app. It organizes the facts into thePassportandMedicalfolders and writes the little programs (constraints) to check for conflicts.- Analogy: This is like a librarian taking a pile of unsorted books and organizing them into a perfect, searchable catalog with a computer system that can instantly calculate statistics.
Why This Changes Everything
The paper claims this shift allows the AI to do three things that the "shoebox" method struggles with:
1. The "Math" Problem (Analytical Inference)
- The Scenario: You ask, "How many international trips did Jessica take in 2025?"
- Old Way: The AI has to search for "trip" notes, guess which ones are international, count them, and hope it didn't miss any. It often fails (getting only 43% right in tests).
- New Way: The AI just runs a simple line of code:
Count all trips where year is 2025 and is_international is True.It gets the answer instantly and perfectly (99% accuracy). It's the difference between counting a pile of coins by hand versus using a machine that knows exactly how many are in the jar.
2. The "Proactive" Problem (Active Service)
- The Scenario: Jessica mentions a penicillin allergy in January. In October, she mentions taking Amoxicillin. She never asks, "Is this safe?"
- Old Way: The AI waits for a question. It won't connect the dots unless you ask, "Does Amoxicillin affect my allergies?"
- New Way: The "Architect" wrote a rule: "Check for drug conflicts every time the medical list changes." The moment the October note is added, the computer program runs, sees the conflict, and immediately pops up a warning: "CRITICAL: You are taking a penicillin drug, but you are allergic to penicillin!"
- Analogy: The old system is a passive library; the new system is a smart home security system that alerts you before the fire starts.
3. The "Contradiction" Problem
- The Scenario: Jessica says she loves cilantro in one chat, then hates it in another.
- Old Way: The shoebox has two conflicting notes. The AI gets confused.
- New Way: The software app has a single
Preferencesobject. When the new fact comes in, the code updates the object to reflect the latest truth, or flags the conflict for review. It keeps the memory clean and consistent.
The Results: What the Paper Found
The authors tested this against other memory systems using standard benchmarks:
- Basic Memory: On simple questions like "What is my passport number?", the new system is just as good as the best existing systems (about 79% accuracy).
- Complex Math: On questions requiring counting or averaging (like the trip example), the new system is nearly perfect (99%), while other systems crash (dropping to 6–43%).
- Safety Alerts: On tests where the AI had to spot dangers without being asked (like the drug allergy), the new system caught 100% of the standard dangers. Other systems missed many because they were waiting for a question.
- Cost: It costs a bit more to build the "software app" at the beginning, but once built, asking questions is very cheap and fast. If you ask the same user many questions, the new system becomes much cheaper than the old way.
The Bottom Line
The paper argues that we should stop treating AI memory as a passive storage bin of text and start treating it as an active software project. By turning a user's history into code that can be run, checked, and calculated, the AI becomes much better at doing math, spotting hidden dangers, and remembering the big picture, rather than just retrieving isolated facts.
In short: Instead of asking the AI to "remember" everything, we give it a program that runs on the user's life, automatically checking for patterns and alerts in real-time.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.