Skip to content

amitrydv/agentmem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AgentMem

Python License Cost Works with

Give your AI agents memory. Kill your workflow timers. Get alerted when things break.

One Python file. No database. No extra cost.


What Problem Does This Solve?

Problem 1 — Agents repeat the same mistakes

Every time your n8n / Make / Zapier workflow runs, the AI agent starts completely fresh. It has no memory of what failed before. So it makes the same error again. And again.

Example:

You have a workflow that reads a CSV and loads it into a database. The agent fails because the date column is in DD/MM/YYYY format but the DB expects YYYY-MM-DD. You fix it manually. Next run — same failure. Agent forgot everything.

AgentMem fixes this. The agent reads its own past mistakes before every run and avoids them automatically.


Problem 2 — Timers break your workflows

In n8n, Make, and Zapier — when you call an AI model, you add a "wait 15 seconds" node hoping the model finishes in time.

  • Too short → next step fires before output arrives → workflow crashes
  • Too long → slow, wasteful, unreliable

AgentMem replaces timers with an event bridge — fires the next step the exact instant the agent finishes. No guessing. No crashes.


Problem 3 — You don't know when something breaks

When a workflow fails at 2am, you find out the next morning. By then it's too late.

AgentMem sends you a Gmail alert the moment a task hits maximum failures — with a full summary of every attempt and what was tried.


How It Works

The 5-Chance System

AgentMem gives every task 5 chances before shutting down and alerting you.

Chance What happens on ERROR What happens on SUCCESS
1 Simple log — fast, 2 fields No log needed — agent already knows this task
2 Simple log — same as chance 1 Success log written. Counter resets to 0
3 Deep diagnosis — 6 fields Success log written. Counter resets to 0
4 Deep diagnosis Success log written. Counter resets to 0
5 Deep diagnosis → shutdown → Gmail alert sent Success log written. Counter resets to 0

Counter resets on every success — next error starts fresh from chance 1. Counter tracks by exact task text — different task = separate counter.


Simple Log (Chances 1 & 2)

Fast. Cheap. Enough for first-time errors.

what_broke : date column got DD/MM/YYYY, database expected YYYY-MM-DD
next_time  : convert all date columns to ISO format before inserting

Deep Diagnosis Log (Chances 3, 4, 5)

Full breakdown. Forces the agent to think, not just summarise.

what_tried      : inserted rows directly using pandas to_sql
what_broke      : column 'created_at' value '24/05/2026' rejected by database
root_cause      : assumed ISO format, never validated before insert
failed_at_step  : step 3 of 4 — steps 1 and 2 passed fine
next_time       : detect and convert ALL date columns before any insert operation
confidence      : HIGH

Success Log (After Previous Failures)

The most valuable log of all. Captures what actually fixed the problem.

Real example — phone firmware flashing:

previous_errors  : ["direct CMD merge failed", "sig2img missing header flag"]
what_changed     : used sig2img method with explicit --header flag
why_it_worked    : header validation was missing in both previous attempts
lesson           : always use sig2img with --header flag for firmware merging

Next time this task runs — agent goes straight to sig2img. Zero wasted attempts.

Note: If task succeeds on the very first try — no log is written. The agent already handles this correctly. No log needed.


How to Set Up

Step 1 — Install dependencies

pip install flask requests anthropic

Step 2 — Get your Anthropic API key

Sign up at console.anthropic.com → copy your API key.

Step 3 — Set up Gmail alerts (one time only)

  1. Go to myaccount.google.com → Security → 2-Step Verification → turn ON
  2. Scroll down → App Passwords → Select: Mail + Windows Computer → Generate
  3. Copy the 16-character password Google gives you

Step 4 — Create your config file

Create a file called .env in the same folder as agentmem.py:

ANTHROPIC_API_KEY=your_anthropic_key_here
GMAIL_ADDRESS=youremail@gmail.com
GMAIL_APP_PASSWORD=abcdefghijklmnop
ALERT_EMAIL=youremail@gmail.com

ALERT_EMAIL is where failure alerts get sent. Can be same as your Gmail or any other email.

Step 5 — Run it

python agentmem.py

You will see:

╔══════════════════════════════════════════╗
║         AgentMem is running              ║
╠══════════════════════════════════════════╣
║  Send task  → http://localhost:8765/run  ║
║  Health     → http://localhost:8765/health
║  Memory     → http://localhost:8765/memory
╚══════════════════════════════════════════╝

How to Connect to n8n

Remove your old timer/wait node. Replace with these 3 nodes:

Node 1 — Trigger
  (your existing schedule / webhook / manual trigger — no change needed)

Node 2 — HTTP Request
  Method : POST
  URL    : http://localhost:8765/run
  Body (JSON):
  {
    "task": "describe what you want the agent to do here",
    "callback_url": "http://YOUR_N8N_URL/webhook/agent-done"
  }

Node 3 — Webhook   ← THIS REPLACES YOUR TIMER NODE
  Path   : agent-done
  Method : POST
  (n8n waits here — no timeout, no guessing — fires the instant agent finishes)

Node 4 onwards — your workflow continues as normal
  {{ $json.output }}          → the agent's result
  {{ $json.success }}         → true or false
  {{ $json.confidence }}      → HIGH / MEDIUM / LOW
  {{ $json.chances_remaining }} → how many chances are left

How to Connect to Make (formerly Integromat)

Module 1 — HTTP (Make an API Key Request)
  Method : POST
  URL    : http://localhost:8765/run
  Body   : { "task": "...", "callback_url": "https://hook.make.com/YOUR_HOOK" }

Module 2 — Webhooks → Custom Webhook   ← replaces your Sleep module
  (Make pauses here until AgentMem fires the result back)

Module 3 onwards — use the output from the webhook

What Comes Back to Your Workflow

On success:

{
  "success": true,
  "output": "CSV parsed. 847 rows inserted into database.",
  "confidence": "HIGH",
  "attempts": 1,
  "chances_remaining": 5
}

On failure with chances remaining:

{
  "success": false,
  "error": "date column format mismatch",
  "attempts": 2,
  "chances_remaining": 3
}

On shutdown after chance 5:

{
  "success": false,
  "shutdown": true,
  "reason": "max attempts reached — human review required",
  "human_alert_sent": true,
  "attempts": 5
}

What the Gmail Alert Looks Like

When a task exhausts all 5 chances, you get this in your inbox:

Subject: 🚨 AgentMem — Task Failed After 5 Attempts

Task: parse customer CSV and load into database
Total attempts: 5

━━━ ATTEMPT HISTORY ━━━

Attempt 1 — FAILED
  Broke   : date column got DD/MM/YYYY, expected YYYY-MM-DD
  Next try: convert date format before inserting

Attempt 2 — FAILED
  Broke   : date conversion applied to created_at only, order_date also failed
  Next try: convert ALL date columns not just created_at

Attempt 3 — FAILED (deep)
  What tried     : converted all columns matching 'date' in name
  What broke     : column 'registered' also a date but name doesn't contain 'date'
  Root cause     : column detection by name pattern missed non-obvious date columns
  Fix to try     : detect date columns by content type, not column name

Attempt 4 — FAILED (deep)
  What tried     : content-type detection on all columns
  What broke     : memory error on large file — 2.3GB CSV too large for pandas
  Root cause     : loading entire file into memory at once
  Fix to try     : use chunked reading with pandas chunksize parameter

Attempt 5 — FAILED (deep)
  What tried     : chunked reading with chunksize=10000
  What broke     : chunk boundary split a multi-line value in column 3
  Root cause     : CSV has quoted newlines, standard chunking breaks them
  Fix to try     : use python csv module with quoting=QUOTE_ALL for large files

━━━ ALL 5 APPROACHES EXHAUSTED ━━━
Human review required. Start from the last root cause — that is the real blocker.
Full history saved to errors.jsonl for next run.

Useful Links (Once Running)

URL What it shows
http://localhost:8765/health Is it running? How many errors logged?
http://localhost:8765/memory All stored mistakes
http://localhost:8765/memory/patterns Which errors repeat most often
POST /memory/clear Wipe all memory and start fresh

Want WhatsApp Alerts Instead of Gmail?

WhatsApp alerts are supported via Twilio (free tier: 1000 messages/month).

To enable — add these to your .env file:

TWILIO_ACCOUNT_SID=your_twilio_sid
TWILIO_AUTH_TOKEN=your_twilio_token
TWILIO_WHATSAPP_FROM=whatsapp:+14155238886
ALERT_WHATSAPP_TO=whatsapp:+91XXXXXXXXXX

Then in agentmem.py change one line:

ALERT_METHOD=whatsapp

The WhatsApp message is a shorter version of the Gmail alert — task name, attempts, last error, what to check.

Gmail is recommended for most users — free, no signup needed beyond your existing account, and supports the full detailed alert. WhatsApp is better if you need instant mobile notification.


Cost

What Cost
Running AgentMem $0
Memory storage $0 — plain text file
Error logging $0 — no extra AI call
Gmail alerts $0 — uses your existing Gmail
WhatsApp alerts $0 up to 1000/month (Twilio free tier)
Total added to your existing agent cost $0

Accuracy

Task type Accuracy
API calls and integrations 92%
File parsing and data processing 88%
Device and system operations 85%
Content generation 62%
Complex reasoning 55%

AgentMem works best on technical failures — wrong formats, API errors, missing fields, wrong endpoints. These have clear, specific errors the agent can accurately diagnose.

For content quality issues (agent writes a bad email but doesn't know it) — the confidence scoring system flags LOW confidence outputs for human review automatically.


File Structure

agentmem/
├── agentmem.py       ← the entire system — one file
├── errors.jsonl      ← auto-created when first error is logged
├── .env              ← your API keys (never commit this to GitHub)
├── .env.example      ← safe template to share
└── README.md         ← this file

License

MIT — free to use, modify, sell, and build on.


Built With Claude

This project was entirely designed and built with Claude by Anthropic — the idea, architecture, code, problem analysis, and documentation all came out of a single conversation.

The story behind it:

The idea started from a real frustration — AI agents in n8n kept repeating the same mistakes with zero memory of what went wrong. Every run started fresh. Timers were brittle. There was no alert when things broke at 2am.

One conversation with Claude later, AgentMem was born.

No team. No funding. No prior coding knowledge required. Just a problem worth solving and the right tool to solve it.

If you have a problem worth solving, Claude can help you build it.


Made with ❤️ and Claude

About

"Your AI agents keep repeating mistakes. AgentMem fixes that. One file, zero cost."

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages