r/OnlyAICoding • u/PSBigBig_OneStarDao • Sep 10 '25
stop firefighting. add a tiny “reasoning firewall” before your ai call
most “ai coding” fixes happen after the model speaks. you get a wrong answer, then you add a reranker or a regex. the same failure shows up elsewhere. the better pattern is to preflight the request, block unstable states, and only generate once it’s stable.
i keep a public “problem map” of 16 reproducible failure modes with one-page fixes. today i’m sharing a drop-in preflight you can paste into any stack in about a minute. it catches the common ones before they bite you.
what this does in plain words:
restate-the-goal check. if the model’s restatement drifts from your goal, do not generate.
coverage check. enforce citations or required fields before you accept an answer.
one retry with a contract. if the answer misses the contract, fix it once, not with random patches.
below is a tiny python version. keep your provider as is. swap ask_llm with your client.
# tiny reasoning firewall for ai calls
ACCEPT = {"deltaS": 0.45} # lower is better
def bag(text):
import re
words = re.sub(r"[^\w\s]", " ", text.lower()).split()
m = {}
for w in words:
m[w] = m.get(w, 0) + 1
return m
def cosine(a, b):
import math
keys = set(a) | set(b)
dot = sum(a.get(k,0)*b.get(k,0) for k in keys)
na = math.sqrt(sum(v*v for v in a.values()))
nb = math.sqrt(sum(v*v for v in b.values()))
return dot / (na*nb or 1.0)
def deltaS(goal, restated):
return 1 - cosine(bag(goal), bag(restated))
async def ask_llm(messages):
# plug your client here. return text string.
# for OpenAI-compatible clients, map messages → completion and return content.
raise NotImplementedError
async def answer_with_firewall(question, goal, need_citations=True, required_keys=None):
required_keys = required_keys or []
# 1) preflight: get restated goal + missing inputs
pre_prompt = [
{"role": "system", "content": "reply only valid JSON. no prose."},
{"role": "user", "content": f"""goal: {goal}
restate as "g" in <= 15 words.
list any missing inputs as "missing" array.
{{"g":"...", "missing":[]}}"""}
]
pre = await ask_llm(pre_prompt)
import json
pre_obj = json.loads(pre)
dS = deltaS(goal, pre_obj.get("g",""))
if dS > ACCEPT["deltaS"] or pre_obj.get("missing"):
return {
"status": "unstable",
"deltaS": round(dS, 3),
"ask": pre_obj.get("missing", []),
"note": "do not generate. collect missing or tighten goal."
}
# 2) generate under a contract
sys = "when you assert a fact backed by any source, append [cite]. keep it concise."
out = await ask_llm([
{"role": "system", "content": sys},
{"role": "user", "content": question}
])
# 3) coverage checks
ok = True
reasons = []
if need_citations and "[cite]" not in out:
ok = False
reasons.append("no [cite] markers")
for k in required_keys:
if f'"{k}"' not in out and f"{k}:" not in out:
ok = False
reasons.append(f"missing field {k}")
if not ok:
fix = await ask_llm([
{"role": "system", "content": "rewrite to satisfy: include [cite] for claims and include required keys."},
{"role": "user", "content": f"required_keys={required_keys}\n\nprevious:\n{out}"}
])
return {"status": "ok", "text": fix, "deltaS": round(dS,3), "retry": True}
return {"status": "ok", "text": out, "deltaS": round(dS,3), "retry": False}
# example idea
# goal = "short answer with [cite]. include code fence if code appears."
# res = await answer_with_firewall("why cosine can fail on short strings?", goal, need_citations=True)
# print(res)why this helps here:
you stop generating into known traps. if the preflight deviates from your goal, you block early.
it is vendor neutral. fits OpenAI, Anthropic, local runtimes, anything.
it maps to recurring bugs many of us keep hitting: No.2 interpretation collapse. chunk right, logic wrong. No.5 semantic vs embedding. cosine looks high, meaning is off. No.16 pre-deploy collapse. first call fails because a dependency was not ready.
acceptance targets i use in practice:
deltaS ≤ 0.45 before generation.
coverage present. either citations or required keys, not optional.
if drift recurs later, treat it as a new failure mode. do not pile more patches.
single link with the full 16-mode map and the one-page fixes:
https://github.com/onestardao/WFGY/tree/main/ProblemMap/README.md
if you post a minimal repro in the comments, i will map it to a number and give the minimal fix order. which bites you more lately, retrieval drift or embedding mismatch?