Learn how to build, test, and deploy open source AI agents for lead gen and outreach, with real prompts, cost breakdowns, and debugging tips.
Open source AI agents: building your first production-ready workflow
Last month I needed to pull fresh leads from LinkedIn, enrich them with company data, and send personalized emails—all without hiring a VA or paying for a brittle Zapier chain. I decided to try an open source AI agent that could browse, extract, and write copy on its own. By the end of this guide you’ll have a working agent that does exactly that, and you’ll know where it tends to trip up.
It’s not magic.
How do you keep the agent from forgetting context between steps?
Most agents lose track of what they’ve done after each action because they rely on a short‑term memory that gets wiped. I ran into this when the agent would fetch a LinkedIn profile, then immediately forget the name while trying to write the email. The fix is to give the agent a persistent memory store that survives loops. I use a lightweight vector database like **FAISS** for the first mention, then switch to **Chroma** when I need metadata filtering. You don’t need a heavyweight setup; a local folder with a few megabytes is enough for a solo operator.
Here’s the pattern I follow: after each tool call, I embed the output and add it to the vector store. Before the next step I query the store for the most relevant snippets and inject them into the prompt. This keeps the agent aware of the lead’s name, company, and any details it scraped earlier. It’s a simple loop, but it stops the dreaded “who are we talking to again?” mistake.
(which, yes, is annoying) the first time I tried this I forgot to normalize the embeddings and the similarity scores were all over the place. Once I switched to cosine similarity with L2‑normalized vectors the recall jumped from 40% to over 90%.
What most guides get wrong
Many tutorials treat the agent as a black box and focus only on the prompt engineering. They show you a fancy prompt that supposedly makes the agent “think” and then call it a day. In reality the prompt is just one piece; the tool chain, memory, and error handling are where the work lives. I’ve seen guides that skip the error‑handling step entirely, leaving the agent to crash when a website returns a 403 or an API rate limit hits.
⚡
Recommended Reading
Prompt Engineering for Profit
50 Tested Templates
50 tested prompt templates for content, copywriting, and automation. Copy, paste, earn.
The other common mistake is to assume the free tier of any LLM provider is enough for anything beyond a toy demo. I’ve seen people burn through their OpenAI credits in an hour because they didn’t token‑count the prompts. The free tier is fine for a few test runs, but if you plan to run the agent daily you’ll need to budget for usage.
Concrete example: building a lead‑gen agent with CrewAI and SerpAPI
Let’s walk through a real workflow I use for outbound outreach. The agent does three things: search for decision‑makers on LinkedIn via SerpAPI, pull the company description from the website, and write a personalized cold email.
First, I define the agents in a YAML file. Here’s a snippet (bolded on first mention):
- Create a folder called
lead_agent and add a file agents.yaml.
- Define a researcher agent that uses SerpAPI:
researcher:
role: Lead researcher
goal: Find LinkedIn profiles of decision makers at target companies
backstory: You are an expert at using search APIs to locate professional profiles.
tools:
- name: serpapi_search
description: Search LinkedIn via SerpAPI
args:
engine: linkedin
api_key: {{ env.SERPAPI_KEY }}
Note: only the first mention of **CrewAI** is bold; later references are plain.
Next, I add a Writer agent that takes the researcher’s output and crafts an email:
writer:
role: Email copywriter
goal: Write a short, personalized cold email based on the researcher’s findings
backstory: You have a knack for turning raw data into concise, friendly outreach.
tools:
- name: llm_call
description: Call the LLM to generate text
args:
model: gpt-4o
temperature: 0.7
max_tokens: 150
I wire them together in a simple flow: researcher → writer → output. The whole thing runs in under two minutes on my laptop.
Now, the cost. Each SerpAPI search costs about $0.005 per request. The LLM call for the email uses roughly 300 tokens (prompt + completion). At $0.06 per 1K tokens for GPT-4o that’s $0.018 per email. If I run 100 leads a day the API bill is around $2.30, which I find reasonable for the time saved.
I love how CrewAI lets you define role‑based agents with simple YAML, which cut my setup time in half. The concrete gripe I have is that the default memory tool in CrewAI resets after each loop, forcing me to add the vector store step myself. It’s a small annoyance, but it tripped me up the first time I tried to scale beyond a single lead.
How to debug when this breaks
When the agent stops producing useful output, I start with the logs. CrewAI prints each tool call and its result to stdout, so I tail the log file while the agent runs. If I see a blank result from the researcher, I check the SerpAPI response—often it’s a rate‑limit error or a malformed query.
If the writer returns gibberish, I look at the prompt that was sent to the LLM. I print the final prompt right before the llm_call tool fires. More than once I’ve discovered that the researcher’s output contained stray HTML tags that broke the prompt formatting.
Another common failure mode is the vector store returning unrelated snippets because the similarity threshold is too low. I adjust the threshold in the query step and rerun. A quick way to test is to run the agent on a single known lead and verify that the email contains the correct company name.
If everything looks fine but the agent still loops forever, I add a hard step counter. After five iterations I force a break and inspect the state. This prevents the agent from getting stuck in a retry loop when a tool keeps failing.
Finally, I keep a cheap fallback: if the LLM call fails, I send a templated email instead of dropping the lead entirely. It’s not as personalized, but it keeps the pipeline moving.
Price‑to‑value take
For a solo operator the monthly cost of running this agent stays under $10 if you limit yourself to a few hundred leads. The OpenAI API is the biggest line item; at $0.06 per 1K tokens for GPT-4o I find it pricey for high‑volume testing, but the free tier is enough for early experiments. If you hit the limit, switching to a local model like **Mistral** via Ollama drops the per‑token cost to zero, though you trade off some fluency.
Overall, the time saved on manual lead hunting and copywriting pays for the API bills many times over. I’d say $29/mo is fair for the peace of mind of having a working agent that runs while you sleep.
If you want the deep cut on this, deeper coverage of AI agent platforms.
If you’d rather skip the build and deploy a working version in an afternoon, we’ve packaged this workflow as a blueprint at deepusecase.com/vault/ai-agent-builder-kit.