Deep dive 26 of 27: Process Control: Self-Correction Loop
Nov 19, 2026 · Michael Chang · Status in NurseOps Now: ✅ built (reviews)
Show definitions in ( ) · hover a highlighted word for its meaning, click it for details · open with ?defs=1 to show definitions
Part of: Remembering the right things: state management in NurseOps Now, section Preventing Drift and Hallucinations in Long-Running Agents (7.4) · All 27 solutions · ← Input Filtering: Relevance Pruning · Output Guardrails: Schema Enforcement →
Read in this order
- In plain words: what it is, without jargon
- The problem it solves: the problem, and the standard approach in my words
- How to implement it (ServiceApps): scenario, steps, code and real output
- In NurseOps Now: built or not, and where
- Trade-offs and Interview answer
In plain words
A self-correction loop (checking the AI's answer and sending it back once to fix what failed) checks the AI's draft answer before the user sees it, and if a check fails, sends it back once with the exact failures to fix. Most mistakes get fixed on the second try; the rest (a common way for programs to talk over the web using URLs) are caught instead of shipped.
In plain terms: an editor who returns your draft with "this quote isn't in the source" circled, once, before it goes to print.
The problem it solves
Hallucination (The Grounding Failure) (the AI stating made-up facts when the answer isn't in front of it): a first draft can contain a quote or fact that isn't in the source.
The standard approach (in my words): ask the model to verify its answer against the provided context before returning the final result.
How to implement it (ServiceApps)
Illustrative example: no ServiceApps (a company platform for IT tickets, HR requests and help-center articles) instance is connected, so the scenario and data are made up. The code is a self-contained sketch; it was run on that made-up data on Nov 19, 2026, and the output below is from that run.
The setting. ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist, an AI helper inside a company's ServiceApps (a company platform for IT tickets, HR requests and help-center articles) portal. Employees ask it IT and HR questions; it answers from knowledge-base (KB (knowledge base: a library of help articles)) articles, looks up the employee's own tickets (incidents (a ServiceApps ticket for something broken) like INC0012345) and requests, and keeps one conversation per employee and ticket.
The scenario. ServiceApps (a company platform for IT tickets, HR requests and help-center articles) Assist's first draft misstates what VPN error 809 means; the check catches it and the retry fixes it.
Steps.
- Generate a draft that includes its quotes.
- Verify in code: every quote must appear in the source.
- If it fails, retry once with the list of failures added to the prompt (the instructions and text given to the AI).
- If it still fails, return MISSING_INFO and hand over to a person.
The code (Python):
import re
SOURCE = "Error 809 means the VPN can't reach the server. Avoid guest Wi-Fi and restart the client."
attempts = iter(['Error 809 means your password expired. [quote: "your password expired"]',
'Error 809 means the VPN can\'t reach the server. [quote: "the VPN can\'t reach the server"]'])
def model(prompt): return next(attempts) # stand-in for the AI call
def verify(draft):
q = re.search(r'\[quote: "([^"]+)"\]', draft)
return [] if q and q.group(1) in SOURCE else [f'quote not in source: "{q.group(1) if q else "none"}"']
def answer(question, max_retries=1):
prompt = question
for attempt in range(max_retries + 1):
draft = model(prompt); fails = verify(draft)
print(f"attempt {attempt + 1}: {'pass' if not fails else 'fail -> ' + fails[0]}")
if not fails:
return draft
prompt = question + "\nYour previous answer failed these checks: " + "; ".join(fails) + ". Quote the source exactly."
return "MISSING_INFO: couldn't verify an answer; a person will follow up."
print("final:", answer("What does VPN error 809 mean?"))
Output (from running the code above):
attempt 1: fail -> quote not in source: "your password expired" attempt 2: pass final: Error 809 means the VPN can't reach the server. [quote: "the VPN can't reach the server"]
What to watch for.
- Verify in code where you can; asking the same model "are you sure?" is weaker.
- Limit retries (one is usually enough) and cap cost per request.
- Send the specific failures back, not just "try again".
In plain terms: draft, check, fix once, and only then answer; if it still can't be verified, a person takes over.
In NurseOps Now
Built for case reviews: review_graph.py runs ai_review → verify; a failed quote check sends Claude (Anthropic's AI model that this app uses) back once with the failures, unless the case's cost cap is reached. Tests: test_failed_quote_retries_claude_once_with_feedback, test_no_retry_when_quotes_verify. See Step 11.
Trade-offs
- Each retry costs another AI call and adds latency (how long it takes to respond).
- Code checks only catch what they test (quotes, formats), not every wrong fact.
Interview answer
"Drafts go through a verify step in code, for example every quote must be in the source. On failure the model gets one retry with the exact failures listed, and if it still fails we return MISSING_INFO and hand off to a person. It fixes most errors and never ships an unverified one."
All data in this post is made up. No real people, tickets or patient data.
← Back to all 27 solutions in the state management blog
Key terms
Every highlighted word in this post links here. Click ↩ Back to return to the spot you came from.
- Claude
- Anthropic's AI model that this app uses.Used in: In NurseOps Now↩ Back to where you were
- hallucination (the grounding failure)
- The AI stating made-up facts when the answer isn't in front of it.Used in: The problem it solves↩ Back to where you were
- incident
- A ServiceApps ticket for something broken.Used in: How to implement it (ServiceApps)↩ Back to where you were
- KB
- Knowledge base: a library of help articles.Used in: How to implement it (ServiceApps)↩ Back to where you were
- latency
- How long it takes to respond.Used in: Trade-offs↩ Back to where you were
- prompt
- The instructions and text given to the AI.Used in: How to implement it (ServiceApps)↩ Back to where you were
- REST
- A common way for programs to talk over the web using URLs.Used in: In plain words↩ Back to where you were
- self-correction loop
- Checking the AI's answer and sending it back once to fix what failed.Used in: In plain words↩ Back to where you were
- ServiceApps
- A company platform for IT tickets, HR requests and help-center articles.Used in: How to implement it (ServiceApps)↩ Back to where you were