{"slug": "my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it", "title": "My agent kept forgetting what it was doing. A scratchpad fixed it.", "summary": "A developer created `agent-scratchpad`, a structured working memory tool that prevents AI agents from repeating themselves by tracking progress as a keyed dictionary. The scratchpad renders its state as plain text for injection into system prompts, and includes helpers for list building, counting, and logging every decision for replay.", "body_md": "*This is a submission for the Hermes Agent Challenge.*\n\nMy Hermes research agent was asking the same questions twice. It would identify a paper, start analyzing it, then two turns later ask if anyone had studied the same topic. The context window had the answer but the agent wasn't tracking its own progress.\n\nThe fix isn't more context — it's structured working memory. That's `agent-scratchpad`\n\n.\n\nA scratchpad is just a keyed dict with helpers for list building and counting. The useful part is `to_text()`\n\n— it renders the current state as plain text you can inject into any system prompt.\n\n``` python\nfrom agent_scratchpad import Scratchpad\n\npad = Scratchpad()\npad.set(\"topic\", \"quantum error correction\")\npad.append(\"papers_found\", \"Shor 1995\")\npad.append(\"papers_found\", \"Steane 1996\")\npad.increment(\"search_count\")\npad.append(\"hypotheses\", \"Surface codes may be more practical than Steane codes\")\n\nprint(pad.to_text(title=\"Research progress\"))\n# Research progress:\n# hypotheses:\n#   - Surface codes may be more practical than Steane codes\n# papers_found:\n#   - Shor 1995\n#   - Steane 1996\n# search_count: 1\n# topic: quantum error correction\ncontext = pad.to_text(title=\"What I know so far\")\n\nresponse = client.messages.create(\n    model=\"claude-sonnet-4-5\",\n    system=f\"You are a research assistant.\\n\\n{context}\",\n    messages=messages,\n)\n```\n\nThe scratchpad goes in the system prompt. The agent can read what it's already found and not repeat itself.\n\n```\npad.set(\"key\", value)          # set scalar\npad.get(\"key\", default=None)   # deep copy\npad.delete(\"key\")\npad.has(\"key\")\n\npad.append(\"papers\", \"Shor 1995\")   # build lists\npad.prepend(\"queue\", \"urgent item\") # front-of-list\npad.extend_list(\"papers\", [...])    # bulk append\n\npad.increment(\"search_count\")   # counter (init to 0)\npad.decrement(\"errors\")\npad.increment(\"cost_cents\", 5)\n\npad.update({\"a\": 1, \"b\": 2})  # set multiple\npad.clear()\npad = Scratchpad(\"logs/scratchpad.jsonl\")\npad.set(\"topic\", \"ML\")\n# appends {\"ts\": ..., \"op\": \"set\", \"key\": \"topic\", \"value\": \"ML\"}\n```\n\nReplay the scratchpad log to see every decision the agent made.\n\n```\npad.save(\"state.json\")\n\n# Next run\npad = Scratchpad.load(\"state.json\")\n```\n\nFull JSON snapshot for resuming long-running agents.\n\nStandard library only: `json`\n\n, `copy`\n\n, `time`\n\n, `pathlib`\n\n. Nothing else.\n\n```\npip install agent-scratchpad\n```\n\n", "url": "https://wpnews.pro/news/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it", "canonical_source": "https://dev.to/mukundakatta/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it-m32", "published_at": "2026-05-25 21:21:09+00:00", "updated_at": "2026-05-25 21:34:05.961942+00:00", "lang": "en", "topics": ["ai-agents", "large-language-models", "artificial-intelligence", "ai-tools", "ai-research"], "entities": ["Hermes", "Claude"], "alternates": {"html": "https://wpnews.pro/news/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it", "markdown": "https://wpnews.pro/news/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it.md", "text": "https://wpnews.pro/news/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it.txt", "jsonld": "https://wpnews.pro/news/my-agent-kept-forgetting-what-it-was-doing-a-scratchpad-fixed-it.jsonld"}}