{"slug": "beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and", "title": "Beyond the Prompt: How to Build Stateful AI Agents with Persistent Memory and Self-Learning Loops", "summary": "Limitations of stateless AI systems, which require feeding entire conversation histories into each new request, and introduces the concept of stateful AI agents that maintain persistent memory and self-learning capabilities. It details the Hermes Agent architecture, which organizes statefulness into three components—Soul (core identity), Memory (episodic facts and preferences), and Skills (procedural knowledge)—to enable continuous adaptation and evolution. The piece also provides a Python implementation guide for building such self-improving agents.", "body_md": "Imagine hiring a brilliant software engineer who suffers from complete amnesia every time they blink.\n\nEvery time you ask them a question, you have to hand them their entire employment history, the codebase documentation, your style guide, and a summary of every conversation you’ve ever had with them. They process the information, give you a great answer, and then—*blink*—it’s all gone.\n\nThis is the exhausting reality of **stateless AI applications**.\n\nMost developers building with Large Language Models (LLMs) today are stuck in this stateless paradigm. They write clever prompts, wrap them in an API call, and rely on the application layer to aggressively feed the entire chat history back into the context window with every new turn. It’s expensive, it’s inefficient, and it places a hard ceiling on how smart an agent can actually become.\n\nTo build truly autonomous, adaptive, and personalized AI systems, we must cross the chasm from **stateless interactions** to **stateful agents**.\n\nIn this deep dive, we will explore the architecture of the **Hermes Agent**—a stateful AI system that possesses persistent memory, a continuous learning loop, and the ability to evolve alongside its user. We will break down the engineering patterns behind statefulness and walk through a complete Python implementation to build your own self-improving agent from scratch.\n\n(The concepts and code demonstrated here are drawn from my ebook [Hermes Agent, The Self-Evolving AI Workforce](https://tiny.cc/HermesAgent))\n\n## The Stateless Ceiling: Why Vending Machines Make Poor Assistants\n\nTo understand the power of statefulness, we must first look at why statelessness cripples AI agents.\n\nThink of a stateless system like a vending machine. You insert a dollar, press a button, and get a soda. The vending machine doesn't care who you are, what your health goals are, or that you bought the exact same drink yesterday. Every transaction is an isolated, self-contained event. It has no memory of its past, no context for the present, and no capacity to learn for the future.\n\nEarly LLM applications operate exactly like this. You send a prompt, and the model returns a response. The model itself does not change.\n\n``` python\n# A classic stateless utility call\nimport datetime\n\ndef parse_date(date_string: str) -> datetime.datetime:\n    return datetime.datetime.strptime(date_string, \"%Y-%m-%d\")\n```\n\nThis simple Python function is a stateless transaction. It takes an input, returns an output, and immediately forgets the operation ever happened. It doesn't learn that you frequently parse dates from European formats, nor does it optimize its parsing logic over time.\n\nWhen developers try to build \"agents\" on top of this stateless foundation, they usually resort to an illusion of continuity. They stitch together a chat history array and send the entire history back to the API on every single turn.\n\nThis approach has three massive flaws:\n\n-\n**Context Bloat:** As the conversation grows, your token usage skyrockets exponentially. -\n**Memory Horizon Limits:** Once the conversation exceeds the model's context window, the agent \"forgets\" the earliest parts of the interaction. -\n**Zero Knowledge Accumulation:** The agent cannot carry lessons learned in Session A over to Session B. If it figures out a complex bash command to fix a Docker bug today, it will have to re-discover that solution from scratch next week.\n\nA **stateful agent** breaks this paradigm entirely. It is not just a wrapper around an LLM; it is an evolving entity. It mirrors the workflow of a skilled artisan—like a master carpenter. The carpenter remembers the tools they used yesterday, the specific quirks of the wood they are carving, the preferences of their client, and the hard-won lessons from a project they completed last month. They do not start their education from scratch every morning.\n\n## The Triad of Persistent State: Soul, Memory, and Skills\n\nIn the Hermes Agent architecture, statefulness is not treated as a single monolithic database. Instead, it is partitioned into a carefully structured triad that mirrors how human professionals organize their own knowledge.\n\n```\n                  ┌────────────────────────────────────────┐\n                  │                 SOUL                   │\n                  │   (Core Identity, Style, Principles)   │\n                  └───────────────────┬────────────────────┘\n                                      │\n                  ┌───────────────────┴────────────────────┐\n                  │                MEMORY                  │\n                  │   (Episodic Facts, User Preferences)   │\n                  └───────────────────┬────────────────────┘\n                                      │\n                  ┌───────────────────┴────────────────────┐\n                  │                SKILLS                  │\n                  │   (Procedural Knowledge, Toolkits)     │\n                  └────────────────────────────────────────┘\n```\n\nLet’s break down each component of this stateful triad.\n\n### 1. The Soul (`SOUL.md`\n\n)\n\nThis is the agent's core identity and \"constitution.\" It defines *who* the agent is, its communication style, its behavioral boundaries, and its operational principles. It is not a dynamic log of facts, but a foundational document.\n\nIn the codebase, a helper function reads this markdown file and injects it directly into the system prompt. It ensures that whether the agent is writing code or debugging a server, its fundamental persona and safety guardrails remain perfectly consistent.\n\n### 2. Memory (`MEMORY.md`\n\nand `USER.md`\n\n)\n\nThis is the agent's episodic and semantic memory store. Instead of keeping a raw, unorganized transcript of every chat, the agent maintains a curated, structured knowledge base of facts about the user and past interactions.\n\n-\n`USER.md`\n\ntracks durable information about the user (e.g., name, programming language preferences, operating system, working hours). -\n`MEMORY.md`\n\ntracks dynamic, episodic facts learned during tasks (e.g., \"The local staging database is hosted on port 5433, not 5432\").\n\nThis layer is managed by a semantic `MemoryStore`\n\nclass. The agent can read from this store to build context and write to it dynamically using custom tools.\n\n### 3. Skills (`~/.hermes/skills/`\n\n)\n\nIf memory is \"knowing *what*,\" skills are \"knowing *how*.\" This is the agent's procedural memory.\n\nA skill in Hermes is a reusable, packaged directory containing:\n\n-\n`SKILL.md`\n\n: A markdown file describing what the skill does, when to use it, and its input parameters. -\n`scripts/`\n\n: Executable scripts (Python, Bash, etc.) that perform the task. -\n`templates/`\n\n: Reusable code or text templates.\n\nInstead of writing complex code on the fly every time, the agent can write a script once, save it to its skills directory, and call it as a custom tool in future sessions. It builds its own personalized toolbox.\n\n## The Closed Learning Loop: How the Agent Self-Improves\n\nA stateful agent must be able to learn without constant human intervention. The Hermes Agent achieves this through a **Closed Learning Loop** executed entirely in the background.\n\nThis loop consists of two primary engines: **Background Review** and the **Skill Curator**.\n\n```\n   ┌───────────────────────────────────────────────────────┐\n   │                  User Interaction                     │\n   └──────────────────────────┬────────────────────────────┘\n                              │ Turn Completes\n                              ▼\n   ┌───────────────────────────────────────────────────────┐\n   │               Background Review Thread                │\n   │  (Spawns quiet, forked agent to analyze conversation)  │\n   └──────────────┬─────────────────────────┬──────────────┘\n                  │                         │\n                  ▼ Extract Facts           ▼ Extract Procedures\n   ┌──────────────────────────┐    ┌───────────────────────┐\n   │       Memory Store       │    │     Skills Engine     │\n   │  (Updates MEMORY.md)     │    │   (Creates SKILL.md)  │\n   └──────────────────────────┘    └────────┬──────────────┘\n                                            │\n                                            ▼ Runs asynchronously\n                                   ┌───────────────────────┐\n                                   │     Skill Curator     │\n                                   │ (Archives stale files)│\n                                   └───────────────────────┘\n```\n\n### The Background Review (Self-Reflection)\n\nWhen a conversation turn completes successfully, the agent doesn't just sit idle waiting for your next message. It increments internal counters: `_turns_since_memory`\n\nand `_iters_since_skill`\n\n.\n\nOnce these counters hit a configured threshold (e.g., every 5 to 10 iterations), the agent initiates a self-reflection phase:\n\n-\n**Forking the Agent:** The system spawns a background thread that instantiates a*forked*copy of the current agent. This copy is set to`quiet_mode=True`\n\n, meaning it operates in complete silence without cluttering the user's console. -\n**The Reflection Prompt:** The forked agent is fed a specialized prompt (e.g.,`_COMBINED_REVIEW_PROMPT`\n\n) along with the recent conversation history. It is asked to analyze the transcript and answer two questions:-\n*Did the user share any new preferences or facts that should be saved to long-term memory?* -\n*Did we execute a complex, successful multi-step procedure that should be codified into a reusable skill?*\n\n-\n-\n**Autonomous Tool Execution:** The silent background agent runs its own mini-reasoning loop. If it identifies new facts, it calls the`memory`\n\ntool to update`USER.md`\n\nor`MEMORY.md`\n\n. If it identifies a new procedure, it calls the`skill_manage`\n\ntool to write a new`SKILL.md`\n\nto disk. -\n**Reporting Back:** Once the background thread finishes, the parent agent prints a clean, non-intrusive summary of what it learned (e.g.,`[System Info: Memory updated - User prefers PyTest over Unittest]`\n\n).\n\n### The Skill Curator\n\nAn agent that constantly learns skills will eventually suffer from \"tool bloat.\" If its toolbox has 500 highly specific scripts, the system prompt will become overwhelmed, and the LLM will experience severe context distraction.\n\nTo prevent this, a background daemon called the **Skill Curator** (`agent/curator.py`\n\n) runs periodically.\n\n- It tracks skill usage via a metadata file (\n`.usage.json`\n\n). - If a skill hasn't been used for a configurable number of days, the Curator automatically moves it to an\n`.archive/`\n\ndirectory. - Archived skills are removed from the active system prompt but can be restored instantly if the agent needs them again.\n- Users can \"pin\" critical skills to exempt them from archiving.\n\n## Building a Stateful Agent from Scratch\n\nLet's put these architectural patterns into practice. Below is a complete, production-grade Python script demonstrating how to initialize and run a stateful AI agent using SQLite-backed session storage and markdown-based long-term memory.\n\n### Prerequisites\n\nTo run this code, make sure you have the necessary environment variables set up for your LLM provider (we'll use OpenRouter pointing to Claude 3.5 Sonnet in this example):\n\n```\nexport OPENROUTER_API_KEY=\"your-api-key-here\"\n```\n\n### The Implementation\n\n``` bash\n#!/usr/bin/env python3\n\"\"\"\nstateful_agent_demo.py\nA complete, runnable example of a stateful AI agent.\nThis script demonstrates persistent memory, cross-session database logging,\nand semantic recall across separate agent executions.\n\"\"\"\n\nimport os\nimport uuid\nimport logging\nfrom datetime import datetime\nfrom pathlib import Path\n\n# --- Core Stateful Agent Architecture Imports ---\n# AIAgent: The central orchestrator managing the reasoning loop and tool execution.\nfrom run_agent import AIAgent\n\n# SessionDB: SQLite-backed persistent store for conversation history with FTS5.\nfrom hermes_state import SessionDB\n\n# MemoryStore: Semantic memory engine managing local markdown databases.\nfrom tools.memory_tool import MemoryStore\n\n# Constants: Helper to get standard home directories.\nfrom hermes_constants import get_hermes_home\n\n# Configure clean logging to observe the agent's internal state transitions\nlogging.basicConfig(\n    level=logging.INFO,\n    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'\n)\nlogger = logging.getLogger(\"StatefulDemo\")\n\n# =====================================================================\n# Step 1: Establish the State Directories\n# =====================================================================\nHERMES_HOME = get_hermes_home()\nHERMES_HOME.mkdir(parents=True, exist_ok=True)\n\n# Define paths for our SQLite session database and memory files\nSESSION_DB_PATH = HERMES_HOME / \"sessions\" / \"stateful_demo.db\"\nSESSION_DB_PATH.parent.mkdir(parents=True, exist_ok=True)\n\nlogger.info(f\"Initializing stateful storage at: {HERMES_HOME}\")\n\n# =====================================================================\n# Step 2: Initialize the SQLite Session Database\n# =====================================================================\n# SessionDB automatically provisions tables for sessions, messages,\n# and full-text search indexes (FTS5) to enable rapid cross-session recall.\nsession_db = SessionDB(db_path=str(SESSION_DB_PATH))\n\n# =====================================================================\n# Step 3: Initialize the Long-Term Memory Store\n# =====================================================================\n# MemoryStore reads and writes structured facts to memory.md and user.md.\n# We set strict character limits to prevent context bloat.\nmemory_store = MemoryStore(\n    memory_char_limit=2000,\n    user_char_limit=1000\n)\n# Load any existing facts from prior runs\nmemory_store.load_from_disk()\n\n# =====================================================================\n# Step 4: Configure and Run Session 1 (Learning the User)\n# =====================================================================\n# We generate a unique session ID for our first conversation.\nsession_id_1 = f\"session_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:4]}\"\n\nlogger.info(f\"Starting Session 1 ID: {session_id_1}\")\n\n# Instantiate the stateful agent\nagent_1 = AIAgent(\n    base_url=os.getenv(\"OPENROUTER_BASE_URL\", \"https://openrouter.ai/api/v1\"),\n    api_key=os.getenv(\"OPENROUTER_API_KEY\"),\n    provider=\"openrouter\",\n    model=\"anthropic/claude-3.5-sonnet\",\n    max_iterations=30,\n    session_id=session_id_1,\n    session_db=session_db,\n    skip_memory=False,\n    platform=\"cli\",\n)\n\n# Inject our persistent memory store into the agent instance\nagent_1._memory_store = memory_store\nagent_1._memory_enabled = True\nagent_1._user_profile_enabled = True\nagent_1._memory_nudge_interval = 1  # Force memory review immediately for this demo\n\nprint(\"\\n\" + \"=\"*70)\nprint(\" SESSION 1: TEACHING THE AGENT PREFERENCES\")\nprint(\"=\"*70)\n\nuser_msg_1 = \"Hello! My name is Dr. Aris Thorne. I am a bioinformatician, and I prefer code snippets written strictly in Rust.\"\nprint(f\"\\n[User]: {user_msg_1}\")\n\n# Execute the conversation loop\nresult_1 = agent_1.run_conversation(\n    user_message=user_msg_1,\n    task_id=\"task_001\"\n)\n\nprint(f\"\\n[Agent]: {result_1['final_response']}\")\nprint(f\"\\n[System]: API calls executed: {result_1['api_calls']}\")\n\n# Flush the in-memory changes to disk (persisting user.md and memory.md)\nif agent_1._memory_store:\n    agent_1._memory_store.save_to_disk()\n\n# Explicitly release client connections\nagent_1.release_clients()\n\n# =====================================================================\n# Step 5: Configure and Run Session 2 (Testing Memory Recall)\n# =====================================================================\n# To simulate a real-world scenario where the application was closed,\n# restarted, or run on a different day, we instantiate a completely \n# new agent instance with a fresh session ID.\nsession_id_2 = f\"session_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:4]}\"\n\nlogger.info(f\"Starting Session 2 ID: {session_id_2}\")\n\n# Reload the database and memory files from disk\nsession_db_reload = SessionDB(db_path=str(SESSION_DB_PATH))\nmemory_store_reload = MemoryStore(memory_char_limit=2000, user_char_limit=1000)\nmemory_store_reload.load_from_disk()\n\nagent_2 = AIAgent(\n    base_url=os.getenv(\"OPENROUTER_BASE_URL\", \"https://openrouter.ai/api/v1\"),\n    api_key=os.getenv(\"OPENROUTER_API_KEY\"),\n    provider=\"openrouter\",\n    model=\"anthropic/claude-3.5-sonnet\",\n    max_iterations=30,\n    session_id=session_id_2,\n    session_db=session_db_reload,\n    skip_memory=False,\n    platform=\"cli\",\n)\n\nagent_2._memory_store = memory_store_reload\nagent_2._memory_enabled = True\nagent_2._user_profile_enabled = True\n\nprint(\"\\n\" + \"=\"*70)\nprint(\" SESSION 2: VERIFYING KNOWLEDGE RETRIEVAL\")\nprint(\"=\"*70)\n\n# We ask a highly ambiguous question that requires previous context to answer correctly.\nuser_msg_2 = \"Can you write a quick function to parse a DNA fasta header?\"\nprint(f\"\\n[User]: {user_msg_2}\")\n\nresult_2 = agent_2.run_conversation(\n    user_message=user_msg_2,\n    task_id=\"task_002\"\n)\n\nprint(f\"\\n[Agent]: {result_2['final_response']}\")\n\nif agent_2._memory_store:\n    agent_2._memory_store.save_to_disk()\n\nagent_2.release_clients()\n\n# =====================================================================\n# Step 6: Cross-Session Full-Text Search (FTS5) Demonstration\n# =====================================================================\nprint(\"\\n\" + \"=\"*70)\nprint(\" SESSION DATABASE: CROSS-SESSION SEARCH\")\nprint(\"=\"*70)\n\n# Search the SQLite database for any reference to \"Thorne\"\nsearch_query = \"Thorne\"\nsearch_results = session_db_reload.search_sessions(search_query, limit=5)\n\nprint(f\"\\nSearching database for '{search_query}'...\")\nprint(f\"Found {len(search_results)} relevant records:\")\n\nfor idx, record in enumerate(search_results):\n    print(f\"\\n  [{idx + 1}] Session: {record.get('session_id', 'Unknown')}\")\n    print(f\"      Snippet match: ...{record.get('snippet', '')}...\")\n\nprint(\"\\n\" + \"=\"*70)\nprint(\" DEMO COMPLETE: Stateful execution verified.\")\nprint(\"=\"*70)\n```\n\n## Deep Dive: The Stateful Agent Loop in Practice\n\nHow does the agent coordinate all of this state behind the scenes? The magic happens inside the `run_conversation()`\n\nmethod within `run_agent.py`\n\n. Let’s trace the exact lifecycle of a single turn.\n\n```\n┌───────────────────────────────────────────────────────────────────────────┐\n│ 1. Context Assembly                                                       │\n│    Reads Soul, Memory, Active Skills, and Platform hints to build system  │\n│    prompt. Caches it to maximize LLM prefix-cache hits.                   │\n└─────────────────────────────────────┬─────────────────────────────────────┘\n                                      │\n                                      ▼\n┌───────────────────────────────────────────────────────────────────────────┐\n│ 2. Preflight Check & Compression                                          │\n│    Measures token count. If history exceeds threshold, triggers proactive │\n│    context compression before making API calls.                           │\n└─────────────────────────────────────┬─────────────────────────────────────┘\n                                      │\n                                      ▼\n┌───────────────────────────────────────────────────────────────────────────┐\n│ 3. Tool-Calling Loop (Reasoning)                                          │\n│    - Calls LLM with stateful prompt.                                      │\n│    - Validates and executes tools (e.g., File I/O, Sandbox Execution).     │\n│    - Monitors guardrails to block infinite loops.                         │\n│    - Checks for mid-turn user steering commands (/steer).                 │\n└─────────────────────────────────────┬─────────────────────────────────────┘\n                                      │\n                                      ▼\n┌───────────────────────────────────────────────────────────────────────────┐\n│ 4. Post-Turn Learning                                                     │\n│    Spawns background reflection thread to extract memories and skills.    │\n└─────────────────────────────────────┬─────────────────────────────────────┘\n                                      │\n                                      ▼\n┌───────────────────────────────────────────────────────────────────────────┐\n│ 5. Session Persistence                                                    │\n│    Writes the entire turn (system, user, tool, assistant messages) to     │\n│    SQLite DB and local JSON logs. Guaranteed write on crash/interrupt.     │\n└───────────────────────────────────────────────────────────────────────────┘\n```\n\n### 1. Context Assembly\n\nWhen you call `run_conversation()`\n\n, the agent doesn't just construct a simple system message. The `_build_system_prompt()`\n\nmethod compiles a highly structured, multi-layered environment:\n\n-\n**The Soul:** Injected at the top to set the core persona. -\n**Persistent Memory:** The contents of`MEMORY.md`\n\nand`USER.md`\n\nare dynamically formatted and injected. -\n**Skills Guidance:** A dynamic list of currently active skills and their execution templates. -\n**Context Files:** Local environment files like`.cursorrules`\n\nor`AGENTS.md`\n\nare appended.\n\nTo keep this process highly performant, the system prompt is compiled and cached (`_cached_system_prompt`\n\n). It is only rebuilt when context compression is triggered, maximizing **prefix cache hits** on modern LLM APIs (like Anthropic and DeepSeek) and reducing latency by up to 80%.\n\n### 2. Pre-Turn Context Management\n\nBefore sending the payload to the API, the agent checks if the conversation history is approaching the model's limits. If it exceeds the compression threshold, the agent proactively condenses the oldest history into a structured summary. This prevents unexpected context-length failures on the first turn of a resumed session.\n\n### 3. The Tool-Calling Loop\n\nThe agent enters a reasoning loop. It makes an API call, parses the requested tool calls, validates their JSON arguments, executes them, and appends the results back to the message history.\n\nDuring this loop, two unique stateful safety features are active:\n\n-\n**Tool Guardrails:** A controller tracks repeated, non-progressing tool calls (e.g., repeatedly running`ls`\n\nbecause it can't find a file). If a loop is detected, the guardrail halts execution to prevent runaway API bills. -\n**Steering Injection:** The loop checks for`/steer`\n\ninputs, allowing users to inject guidance mid-turn without interrupting the underlying execution thread.\n\n### 4. Session Persistence\n\nFinally, the agent persists the entire session. Whether the run succeeded, failed, or was manually aborted via `Ctrl+C`\n\n, the `_persist_session()`\n\nmethod is guaranteed to run. It commits the exact state to both a local JSON log and the SQLite `SessionDB`\n\n.\n\n## Resource Safeguards: The Iteration Budget\n\nStatefulness introduces a major engineering challenge: **resource management**.\n\nWhen an agent has the power to call tools, write scripts, read files, and trigger background self-reflection loops, it can easily get caught in an infinite loop. A single unhandled exception in a tool could cause the agent to call the API hundreds of times, burning through thousands of dollars in tokens in minutes.\n\nTo solve this, Hermes utilizes a thread-safe ** IterationBudget** class.\n\n``` python\nclass IterationBudget:\n    def __init__(self, limit: int):\n        self._remaining = limit\n        self._lock = threading.Lock()\n\n    def consume(self, amount: int = 1) -> bool:\n        with self._lock:\n            if self._remaining >= amount:\n                self._remaining -= amount\n                return True\n            return False\n\n    def refund(self, amount: int = 1):\n        with self._lock:\n            self._remaining += amount\n```\n\nThe `IterationBudget`\n\nacts as the agent's fuel gauge.\n\n- Every API call and tool execution consumes a portion of the budget.\n- The budget is\n**thread-safe** and shared between the parent agent and any spawned background reflection agents. This prevents a background thread from spinning out of control. -\n**The Refund Mechanism:** If the agent executes a highly efficient, cheap programmatic tool (like reading a local file or checking a system variable), the iteration is*refunded*. If it executes a heavy, slow, or expensive tool (like running a web browser sandbox or calling a sub-agent), the budget is fully consumed.\n\nThis programmatic budgeting ensures that statefulness does not come at the expense of financial and computational safety.\n\n## Conclusion: The Shift from Tools to Partners\n\nThe transition from stateless to stateful AI is more than an engineering upgrade; it is a fundamental shift in how humans interact with software.\n\nA stateless agent is a **utility tool**. It is a hammer—reliable, but entirely dependent on you picking it up, positioning it, and swinging it correctly every single time.\n\nA stateful agent is a **partner**. It learns your codebase, remembers your architectural preferences, builds its own library of custom tools, and refines its performance silently while you sleep. By implementing the triad of Soul, Memory, and Skills, and orchestrating them within a closed learning loop, we can build systems that don't just process text—they accumulate wisdom.\n\nThe future of software belongs to systems that grow with us. And the foundation of that growth is statefulness.\n\n### Let's Discuss\n\n-\n**The Tool Bloat Dilemma:** As an agent creates more custom skills, how do you think we should handle semantic search over skills? Should the agent use vector embeddings to dynamically load only the top 3 relevant skills into its prompt, or is the Curator's active/archive model sufficient? -\n**The Ethics of Agent Identity:** If an agent's \"Soul\" (`SOUL.md`\n\n) and \"Memory\" (`MEMORY.md`\n\n) are continuously modified by background threads, at what point does the agent's behavior drift too far from its original design? How would you implement \"identity guardrails\" to prevent an agent from editing its core safety principles?\n\n*Leave your thoughts and engineering approaches in the comments below!*\n\nThe concepts and code demonstrated here are drawn directly from the comprehensive roadmap laid out in the ebook **Hermes Agent, The Self-Evolving AI Workforce**: [details link](https://tiny.cc/HermesAgent), you can find also my programming ebooks with AI here: [Programming & AI eBooks](http://tiny.cc/ProgrammingBooks).", "url": "https://wpnews.pro/news/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and", "canonical_source": "https://dev.to/programmingcentral/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and-self-learning-loops-2e1k", "published_at": "2026-05-21 20:00:00+00:00", "updated_at": "2026-05-21 20:03:09.609778+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "large-language-models", "developer-tools", "enterprise-software"], "entities": ["Hermes Agent", "LLMs", "Large Language Models"], "alternates": {"html": "https://wpnews.pro/news/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and", "markdown": "https://wpnews.pro/news/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and.md", "text": "https://wpnews.pro/news/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and.txt", "jsonld": "https://wpnews.pro/news/beyond-the-prompt-how-to-build-stateful-ai-agents-with-persistent-memory-and.jsonld"}}