DEV Community

Akın Coşkun
Akın Coşkun

Posted on

How I Built an AI SDR Agent That Finds Leads and Writes Personalized Cold Emails

TL;DR
I built LeadPilot — an AI-powered Sales Development Representative that automates lead discovery, company research, pain point analysis, and personalized cold email generation. A 4-agent AI pipeline does in 5 minutes what takes a human SDR 4+ hours. Here's how it works and what I learned building it.

What is an AI SDR Agent?
An SDR (Sales Development Representative) is the person who finds potential customers, researches them, and sends the first outreach message. It's the starting point of every sales pipeline.
An AI SDR agent automates this entire process. Instead of manually searching LinkedIn, reading company websites, and writing individual emails, AI does it all — faster, more consistently, and at scale.
LeadPilot handles 4 steps that would normally take hours:

Find leads matching your target criteria
Research each company by analyzing their website
Identify pain points relevant to your offering
Write personalized emails — not templates, genuinely personalized to each company

Why I Built This
I'm a freelancer. I need clients. The traditional approach — browsing job boards, sending generic proposals — is slow and competitive. Cold outreach works better, but it's time-consuming to do well.
I wanted a tool where I could say "Find SaaS companies with 10-50 employees that might need automation" and get back a list of companies with ready-to-send emails. So I built one.

The 4-Agent Architecture
LeadPilot uses a sequential AI pipeline. Each agent has a specific job, and the output of one feeds into the next.
Agent #1: Lead Finder
The user provides: target sector, company size, location, and their product/service. The Lead Finder uses Serper.dev (Google Search API) to find matching companies. It runs multiple search queries in parallel to cast a wide net, then AI extracts structured company data from the search results.
Input: "SaaS companies, 10-50 employees, US, N8N automation setup"
Output: List of 10 companies with names, websites, and descriptions.
Agent #2: Company Researcher
For each lead, this agent fetches the company's website using Cheerio (server-side HTML parsing) and extracts: what they do, their products, about page content, team information, and any public contact details.
This is where personalization starts. The researcher doesn't just grab the company name — it understands their business, their tech stack, their recent activity.
Agent #3: Pain Point Analyzer
This is the strategic brain. It takes the company profile and the user's product/service, then identifies specific pain points this company likely has. It also generates a match score (0-100) and the best "angle" for outreach.
For example: "This company uses Shopify but handles order notifications manually. Your N8N automation could save them 15+ hours/week on order processing workflows. Match score: 82."
Agent #4: Email Composer
The final agent writes a 3-email sequence for each lead:

Email 1 (Day 0): First touch. Opens with something specific about their company. States the problem. Offers the solution. Clear call-to-action.
Email 2 (Day 3): Follow-up. Different angle, social proof or case study reference.
Email 3 (Day 7): Last attempt. Short, direct, final compelling reason.

Every email is under 150 words. No corporate jargon. No "I hope this email finds you well." Each one references specific details about the company — because the previous agents already did the research.

The Tech Stack (Zero Cost)
LayerTechnologyCostFrontend + BackendNext.js 14 + Tailwind + shadcn/ui$0AI (Primary)Groq Llama 3.3 70B$0AI (Fallback)Google Gemini 2.0 Flash$0SearchSerper.dev (2,500 free searches/month)$0HTML ParsingCheerio$0AuthNextAuth.js v5$0DatabaseNeon PostgreSQL + Prisma$0DeployVercel$0
Total: $0/month.

Technical Challenges I Solved
Challenge 1: Long-Running Pipeline
A 10-lead campaign takes 3-5 minutes. You can't keep an HTTP connection open that long. My solution: fire-and-forget pattern. The API starts the pipeline in the background, immediately returns a campaign ID. The frontend polls /api/campaign/[id]/status every 2 seconds to get progress updates.
The user sees: "Finding leads... (3/10 found)" → "Researching companies... (5/10)" → "Composing emails... (9/10)" → "Complete!"
Challenge 2: AI Returning Invalid JSON
AI models sometimes return markdown-wrapped JSON, add explanations, or produce malformed structures. My solution: strict prompt instructions ("Respond ONLY with valid JSON"), JSON parse with try/catch, one retry with the same model, then fallback to the secondary model. If both fail, the lead is marked with an error but the pipeline continues.
Challenge 3: Website Scraping Reliability
Not every website plays nice. Some block scrapers (403), some redirect infinitely, some are entirely JavaScript-rendered (invisible to Cheerio). My solution: 10-second timeout on all fetches, User-Agent header to avoid blocks, graceful degradation (if scraping fails, the AI works with whatever the search result provided).
Challenge 4: Prompt Injection
Users enter free text (sector, product description, value proposition) that gets injected into AI prompts. A malicious user could try to manipulate the AI's behavior. My solution: input sanitization that strips control characters and potential injection patterns before they reach the prompt.
Challenge 5: SSRF Protection
The scraper fetches URLs from search results. Without protection, an attacker could trick it into fetching internal network resources. My solution: URL validation that blocks localhost, private IP ranges, and non-HTTP protocols before any fetch request.

What Makes Good Cold Emails
Building the Email Composer agent taught me a lot about cold outreach:
Personalization is everything. "I noticed your company uses Shopify" beats "Dear business owner" by 10x. The research agents exist specifically to feed the email agent with personalization material.
Shorter is better. Every email is capped at 150 words. Busy people don't read essays from strangers.
One CTA per email. "Would you be open to a 15-minute call?" — not "check our website, read our blog, follow us on LinkedIn, and also here's a PDF."
The 3-email sequence matters. Most responses come from email 2 or 3, not email 1. Persistence (without being annoying) is key. Day 0, Day 3, Day 7 is the sweet spot.

Real Output Example
Campaign: "Find marketing agencies in the US that need AI automation"
Lead: Bright Spark Digital (fictional example)
Email 1 — Day 0:
Subject: "Quick question about your client reporting"
"Hi Sarah, I noticed Bright Spark Digital manages campaigns across Google Ads and Meta for multiple clients. Reporting across platforms usually eats up 5-10 hours per week for agencies your size. I build custom AI automation workflows that pull data from all ad platforms into unified dashboards — automatically. Would you be open to a quick 15-minute chat about how this could work for your team?"
Email 2 — Day 3:
Subject: "Re: your client reporting"
"Hi Sarah, following up on my previous note. I recently helped a similar-sized agency reduce their weekly reporting time from 8 hours to 20 minutes using N8N automation. Happy to share exactly what we built. Worth a quick conversation?"
Email 3 — Day 7:
Subject: "Last note"
"Hi Sarah, I know you're busy. One quick question: if you could automate one repetitive task in your agency, what would it be? I might already have a solution built. Either way, no hard feelings — just wanted to make sure this didn't slip through the cracks."

Try It

Live app: leadpilot-ashy.vercel.app
Source code: github.com/akincskn/leadpilot

Create a campaign, define your target market, and get personalized leads with email sequences in minutes.

What's Next
LeadPilot is part of a larger ecosystem of AI tools I'm building. If you're interested in AI automation, SDR tooling, or building SaaS with zero-cost infrastructure, follow me for more.
Other tools in the ecosystem:

RivalRadar — AI competitor analysis
GEO Analyzer — AI search optimization scoring
Portfolio MCP Server — Query my portfolio via AI assistants

I'm Akın Coşkun, a full-stack developer and AI automation specialist from Turkey. I build production SaaS applications with zero-cost infrastructure. Find me on GitHub or check my portfolio.

Top comments (0)