Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(api): stop page-load reads from draining the agent-creation rate limit

fudjia9c9f54765b69
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.js
index 9a6f030ca..4909e63c1 100644
--- a/api/_lib/rate-limit.js
+++ b/api/_lib/rate-limit.js
@@ -108,7 +108,7 @@ function getLimiter(name, opts) {
const key = `${name}:${opts.limit}:${opts.window}`;
if (limiters.has(key)) return limiters.get(key);
if (opts.local) {
- const lim = memoryLimiter(opts);
+ const lim = memoryLimiter(name, opts);
limiters.set(key, lim);
return lim;
}
@@ -117,7 +117,7 @@ function getLimiter(name, opts) {
// Redis outage (degraded per-instance cap) rather than lock everyone out.
const lim =
opts.degradeToMemory || !(opts.critical && IS_PRODUCTION)
- ? memoryLimiter(opts)
+ ? memoryLimiter(name, opts)
: failClosedLimiter(opts);
limiters.set(key, lim);
return lim;
@@ -175,7 +175,7 @@ function resilientLimiter(rl, name, opts) {
// log in — is a worse outcome than a weaker, per-instance brute-force cap, and
// bcrypt already bounds per-request cost on the credential path. Money-moving
// buckets keep failing closed: there, unbounded spend is worse than a 503.
- const memFallback = opts.degradeToMemory ? memoryLimiter(opts) : null;
+ const memFallback = opts.degradeToMemory ? memoryLimiter(name, opts) : null;
return {
async limit(id) {
try {
@@ -225,22 +225,28 @@ function sweepMemoryBuckets(now) {
}
}
-function memoryLimiter({ limit, window }) {
+// `name` namespaces the shared memoryBuckets map. Without it every fallback
+// limiter counts against ONE array per IP, so distinct buckets contaminate
+// each other the moment more than one degrades to memory (e.g. a Redis outage
+// putting auth:ip and authed:read:ip in the same counter — page reads would
+// then exhaust the login budget).
+function memoryLimiter(name, { limit, window }) {
const ms = parseWindowMs(window);
if (ms > maxMemoryWindowMs) maxMemoryWindowMs = ms;
return {
async limit(id) {
+ const key = `${name}${id}`;
const now = Date.now();
sweepMemoryBuckets(now);
- const bucket = memoryBuckets.get(id) || [];
+ const bucket = memoryBuckets.get(key) || [];
const cutoff = now - ms;
const kept = bucket.filter((t) => t > cutoff);
if (kept.length >= limit) {
- memoryBuckets.set(id, kept);
+ memoryBuckets.set(key, kept);
return { success: false, limit, remaining: 0, reset: kept[0] + ms };
}
kept.push(now);
- memoryBuckets.set(id, kept);
+ memoryBuckets.set(key, kept);
return { success: true, limit, remaining: limit - kept.length, reset: now + ms };
},
};
@@ -270,6 +276,23 @@ export const limits = {
getLimiter('auth:ip', { limit: 50, window: '10 m', critical: true, degradeToMemory: true }).limit(ip),
registerIp: (ip) =>
getLimiter('register:ip', { limit: 5, window: '1 h', critical: true, degradeToMemory: true }).limit(ip),
+ // Session-scoped READS that fire on ordinary page loads (/api/me, home feed,
+ // credits balance, profile pages, wallet balance lookups). These borrowed the
+ // strict credential `authIp` bucket for years, which meant a few minutes of
+ // normal browsing drained the same 50/10m budget that gates real writes —
+ // users then got 429'd on the one request that mattered (e.g. creating an
+ // agent). Reads are cheap DB lookups behind auth, so the ceiling is generous:
+ // it only needs to stop scripted scraping, not human navigation.
+ authedReadIp: (ip) =>
+ getLimiter('authed:read:ip', { limit: 300, window: '5 m', degradeToMemory: true }).limit(ip),
+ // Agent creation (POST /api/agents) mints real EVM + Solana wallets, so it
+ // keeps a strict ceiling — but a DEDICATED one. Sharing `authIp` meant page
+ // browsing could exhaust the budget before the user ever pressed "create",
+ // failing the flow after the avatar upload had already succeeded. 20/10m is
+ // far more than any human setting up agents needs while still capping
+ // scripted wallet-mint spam per IP.
+ agentCreateIp: (ip) =>
+ getLimiter('agent:create:ip', { limit: 20, window: '10 m', critical: true, degradeToMemory: true }).limit(ip),
// CAPTCHA-verified login bucket. When a user solves the Altcha proof-of-work
// puzzle (api/auth/captcha.js) they receive a signed bypass token that routes
// their login through this separate bucket instead of authIp. It is intentionally
diff --git a/api/agents.js b/api/agents.js
index a599bae6f..0f200f710 100644
--- a/api/agents.js
+++ b/api/agents.js
@@ -291,9 +291,10 @@ async function handleCreate(req, res) {
const auth = await resolveAuth(req);
if (!auth) return error(res, 401, 'unauthorized', 'sign in or provide a bearer token');
// Creation mints real wallets — same CSRF gate as PUT/PATCH/DELETE (bearer
- // callers are exempt inside requireCsrf) plus a per-IP rate limit.
+ // callers are exempt inside requireCsrf) plus a dedicated per-IP rate limit
+ // (NOT the shared auth:ip bucket, which page-load reads used to drain).
if (!(await requireCsrf(req, res, auth.userId))) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.agentCreateIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const scopeErr = requireScopeForMutation(auth, res, 'avatars:write');
if (scopeErr) return scopeErr;
diff --git a/api/agents/by-wallet.js b/api/agents/by-wallet.js
index 00c95eb4d..eceff2f91 100644
--- a/api/agents/by-wallet.js
+++ b/api/agents/by-wallet.js
@@ -15,7 +15,7 @@ export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const url = new URL(req.url, 'http://x');
diff --git a/api/agents/nfts.js b/api/agents/nfts.js
index 1b6fbba52..e213b65c9 100644
--- a/api/agents/nfts.js
+++ b/api/agents/nfts.js
@@ -39,7 +39,7 @@ export default wrap(async (req, res) => {
return error(res, 403, 'insufficient_scope', 'requires mcp or profile scope');
}
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const op = req.query.op || 'portfolio';
diff --git a/api/credits/index.js b/api/credits/index.js
index f51a5c3f8..3033c7c62 100644
--- a/api/credits/index.js
+++ b/api/credits/index.js
@@ -29,7 +29,7 @@ export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const user = await resolveUser(req, res);
diff --git a/api/me.js b/api/me.js
index 1606f73b3..bb351778f 100644
--- a/api/me.js
+++ b/api/me.js
@@ -25,7 +25,7 @@ export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const auth = await resolveAuth(req);
diff --git a/api/sniper/history.js b/api/sniper/history.js
index d281664b8..c4234a0b5 100644
--- a/api/sniper/history.js
+++ b/api/sniper/history.js
@@ -40,7 +40,7 @@ export default wrap(async (req, res) => {
const userId = await resolveUserId(req);
if (!userId) return error(res, 401, 'unauthorized', 'sign in to view your trade history');
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const params = new URL(req.url, `http://${req.headers.host || 'x'}`).searchParams;
diff --git a/api/sniper/journal.js b/api/sniper/journal.js
index ee483e6ac..6cfae34dd 100644
--- a/api/sniper/journal.js
+++ b/api/sniper/journal.js
@@ -36,7 +36,7 @@ export default wrap(async (req, res) => {
const userId = await resolveUserId(req);
if (!userId) return error(res, 401, 'unauthorized', 'sign in to view your trade journal');
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const params = new URL(req.url, `http://${req.headers.host || 'x'}`).searchParams;
diff --git a/api/threews/me.js b/api/threews/me.js
index 2e690a69e..b198593fe 100644
--- a/api/threews/me.js
+++ b/api/threews/me.js
@@ -23,7 +23,7 @@ export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const auth = await resolveAuth(req);
diff --git a/api/users/[username].js b/api/users/[username].js
index 9f17c9cac..2d6a6844f 100644
--- a/api/users/[username].js
+++ b/api/users/[username].js
@@ -12,7 +12,7 @@ export default wrap(async (req, res) => {
return error(res, 400, 'validation_error', 'invalid username');
}
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const [user] = await sql`
diff --git a/api/users/[username]/collectibles.js b/api/users/[username]/collectibles.js
index e7ff1e5e3..bdff4c75b 100644
--- a/api/users/[username]/collectibles.js
+++ b/api/users/[username]/collectibles.js
@@ -43,7 +43,7 @@ export default wrap(async (req, res) => {
return error(res, 400, 'validation_error', 'invalid username');
}
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const [user] = await sql`
diff --git a/api/users/[username]/follows.js b/api/users/[username]/follows.js
index a7034b602..c0d3afad1 100644
--- a/api/users/[username]/follows.js
+++ b/api/users/[username]/follows.js
@@ -27,7 +27,7 @@ export default wrap(async (req, res) => {
const limit = Math.min(100, Math.max(1, parseInt(req.query.limit, 10) || 50));
const offset = Math.max(0, parseInt(req.query.offset, 10) || 0);
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const [target] = await sql`
diff --git a/api/users/earnings.js b/api/users/earnings.js
index 74051c421..5e24faf2a 100644
--- a/api/users/earnings.js
+++ b/api/users/earnings.js
@@ -12,7 +12,7 @@ export default wrap(async (req, res) => {
if (!session && !bearer) return error(res, 401, 'unauthorized', 'sign in required');
const userId = session?.id ?? bearer.userId;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const service = new MonetizationService(userId);
diff --git a/api/users/lookup.js b/api/users/lookup.js
index fea5fab77..0662dcbe7 100644
--- a/api/users/lookup.js
+++ b/api/users/lookup.js
@@ -19,7 +19,7 @@ export default wrap(async (req, res) => {
if (!session && !bearer) return error(res, 401, 'unauthorized', 'sign in required');
const callerId = session?.id ?? bearer.userId;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const q = new URL(req.url, 'http://x').searchParams.get('q');
diff --git a/api/users/me/feed.js b/api/users/me/feed.js
index 7925aede8..7d7600b90 100644
--- a/api/users/me/feed.js
+++ b/api/users/me/feed.js
@@ -26,7 +26,7 @@ export default wrap(async (req, res) => {
const viewer = await getSessionUser(req).catch(() => null);
if (!viewer) return error(res, 401, 'unauthorized', 'sign in to view your feed');
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const limit = Math.min(50, Math.max(1, parseInt(req.query.limit, 10) || 30));
diff --git a/api/users/referrals.js b/api/users/referrals.js
index ad719dba5..aa79f696d 100644
--- a/api/users/referrals.js
+++ b/api/users/referrals.js
@@ -21,7 +21,7 @@ export default wrap(async (req, res) => {
if (!session && !bearer) return error(res, 401, 'unauthorized', 'sign in required');
const userId = session?.id ?? bearer.userId;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const card = await getMembershipCard(userId);
diff --git a/api/wallet/balances.js b/api/wallet/balances.js
index 01470935d..37b661d6d 100644
--- a/api/wallet/balances.js
+++ b/api/wallet/balances.js
@@ -16,7 +16,7 @@ export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
- const rl = await limits.authIp(clientIp(req));
+ const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
let body;