Build AI Loops, Not Prompts: A Complete Guide with Runnable Code
This guide explains how to build autonomous AI loops with triggers, judgment, gates, and memory instead of one-off prompts, providing a complete Python implementation of an outbound engine that learns from outcomes and runs on a schedule.
Prompts Answer. Loops Run.
When you write a prompt, you are the trigger, the judge, the actor, and the memory. You decide when to start, you evaluate the output, you take action, and you are the only place the result is remembered — usually forgotten by Friday. A loop moves those four jobs out of your head and into code: something triggers it without you, the model makes one judgment, an action happens or waits for your approval, and every run is recorded so the next run is smarter. That is why a clever prompt that looks great in a demo collapses by week two — it was never a system, just you working faster with help.
Prompts end. Loops come back.
Every Loop Has Four Parts
Regardless of the task, the shape is always the same:
Trigger. Something that starts the loop without you remembering — a cron job, a new data row, a market signal, an inbound reply.
Judgment. The one decision that actually needs the model. Not the whole task, just that decision. Keep everything around it deterministic so the model spends its intelligence on the hard judgment.
Gate. What the loop can do on its own versus what must wait for your nod. Read, classify, draft, score: let it run. Send, pay, publish, delete: put a human in front.
Memory. A record the loop writes every run and reads next run. Skip it and your automation stays stupid forever.
The rule that stitches them together: the model is a step, not the whole loop. When you hand the entire task to the model in one giant prompt, you get something that fails in undebuggable ways. When the model is just a labeled step, failure has an address.
Every loop has four parts: Trigger, Judgment, Gate, Memory.
Before filling in the details, here is the skeleton. Every loop in this guide, and most loops you will ever build, is a version of this:
def loop(trigger, judge, act, record):
for item in trigger(): # 1. Something triggers it
verdict = judge(item, record) # 2. Model decides one thing
if verdict["act"]: # 3. Gate
act(item, verdict, record)
record.write(item, verdict) # 4. Memory, every single timeStart Building
The full example here is something I actually run at Sortlist — about fourteen agents, roughly four hundred dollars a month. It is a signal-based outbound engine. Every night it reads company activity, judges who is worth a message and why, drafts the message, checks it, and leaves me a ranked shortlist in the morning. It is exactly the four-part loop above.
Outbound loop, end-to-end: Sense, Score, Draft & Check, Record & Learn.
One Model Call
Everything builds on a single function: it sends a step's task to the model and gets back structured data. Point it at any provider — OpenAI, GLM, a local model via Ollama — they all use the same API so the rest of the code never changes.
import json, os
from openai import OpenAI
client = OpenAI(
base_url=os.environ.get("MODEL_BASE_URL", "https://api.openai.com/v1"),
api_key=os.environ.get("MODEL_API_KEY"),
)
MODEL = os.environ.get("MODEL", "gpt-5.4-mini")
def call_model(system, payload):
"""Input a step's task, output validated JSON."""
resp = client.chat.completions.create(
model=MODEL,
messages=[
{"role": "system", "content": system},
{"role": "user", "content": json.dumps(payload)},
],
response_format={"type": "json_object"},
temperature=0.3,
)
return json.loads(resp.choices[0].message.content)One thing that saves money later: cheap models are enough for classification steps; only judgment and drafting need the stronger model. You can set MODEL per step if you want. The first-pass classifier can run on the cheapest tier.
Memory First
Build the record first, then the stages, because every stage reads and writes it. Keep it simple. A JSON file keyed by company is enough to feel the difference. Migrate to a database when it hurts, not before.
from datetime import datetime, timezone
RECORD_PATH = "record.json"
def load_record():
return json.load(open(RECORD_PATH)) if os.path.exists(RECORD_PATH) else {}
def save_record(record):
json.dump(record, open(RECORD_PATH, "w"), indent=2)
def history_for(record, account):
return record.setdefault(account, {"signals": [], "touches": []})
def days_since_last_touch(hist):
touches = hist.get("touches", [])
if not touches:
return "never"
last = max(t["date"] for t in touches)
return (datetime.now(timezone.utc) - datetime.fromisoformat(last)).daysThis is the part most people skip, and skipping it is exactly why the third funding signal looks exactly the same whether the company is a stranger or someone you have already emailed twice with no reply. The record tells you the difference.
Part 1, Trigger: Turn Activity into Signals
Detection is not "monitor everything." It reads a raw activity stream and pulls out the few real company actions. This step turns noise into structured signals the rest of the loop can use.
EXTRACT = """You read raw activity and extract buying signals. Ignore anything that is not a real company action.
For each real signal return: account, bucket (one of funding|job|company|social|first_party),
summary (one factual sentence citing specifics: amount, role, date), source.
A signal is a concrete, dateable action, not a vague mention or opinion. A company can produce multiple signals.
Output only JSON: {"signals":[{"account":"","bucket":"","summary":"","source":""}]} """
def extract(activity_text):
return call_model(EXTRACT, {"text": activity_text}).get("signals", [])Part 2, Judgment: Decide Who Gets a Message
This is the only call that truly needs the model, and the only place I let it reason freely.
A signal is not a reason to send. It is a question: Is this account worth messaging right now, why this week, and which play. It answers based on the new signals and the account's full history.
SCORE = """You score a single account for a signal-based outbound engine. Judge whether it is worth a message right now.
Input: icp, new_signals, history, days_since_last_touch.
80-100: Strong fit with high-intent signal: funding, budget-holder hire, or two signals within a week.
50-79: Good fit, one solid signal. 1-49: Weak fit or single soft signal.
Rule: If days_since_last_touch < 7, prefer nurture or skip, never as first touch.
why_now must cite the real trigger, never a generic value prop. Skip is a normal answer; most accounts should skip.
Output only JSON: {"score":0-100,"why_now":"citing the trigger","play":"first_touch|follow_up|nurture|skip"}"""
def score(icp, new_signals, hist):
return call_model(SCORE, {
"icp": icp,
"new_signals": new_signals,
"history": hist,
"days_since_last_touch": days_since_last_touch(hist),
})Part 3, Action: Write, Then Gate
The writing step opens with what the company actually did, connects it to a problem you solve, and makes one small ask. Then a second pass reads the draft like a skeptic. This check is the first half of the gate, and it is exactly what stops an autonomous engine from sending confident but wrong claims to your whole list.
WRITE = """You write a first message for a signal-based outbound engine.
Input: trigger, why_now, play, problem.
First line states what they just did. Never use a template opener. Connect the trigger to the problem in one sentence.
One small ask: fifteen minutes or a one-pager. Three to five sentences, plain language, varied sentence length.
No false urgency, no "just circling back", no em-dashes.
Output only JSON: {"subject":"6-9 words","body":"3-5 sentences"}"""
CHECK = """You are the final pre-send check. Default to wrong until it proves itself right.
Input: trigger, draft.
Every claim backed by trigger, no hallucinated facts. First line states the real trigger.
One low-friction ask. Plain language, no em-dashes.
If it passes, return as-is. If it fails, fix it and say what was wrong.
Output only JSON: {"pass":true,"reason":"what was wrong or 'clean'","subject":"","body":""}"""
def write(trigger, verdict, problem):
return call_model(WRITE, {
"trigger": trigger, "why_now": verdict["why_now"],
"play": verdict["play"], "problem": problem,
})
def check(trigger, draft):
return call_model(CHECK, {"trigger": trigger, "draft": draft})The second half of the gate is you. The loop never sends directly. It queues a clean draft and waits for a "yes." That single decision — draft unattended, send only on approval — is what lets you run this every night without losing sleep.
Wire One Run Together
Now chain them. This is the nightly run, in order: extract, group by account, score each account, drop the skips, draft and check messages for the rest, then produce a ranked shortlist. The record writes on every account, whether it earned a message or not.
def run(activity_text, icp, problem):
record = load_record()
signals = extract(activity_text)
by_account = {}
for s in signals:
by_account.setdefault(s["account"], []).append(s)
shortlist = []
for account, new_signals in by_account.items():
hist = history_for(record, account)
hist["signals"].extend(new_signals) # 4. Memory, always write
verdict = score(icp, new_signals, hist) # 2. Judgment
if verdict["play"] == "skip":
continue
trigger = new_signals[0]["summary"]
draft = write(trigger, verdict, problem) # 3. Action,
clean = check(trigger, draft) # gated by check
if not clean.get("subject"):
continue
shortlist.append({
"account": account, "score": verdict["score"],
"why_now": verdict["why_now"],
"subject": clean["subject"], "body": clean["body"],
})
save_record(record)
shortlist.sort(key=lambda x: x["score"], reverse=True)
return shortlist # Waits for humanWhen a human approves one and sends it, log the touch. That logged outcome is what the loop learns from.
def log_touch(account, bucket, subject, variant="v1", result=None):
record = load_record()
history_for(record, account)["touches"].append({
"date": datetime.now(timezone.utc).isoformat(),
"bucket": bucket, "subject": subject,
"variant": variant, "result": result, # replied|meeting|no_reply|bounced
})
save_record(record)Part 4, Ship It: Weekly Learning
Per-message memory gives the loop perception. Weekly learning makes it better. It reads outcomes, re-weights which signals are worth chasing, and points out which copy is winning. The new weights feed back into the next run's scoring step.
LEARN = """You tune the engine based on what actually happened. You run weekly, not per message.
Input: outcomes [{bucket, copy_variant, result}], where result is replied|meeting|no_reply|bounced; current_weights.
Re-weight each bucket by win rate (meetings + replies / total touches). Keep a floor so no bucket goes to zero.
If real samples exist, call out the copy_variant with the highest reply rate.
Output only JSON: {"weights":{"funding":0.0,"job":0.0,"company":0.0,"social":0.0,"first_party":0.0},"best_variant":""}"""
def weekly_learn(current_weights):
record = load_record()
outcomes = [
{"bucket": t["bucket"], "copy_variant": t["variant"], "result": t["result"]}
for hist in record.values()
for t in hist.get("touches", []) if t.get("result")
]
tuned = call_model(LEARN, {"outcomes": outcomes, "current_weights": current_weights})
json.dump(tuned, open("weights.json", "w"), indent=2)
return tunedThat feedback loop is the entire difference between an engine that gets better every week and a script that repeats itself forever.
Put It on Cron
A loop is only a loop when it runs without you. Two cron lines, one nightly, one weekly:
0 8 * * * cd outbound-loop && python run.py
0 9 * * 1 cd outbound-loop && python learn.pyWhen you open your laptop, a ranked shortlist is waiting with a reason next to every name. No list building, no subject-line guessing. You read, approve, and get on with your day.
The Four Parts in the Code
Look back and you can point to each part.
Cron and the activity stream are the Trigger . score is the Judgment , the only place the model reasons. The check step plus human approval is the Gate . record.json is the Memory , writing on every account and read on every score; weekly_learn turns that memory into a better engine over time. Remove any one and it stops being a loop. Drop memory and it forgets. Drop the gate and it sends garbage. Drop the trigger and you are back to writing prompts.
Same Skeleton, Other Tasks
Outbound is just one example. The shape transfers.
A support loop triggers on a new ticket, judges whether it is a known issue, drafts a reply, and gates anything uncertain to a human.
A research loop triggers weekly, judges which of a hundred sources are worth reading, and remembers what it has already summarized.
An observability loop watches your own metrics and only alerts when something actually changed. Swap the four functions, keep the skeleton.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
