Build an Autonomous AI SDR Engine in n8n with CRM Memory
I built an autonomous, end-to-end AI Sales Development Representative (SDR) engine entirely in n8n. On a scheduled trigger, the agent calculates targeting parameters, reads long-term CRM memory to avoid duplicate outreach, searches for and qualifies prospective leads, scrapes company websites for buying signals, drafts tailored outreach emails, writes structured relational data to PostgreSQL, and reports execution summaries through Telegram—with zero manual intervention. The system is currently deployed in production for a B2B agricultural export business, generating qualified international wholesale leads on a recurring schedule. Most AI automations rely on simple linear scripts or break down when handling complex agentic tool workflows. This system addresses three common failure points: - High API costs: Re-sending large system prompts and tool schemas on every agent iteration drains tokens. - Context blindness: Agents without memory of previous contacts can send duplicate outreach. - Database crashes: Agents may hallucinate ENUM values or fail to insert nested one-to-many arrays into relational tables. The workflow uses a Cloudflare-proxied Claude Sonnet 4.6 model with prompt caching, persistent CRM memory reads, and a fault-tolerant parallel database-write architecture. The stack includes n8n as the orchestrator; Claude Sonnet 4.6 through a Cloudflare Worker proxy as the LLM core with ephemeral prompt caching; PostgreSQL for CRM contacts, intelligence, and outreach tables with custom ENUMs; SerpAPI for prospect discovery; Firecrawl for website content extraction; and Telegram for execution reporting. The workflow exposes these tools to the n8n agent: - `read_relationship_memory`: Read-only SQL access to historical contact and outreach data, preventing duplicate prospecting. - `Lead_Finder`: Searches for and identifies target prospects by country and sector. - `Scrape_Website_Content`: Extracts website content, buyer-intent signals, and objections from discovered domains. - `write_relationship_memory`: Writes leads, intelligence facts, and drafted emails to Postgres in one resilient call. Step-by-step: 1. A Schedule Trigger feeds a JavaScript “Country Calculator” node that resolves the day’s targeting parameters—region and industry focus—using ISO week rotation. This cycles outreach across markets automatically. 2. The AI Agent connects to an OpenAI Chat Model node whose Base URL points to a custom Cloudflare Worker. The worker translates OpenAI-formatted requests into Anthropic’s Messages API, enabling Claude Sonnet 4.6 while injecting ephemeral cache-control headers into the system prompt and tool definitions to reduce repeat-token costs. 3. Before researching, the agent calls `read_relationship_memory` to check relationship status and outreach history, preventing duplicate contact attempts. 4. `Lead_Finder` searches target sectors in the day’s region and returns seven filtered candidates. `Scrape_Website_Content` then visits each domain, extracts clean page text, and surfaces offerings, value propositions, and likely objections. 5. The workflow writes nested one-to-many data—multiple facts and one outreach log per contact—without item duplication or ENUM crashes. The tool schema requires a strict JSON array with exact ENUM string choices spelled out in the description. 6. A sub-workflow triggered by “When Executed by Another Workflow” splits the array, then flattens nested `contact.*` fields to root keys using JavaScript. 7. An upsert query, `ON CONFLICT (email) DO UPDATE`, writes the contact, increments `email_count` for repeats, and returns `contact_id`. 8. A “Re-attach Context” node merges `contact_id` back with the original intelligence array and outreach payload because n8n strips extra data through single-row database nodes. 9. Two parallel branches run: one inserts the outreach log with `ON CONFLICT DO NOTHING`, while the other splits and inserts each intelligence fact with defensive ENUM sanitization. This eliminates crashes and duplicate rows during retries. 10. The agent’s final output triggers a Telegram message summarizing the discovered leads, extracted facts, and drafted emails, sent directly to the operator’s phone. The result is a production-grade, self-healing AI outbound pipeline running with zero manual intervention. It maintains CRM data integrity, avoids duplicate outreach, and uses prompt caching to keep LLM costs low at scale.
0 comments