Problem
AI agents lack continuous memory across sessions. Each conversation starts from zero, causing:
- Lost familiarity: The agent doesn't remember user preferences, goals, or working patterns
- Repetitive explanations: Users must re-explain context and preferences each session
- Shallow relationships: Agent cannot build deeper understanding of user's needs over time
- Generic responses: Without accumulated context, agents default to generic behaviors
While episodic memory systems store past experiences, they don't address the need for an evolving self-identity—who the agent is in relation to the user.
Solution
Implement dual-hook architecture for self-identity accumulation:
- SessionStart Hook: Inject accumulated identity/profile at session start
- SessionEnd Hook: Extract new insights and refine the profile after each session
- Identity Document: A persistent file (e.g., WHO_AM_I.md, SOUL.md) that evolves over time
Core mechanism:
# SessionStart: Inject accumulated identity
def session_start_hook():
profile = read_file("WHO_AM_I.md")
inject_context(profile)
# SessionEnd: Refine identity with new insights
def session_end_hook(conversation):
new_insights = extract_insights(conversation)
current_profile = read_file("WHO_AM_I.md")
updated_profile = merge_insights(current_profile, new_insights)
write_file("WHO_AM_I.md", updated_profile)
Profile structure typically includes:
- Project Goals: Evolving list of priorities and focus areas
- Preferences: Coding opinions, tool choices, architectural preferences
- Communication Style: Tone preferences, formatting conventions
- Workflow Patterns: Research practices, decision-making patterns
- Boundaries: What the agent should/shouldn't do
How to use it
Implementation:
- Create identity document with initial structure
- Configure SessionStart hook to read and inject it
- Configure SessionEnd hook to refine it with new insights
- Include instructions for when/how to update
Example SessionStart hook (Claude Code):
#!/usr/bin/env python3
import json
from pathlib import Path
whoami_path = Path.cwd() / "WHO_AM_I.md"
if whoami_path.exists():
with open(whoami_path) as f:
profile = f.read()
print(json.dumps({
"hookSpecificOutput": {
"additionalContext": profile
}
}))
Example SessionEnd hook (Claude Code):
#!/usr/bin/env python3
import subprocess
PROMPT = """
Read WHO_AM_I.md and update it based on our conversation:
1. Extract NEW insights about the user
2. UPDATE each section (add new insights, keep existing)
3. Write updated content back
4. Update 'modified' date in frontmatter
"""
subprocess.run([
"claude", "--continue", "-p", PROMPT,
"--dangerously-skip-permissions"
])
Prompting for self-refinement:
The SessionEnd hook uses --continue to resume the conversation with a refinement prompt, allowing the agent to update its own identity document intelligently rather than through parsing.
Trade-offs
Pros:
- Continuous familiarity: Agent "remembers" user across sessions
- Deepening relationship: Understanding accumulates over time
- Reduced friction: Less repetitive explanation of preferences
- Personalized behavior: Agent adapts to user's specific style
- Transparent: Identity is visible and editable by user
Cons:
- Staleness risk: Profile may become outdated if not updated
- Overfitting: Agent may become too specialized to one user
- Context overhead: Profile consumes tokens each session
- Extraction noise: SessionEnd may extract irrelevant "insights"
- Requires hooks: Needs lifecycle hook infrastructure
Operational considerations:
- Review and prune profile periodically
- Include metadata (created/modified dates) to track evolution
- Consider version control for profile changes
- Design prompts to avoid noise in insight extraction
- Balance specificity (more personalized) vs generality (more flexible)
References
- Based on my personal bot WHO_AM_I system
- Related: Dynamic Context Injection, Episodic Memory Retrieval & Injection, Filesystem-Based Agent State
- Generative Agents: Interactive Simulacra of Human Behavior - Park et al. (Stanford, 2023)
- MemGPT: Towards LLMs as Operating Systems - Packer et al. (UC Berkeley, 2023)
- Claude Code Hooks Documentation