DEV Community

Youngchan Kim
Youngchan Kim

Posted on AI-assisted

Making an LLM write a client email without letting it invent a single fact

AI-assisted: I wrote this post with an assistant's help. The workflow, the code, and the failures described come from a pack I built and run.

A first version of this workflow took twenty minutes: a Google Sheets node, a prompt, and a Gmail node. It was also unusable, and the reason was not the model.

The test invoice was 19 days overdue for $4,200. The model wrote a warm, correct, well-structured reminder — and added "a 1.5% monthly late fee applies from the due date, per our agreement", then closed with bank details that belonged to no bank. My contract has no late fee. I have never put banking details in a reminder email.

A prompt cannot fix that. Once the model is asked to "write a professional payment reminder", the gap between what it knows and what is true gets filled by something that reads like a professional payment reminder. The fix was to stop treating the model as the author and start treating it as a formatter with a narrow, checkable job. Four rules made that work.

1. The sheet is the only source of facts

The model gets nothing beyond the row it is chasing. No customer history, no free-text "notes from the owner". The user message is a fixed template that prints the fields as data:

Client: {{ $json.client_name }}
Invoice: {{ $json.invoice_no }}
Amount due: {{ $json.amount_display }}
Issued: {{ $json.issue_date }} · Due: {{ $json.due_date }}
Days overdue: {{ $json.days_overdue }}
Previous reminders sent: {{ $json.previous_chases }}
Terms / payment details: {{ $json.payment_terms }}
Enter fullscreen mode Exit fullscreen mode

And the system prompt names what may not appear:

- Use ONLY the supplied invoice facts. Never invent a late fee, a discount,
  interest, a bank detail or a payment link - write `[PAYMENT LINK]` if one is needed.
- 70-140 words. Plain text, no markdown, no emoji, no exclamation marks.
- Subject must include the invoice number and stay under 9 words.
Enter fullscreen mode Exit fullscreen mode

The placeholder matters more than the prohibition. A model told only "never invent a payment link" tends to either drop the line or invent one anyway; a model told "write [PAYMENT LINK] if one is needed" produces a literal string a human can find with Ctrl+F before sending. Every rule that forbids something needs somewhere for the model to put the need.

2. Pair the output back by index, not by hope

This one cost me an afternoon, and it costs every n8n user who chains a Sheets node into an LLM node.

The OpenAI node returns one item per input item, in order — until one input fails validation, or a node is set to continue on error, or the model answers a batch with a single item. Then output index 3 belongs to input index 4, and invoice A's reminder carries invoice B's amount. Nobody notices, because both emails look fine.

So the model's output is joined back to the exact row that produced it, by index, against a snapshot taken before the call:

// One chase per invoice: pair AI output with its invoice by index.
const bases = $('Cap At 10').all().map(i => i.json);   // what we sent
const outs  = $input.all().map(i => i.json);           // what came back

return outs.map((res, i) => {
  const base = bases[i] || {};
  // ... parse and guard ...
});
Enter fullscreen mode Exit fullscreen mode

$('Cap At 10') is the node that limited the batch to ten invoices, so the pairing is anchored to a snapshot instead of to whatever happens to be flowing now. If the two lists ever disagree in length, base is {} and the guard in the next section catches it — rather than an email going out with an empty amount.

3. Make failure loud, not silent

A model that returns prose instead of JSON, or two words of body, must not become an email. Every failure lands in one visible state:

const ok = body.length > 60;
if (!ok) {
  body = 'The AI step did not return a usable reminder for this invoice.\n\n'
       + 'Invoice ' + base.invoice_no + ' is ' + base.days_overdue
       + ' days overdue - write this one by hand.';
  subject = '[NEEDS A HUMAN] Overdue invoice ' + base.invoice_no;
}
return { json: { ...base, subject, body,
  chase_status: ok ? 'drafted' : 'needs_review' } };
Enter fullscreen mode Exit fullscreen mode

[NEEDS A HUMAN] goes in the subject line, so it surfaces in the Gmail draft list under a search rule (subject:"[NEEDS A HUMAN]") and nothing else. The arrival-guide workflow in another pack uses the same idea as [[ACCESS_CODE - add manually]]: a human decision becomes a marked hole, not a missing sentence that reads like a mistake.

4. Draft only. Never send.

Every message this workflow produces is a Gmail draft. There is no send node in the JSON. The stage table also refuses to chase the same invoice twice in two days:

const done = Number(r.chase_stage || 0);
if (done >= target.stage) continue;           // already at this tone
const last = dstr(r.last_chase_at);
if (last && days(today, last) < 2) continue;  // too soon
Enter fullscreen mode Exit fullscreen mode

Stages are D+3 reminder, D+7 direct, D+14 firm, D+21 final notice. The final notice offers a payment plan because that is the one concession the workflow is allowed to make — it is written into the stage rules, not left to the model's judgement about what a reasonable seller would accept.

What this does not do

  • It never generates a late fee, interest, discount, or bank detail. If your invoice needs one, it has to be a column in the sheet.
  • It does not decide who owes what. Days overdue come from a date subtraction in code, not from the model.
  • It does not guarantee the email is good. It guarantees the numbers came from the sheet and that a human read it before it left.
  • The free-text notes column is passed to the model, which means anything careless written there can leak into a draft. Keep it factual.

Why a policy layer instead of a better prompt

A prompt is a request. A policy layer is a set of places the system cannot go: no send node, no amount that did not come from a row, no silent skip, no unmarked gap. The model still writes better prose than any template I could write by hand — it just does it inside a shape where the worst outcome is a marked draft a human fixes in ten seconds.


The invoice chaser above is one of three workflows in a pack I sell for $39 (client onboarding, the Friday status update, and the D+3/D+7/D+14/D+21 chase). I'm giving the first five copies away until Dec 14 with the code FREEAGENCY5, and I'm not attaching anything to what you tell me back: https://opsforgeai.gumroad.com/l/agency-client-ops-pack/FREEAGENCY5

Top comments (0)