Chapter 01 · Agent Email Inbox
Subchapter 1.1
references/advanced-patterns.mdMarkdown4 KBView on GitHub
Prevent any single sender from overwhelming your agent with emails:
const rateLimiter = new Map
Prevent token stuffing by truncating oversized email content:
const MAX_BODY_LENGTH = 10000; // Prevent token stuffing
function truncateContent(content: string): string {
if (content.length > MAX_BODY_LENGTH) {
return content.slice(0, MAX_BODY_LENGTH) + '\n[Content truncated for security]';
}
return content;
}Before analyzing email content for safety, strip quoted reply threads. Old instructions buried in > quoted sections or On [date], [person] wrote: blocks could contain unintended directives hidden in legitimate-looking reply chains.
function stripQuotedContent(text: string): string {
return text
// Remove lines starting with >
.split('\n')
.filter(line => !line.trim().startsWith('>'))
.join('\n')
// Remove "On ... wrote:" blocks
.replace(/On .+wrote:[\s\S]*$/gm, '')
// Remove "From: ... Sent: ..." forwarded headers
.replace(/^From:.+\nSent:.+\nTo:.+\nSubject:.+$/gm, '');
}This is critical for Level 3+ security. Even emails from trusted senders can contain quoted sections with malicious content.
Cause: Resend SDK version too old — resend.webhooks.verify() was added in recent versions.
Fix: Update to the latest SDK:
npm install resend@latestOr use the Svix fallback (see webhook-setup.md).
Cause: Resend SDK version too old — emails.receiving.get() requires a recent SDK.
Fix:
npm install resend@latest
# Verify version:
npm list resendPossible causes:
data.signing_secret). If you’ve lost it, delete and recreate the webhook to get a new one.express.raw({ type: 'application/json' }) on the webhook route, not express.json().resend@latest.Cause: Free ngrok tunnels time out and change URLs on restart. Fix: Restart ngrok, then delete and recreate the webhook via the API with the new tunnel URL. Better: Use Tailscale Funnel or deploy to production.
/webhook)curl https://<your-tunnel-url>ALLOWED_SENDERS listconsole.log('Sender:', event.data.from.toLowerCase())This is expected behavior. The webhook delivers a notification to the user, who then instructs the agent how to respond. This is the safest approach — the user reviews each email before the agent acts on it.