Advanced Problem

Small AI assistants often look good in demos but become risky in production when memory, prompt construction, and fallback behavior are loosely coupled.

Step 1: Separate instruction layers

type PromptLayers = {
  systemRules: string;
  taskContext: string;
  userInput: string;
};

Step 2: Add response quality gates

function passesQuality(output: string): boolean {
  if (output.length < 180) return false;
  if (/\b(I am just an AI|cannot provide)\b/i.test(output)) return false;
  return true;
}

Step 3: Use fallback paths with explicit reason codes

async function generate(primary: () => Promise, fallback: () => Promise) {
  try {
    const out = await primary();
    if (!passesQuality(out)) throw new Error('quality_gate_failed');
    return { output: out, source: 'primary' };
  } catch (e) {
    const out = await fallback();
    return { output: out, source: 'fallback', reason: String(e) };
  }
}
Preview: first 50% is visible. Unlock to read the full article.
To view this content, you must be a member of CodeWithWilliamJiamin's Patreon at $1 or more
Already a qualifying Patreon member? Refresh to access this content.