Last quarter I tried to automate lead follow‑up with a chain of GPT prompts and kept hitting walls: the agent forgot context, spawned duplicate tasks, and racked up API bills I couldn’t explain.
After tearing apart three failed attempts, I settled on a pattern that reliably turns a single prompt into a self‑correcting agent that logs every step and caps cost.
By the end of this guide you’ll be able to sketch, test, and deploy your own agent—or grab a pre‑built blueprint if you’d rather skip the build.
Why does my agent lose context after three steps?
Most tutorials show you how to string a few prompts together and call it an agent.
What they rarely mention is that each call is stateless unless you explicitly pass the prior output back in.
If you forget to re‑inject the conversation history, the model treats every request as a blank slate.
That’s why you see the agent repeat the same question or lose the lead’s name after the second follow‑up.
The fix is simple: store the conversation in a durable place—like a tiny SQLite table or a JSON file—and prepend it to every new prompt.
I use a single‑row table called agent_state with columns run_id, history, and updated_at.
Before each model call I read the row, concatenate the stored history to the user prompt, and after receiving the answer I write the new history back.
This pattern costs almost nothing in extra latency and eliminates the dreaded drift.
Concrete named example: building a lead‑nurture agent with OpenAI Assistants and SendGrid
Here’s the exact stack I run for a solo consulting business.
OpenAI Assistants API (the model backbone), SendGrid for email, and a lightweight Python wrapper.
First, create an assistant with the instruction:
You are a polite sales follow‑up bot. Use the supplied lead data to craft a short, personalized email. Never add fluff. Keep each message under 120 words.
Then attach a function called send_email that hits SendGrid’s v3 Mail Send endpoint.
The function expects three arguments: to, subject, body and returns a success flag.
Here’s the numbered step‑list I follow each morning:
- Query my CRM for leads that haven’t been touched in 48 hours.
- For each lead, fetch the current
agent_staterow (or create a blank one). - Build the prompt: “Lead name: {name}. Last contact: {date}. Goal: book a 15‑minute call.”
- Call the assistant with the prompt plus the stored history.
- Extract the assistant’s text output; treat it as the email body.
- Run the
send_emailfunction with the lead’s email, a subject line like “Quick question, {name}”, and the body. - If the function returns true, append the assistant’s output and the function result to the history and save the row.
- If the function fails, log the error, add a note to the history, and retry once after a ten‑second pause.
Each run consumes roughly 850 tokens of input and 320 tokens of output.
At today’s rate of $0.006 per 1 000 tokens, a single lead follow‑up costs about $0.007.
Running the agent for 200 leads a day adds up to $1.40—practically free.
I love the built‑in conversation state persistence in the Assistants API—once I turned it on, the agent remembered the lead’s name across five follow‑up messages without me re‑passing it.
My gripe? The Assistants API charges per token for both input and output, and the pricing page hides the fact that each function call adds a fixed 150‑token overhead, which blew my budget on a simple email‑sender until I spotted it in the usage dashboard.
(Which, yes, is annoying.)
