AI Agent Design Patterns: Build Reliable Agents Without Overengineering
You’ve tried chaining prompts together, but the agent forgets context, repeats itself, or stalls on edge cases. After reading this, you’ll be able to sketch a simple agent architecture, pick the right pattern for your use case, and wire it up with tools you already know.
Why does my agent lose track after three steps?
Most beginners stack prompts in a linear chain and hope the model remembers earlier details. In practice, the context window fills quickly and the model starts to hallucinate or drop facts. A simple fix is to externalize memory: store key facts in a separate store and retrieve them before each generation step.
Think of it like a notebook you keep beside you while you work. You write down the important bits, then glance at the notebook before you write the next sentence.
One‑sentence paragraph: External memory turns a fragile chain into a resilient loop.
You can implement this with a lightweight key‑value store like Redis or even a Google Sheet if you’re just testing. The pattern is called “Read‑Act‑Learn”: read the current state and memory, act with the model, learn by writing the outcome back to memory.
What most guides get wrong about agent memory
Many tutorials tell you to just increase the model’s context size or to use a fancy vector database. That advice misses the point: the bottleneck isn’t storage, it’s the retrieval logic. If you pull the wrong snippet, the agent gets confused.
I’ve seen guides recommend loading the entire conversation into the prompt every turn. That works until you hit the token limit, then the API returns an error and your workflow stops.
The better approach is to keep memory lean: store only the facts you need for the next decision, and use a simple lookup (like a hash map) to fetch them. This keeps token usage low and makes the agent predictable.
Concrete gripe: I once spent two hours debugging why my agent kept repeating the same answer, only to discover the tutorial had me appending the full transcript to the prompt each loop, blowing past the 4k limit on GPT‑4o‑mini.
A concrete example: building a lead‑qualifier with n8n workflows and GPT‑4o
Let’s walk through a real‑world flow you can copy today. We’ll use n8n for orchestration, the OpenAI API for reasoning, and a Google Sheet as our memory store.
Step 1: Receive a new lead via webhook (n8n catches the POST).
Step 2: Pull the lead’s name and company from the payload.
Step 3: Query the Google Sheet for any prior notes about that company (this is our external memory).
Step 4: Build a prompt that includes the lead info and the retrieved notes.
Example prompt (plain text):
You are a sales qualifier. Given the following lead details and any prior notes, decide if the lead is worth pursuing.
Lead: {{name}} from {{company}}
Prior notes: {{notes}}
Respond with either "QUALIFIED" or "NOT QUALIFIED" and a brief reason.
Step 5: Send the prompt to the OpenAI API (we’ll use gpt-4o-mini, priced at $0.0003 per 1k tokens in 2026).
Step 6: Parse the response. If qualified, update the Google Sheet with a timestamp and move the lead to a “Hot” tab; otherwise, log it as “Cold”.
Step 7: Reply to the webhook sender with the outcome (optional).
Concrete love: I love how n8n’s built‑in Google Sheets node lets me read and write without writing a single line of authentication code—just plug in your service account and you’re good.
Price mention with opinion: At $0.0003 per 1k tokens, running 500 qualifying calls a day costs under $0.05, which is basically free for a solo founder.
