@srk0102/engram

Primitives for pattern-cached LLM decisions on any Postgres backend. Bring your own brain, prompt, schema, and cache key. Engram does storage, retrieval, and caching.


Keywords
behavioral-caching, pattern-memory, bot-detection, api-protection, postgres, express, fastify, hono, nestjs, engram, churn-detection, fraud-detection, mit-license, open-source, postgresql, self-learning, supabase
License
MIT
Install
npm install @srk0102/engram@0.2.0

Documentation

Engram

Engram

Your API learns to defend itself.

version tests scp supabase


The problem

You built an API. Users pay for it. Bots don't.

A bot scraping your data costs you the same compute as a real user. A fraudster with a stolen card burns through your paid features in minutes. A churning user silently walks away with unused credits.

Rate limiters don't help. They count requests, not behavior. A smart bot sends 29 requests per minute and passes your 30/min limit. A real user doing 5 actions in quick succession gets blocked.

The solution

Engram watches behavior, not requests.

A real user has an account age, a payment history, a browsing pattern. A bot has a 0-day account doing 15 uploads per hour with no user agent.

Engram classifies the behavior shape. Stores the decision. Next request with the same shape - decision served from cache. No classification needed.

One brain call teaches. The pattern store remembers.

What How long Cost
First request from a new behavior shape ~100ms ~$0
Every request after that with same shape <1ms $0

What happens to different users

User Engram sees Decision Reaches your API?
Real user, 30 days old, 2 actions today Normal behavior allow Yes
Bot, 0 days old, 15 requests this hour Burst velocity + no UA fraud (403) No
Scraper, fake Chrome UA, 8 requests/hr Young account + high velocity fraud (403) No
Paid user, inactive 20 days, has credits Idle with unspent credits churn_risk Yes (+ team notified)

Bots never reach your database. Fraudsters never hit your backend. Real users never notice Engram exists.

Install

Option A: As a Postgres extension (recommended)

-- Enable pg_tle (one time per project)
create extension if not exists pg_tle;

-- Register Engram (paste supabase/tle-register.sql in SQL Editor)
-- Then:
create extension engram;

Option B: Plain SQL

-- Paste supabase/install.sql into your SQL Editor. Run it.

After either option:

1. Go to Settings → API → Exposed schemas → add engram.

2. Verify:

select engram.classify('{"account_age_days":0,"uploads_last_hour":15,"ua_class":"missing"}'::jsonb);
-- → {"decision":"fraud","confidence":0.95}

Done. No new infrastructure. No code changes. Uses your existing Postgres.

Use it

-- Classify a request
select engram.classify('{"account_age_days":45,"ua_class":"browser"}'::jsonb);

-- Full flow: check cache → classify → learn → return
select engram.decide('{"account_age_days":45,"ua_class":"browser"}'::jsonb, 'my_app');

-- See everything Engram has learned
select engram.dashboard();

-- List all patterns, all visits, all flagged users
select engram.list_patterns();
select engram.list_visits(null, 'fraud', 20);
select engram.list_churn_queue();

Use it in Node.js

import { withEngram } from './lib/engram'

export const POST = withEngram(async (request) => {
  // Bots and fraudsters never reach this line.
  // Engram already returned 403/429 for them.
  const data = await handleRequest(request)
  return Response.json(data)
})

Fail-open by design. If Supabase is down, your handler runs anyway. Engram never blocks a real user because it crashed.

How it learns

Request 1 (new shape):  classify → learn → return "fraud"
Request 2 (same shape): cache hit → return "fraud" instantly
Request 3 (same shape): cache hit → return "fraud" instantly
...
Request 1000:           still cached. Brain never called again.

Patterns get stronger with correct decisions. Wrong decisions weaken them. Below 20% confidence, patterns auto-evict and the brain re-classifies fresh.

Documentation

Read the full docs →

Covers: all 18 functions, schema reference, classification rules, Node.js integration, security model, retention policies, custom rule examples.

Links

Contributing

Read the contributing guide

License

MIT