cd /news/ai-agents/how-to-implement-human-in-the-loop-f… · home topics ai-agents article
[ARTICLE · art-71268] src=promptcube3.com ↗ pub= topic=ai-agents verified=true sentiment=· neutral

How to Implement Human-in-the-Loop for AI Emails

A new human-in-the-loop workflow for AI-generated emails, demonstrated with Node.js and TypeScript using the Impri review API and nodemailer, replaces risky 'fire and forget' AI email sending with a controlled pipeline where an AI agent drafts, a human audits and edits, and only the approved version is sent. The implementation polls Impri for a human decision before executing the send command, ensuring production-ready fail-safe LLM agent deployment.

read2 min views1 publishedJul 23, 2026
How to Implement Human-in-the-Loop for AI Emails
Image: Promptcube3 (auto-discovered)

approved

status.This approach transforms the AI workflow from a risky "fire and forget" system into a controlled pipeline: the agent drafts, a human audits/edits, and only then does the code execute the send command.

The Logic Flow #

Instead of a direct call to an SMTP server, the agent follows this sequence:

  1. Draft Generation: The agent produces the content.

  2. External Holding: The draft is pushed to a review system (like Impri) via API.

  3. Human Intervention: A reviewer reads the draft, makes necessary edits to the body or subject, and hits "Approve."

  4. Execution: The system polls for the decision; if approved, it sends the final edited version, not the original AI draft.

Real-world Implementation: Node.js & TypeScript #

Here is a practical tutorial on how to build this approval gate using nodemailer

and a review API.

import nodemailer from "nodemailer";

const IMPRI_KEY = process.env.IMPRI_API_KEY!;
const IMPRI_BASE = "https://api.impri.dev";

async function sendWithApproval(opts: {
 to: string;
 subject: string;
 body: string;
 expiresIn?: number; // seconds; default 3600
}): Promise<"approved" | "rejected" | "expired"> {
 // 1. Push the draft to review
 const push = await fetch(`${IMPRI_BASE}/v1/actions`, {
 method: "POST",
 headers: {
 Authorization: `Bearer ${IMPRI_KEY}`,
 "Content-Type": "application/json",
 },
 body: JSON.stringify({
 kind: "email.send",
 title: `Email to ${opts.to}: ${opts.subject}`,
 preview: {
 format: "markdown",
 body: `**To:** ${opts.to}\n**Subject:** ${opts.subject}\n\n---\n\n${opts.body}`,
 },
 editable: ["preview.body"], 
 expires_in: opts.expiresIn ?? 3600,
 }),
 });

 if (!push.ok) throw new Error(`Push failed: ${push.status}`);
 const { id: actionId } = await push.json();

 // 2. Poll until human decision
 let result: { status: string; decision?: { final_preview?: { body: string } } };
 for (;;) {
 const poll = await fetch(`${IMPRI_BASE}/v1/actions/${actionId}`, {
 headers: { Authorization: `Bearer ${IMPRI_KEY}` },
 });
 result = await poll.json();
 if (result.status !== "pending") break;
 await new Promise((r) => setTimeout(r, 10_000));
 }

 if (result.status !== "approved") {
 return result.status as "rejected" | "expired";
 }

 // 3. Send the human-approved version
 const approvedBody = result.decision!.final_preview!.body;

 const transporter = nodemailer.createTransport({
 host: process.env.SMTP_HOST,
 port: 587,
 auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS },
 });

 await transporter.sendMail({
 from: process.env.SMTP_FROM,
 to: opts.to,
 subject: opts.subject,
 text: approvedBody,
 });

 // 4. Update audit log
 await fetch(`${IMPRI_BASE}/v1/actions/${actionId}/result`, {
 method: "POST",
 headers: {
 Authorization: `Bearer ${IMPRI_KEY}`,
 "Content-Type": "application/json",
 },
 body: JSON.stringify({ status: "success" }),
 });

 return "approved";
}

By treating human approval as a mandatory API response rather than a prompt suggestion, you create a fail-safe LLM agent deployment that is actually production-ready.

Next Mage-Flow: 4B Params vs 32B Giants →

── more in #ai-agents 4 stories · sorted by recency
── more on @impri 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-to-implement-hum…] indexed:0 read:2min 2026-07-23 ·