{"slug":"prompt-quality","category":"composer","job":"steer","added":"2026-11-08T13:31Z","title":"Prompt Quality Meter","description":"A meter under the prompt that fills as it gets more specific, with a chip per missing detail that inserts a fill-in phrase, and one tip for what to add next.","url":"https://ai-patterns.app/patterns/composer/prompt-quality","markdown":"https://ai-patterns.app/patterns/composer/prompt-quality.md","json":"https://ai-patterns.app/api/patterns/composer/prompt-quality","seenIn":[],"spec":"# Prompt Quality Meter\n\n## Summary\nA small meter under the prompt that fills up as the prompt gets more specific, with a chip for each detail that helps (task, audience, format, length or tone) and one tip for the next detail missing. It answers \"is this enough to get a good answer?\" before the person sends, and teaches what a strong prompt has in it. It never blocks sending.\n\n## When to use\n- Products where vague prompts lead to generic answers, and people often have to send a second message to clarify.\n- Tasks with a known recipe for a good request: drafting copy, briefs, image prompts, data questions.\n- New users who don't yet know what an AI tool needs from them.\n\n## When not to use\n- Conversational follow-ups (\"make it shorter\", \"why?\"), where a short message is the right message.\n- Expert tools whose users write precise prompts already; the meter becomes clutter they learn to ignore.\n- As a gate. If a prompt must have certain details before it can run, ask for them with Prompt Wizard or Prompt Template instead of scoring the person.\n- To rewrite the prompt for them — use Prompt Enhancer.\n\n## Anatomy\n- Prompt field and Send button.\n- Meter: a few segments and a level label (\"Vague\", \"Basic\", \"Good\", \"Specific\"), inside the field next to Send.\n- Detail chips under the field: a filled chip with a check for each detail that's covered, and a dashed chip with a plus for each one that's missing.\n- Tip line: one sentence about the first missing detail.\n\n## Behavior\n- Nothing shows until the prompt has a few characters, so an empty field isn't scored.\n- The score updates after a short pause in typing (around 350 ms), not on every keystroke, so chips and segments don't flicker.\n- Segments fill in proportion to the details covered. The top level is reserved for prompts that cover all of them.\n- A missing chip is also an action: clicking it adds a fill-in phrase (\" for [audience]\") and selects the bracketed part, so typing replaces it. A missing task goes at the start of the prompt, other details at the end.\n- The tip names only the next missing detail. Once everything is covered it says the prompt is specific enough and stops coaching.\n- Send works at every level. The meter informs and never disables or delays sending.\n- Checks can be simple keyword heuristics or a fast model call. Either way, they must be quick and private enough to run while the person types.\n\n## Content guidelines\n- Use four or five details at most, named as plain nouns: \"Audience\", \"Format\", \"Length or tone\".\n- Write tips as advice with an example, not as criticism: \"Say who it's for\", not \"Your prompt lacks an audience\".\n- Avoid scores and percentages. Words and segments are enough, and a number invites gaming.\n- Keep fill-in phrases short and in the same voice as the prompt. Use bracketed placeholders that read naturally: \"[audience]\", \"[100] words\".\n\n## Accessibility\n- The meter has `role=\"meter\"` with an accessible name, the score as its value and a text version (\"Basic: 2 of 4 details\"). The textarea points to it with `aria-describedby`.\n- Covered chips say \"included\" and missing ones say \"missing — add it\" to screen readers, so the state isn't conveyed by the check and dashed border alone.\n- Missing chips are real buttons; after one is clicked, focus moves back to the textarea with the placeholder selected.\n- The tip is a polite live region, and it only changes when the next missing detail changes.\n- Segment fills are instant when the user prefers reduced motion.\n\n## Related patterns\n- **Prompt Enhancer** — Use it instead to rewrite a short prompt into a specific one in a single step.\n- **Prompt Wizard** — Use it instead when the details are required, and ask for them one at a time.\n- **Prompt Autocomplete** — Pair it to suggest the words, while the meter suggests what's missing.\n- **Clarifying Questions** — Use it instead when the AI should ask for missing details after the prompt is sent.\n","code":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { ArrowUp, Check, Plus } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface QualityCriterion {\n  id: string;\n  /** Short name shown on the chip: \"Audience\", \"Format\". */\n  label: string;\n  /** The tip shown while this is the next thing missing: \"Say who it's for\". */\n  tip: string;\n  /** Whether the prompt already covers this. Heuristics or a model's verdict. */\n  test: (prompt: string) => boolean;\n  /**\n   * Text appended when the chip is clicked. Wrap the part to fill in with\n   * square brackets — \" for [audience]\" — and it's selected after insertion.\n   */\n  insert?: string;\n  /** Where `insert` goes. Defaults to the end; a missing task reads better first. */\n  at?: \"start\" | \"end\";\n}\n\nexport interface PromptQualityProps {\n  value: string;\n  onChange: (value: string) => void;\n  criteria: QualityCriterion[];\n  onSubmit?: (prompt: string) => void;\n  placeholder?: string;\n  /** The meter appears once the prompt has this many characters. */\n  minLength?: number;\n  /** Labels from lowest to highest. The meter has one segment per label. */\n  levels?: string[];\n  className?: string;\n}\n\nconst DEFAULT_LEVELS = [\"Vague\", \"Basic\", \"Good\", \"Specific\"];\n\nexport function PromptQuality({\n  value,\n  onChange,\n  criteria,\n  onSubmit,\n  placeholder = \"Describe what you need…\",\n  minLength = 4,\n  levels = DEFAULT_LEVELS,\n  className,\n}: PromptQualityProps) {\n  const textareaRef = React.useRef<HTMLTextAreaElement>(null);\n  const reduceMotion = useReducedMotion();\n  const meterId = React.useId();\n\n  // Let the meter settle while typing fast, so chips don't flicker per keystroke.\n  const settled = useDebounced(value, 350);\n\n  const met = criteria.map((c) => c.test(settled));\n  const score = met.filter(Boolean).length;\n  // Segments fill in proportion to what's covered; the top one only when everything is.\n  const filled =\n    score === 0 ? 0 : score === criteria.length ? levels.length : Math.min(levels.length - 1, Math.ceil((score / criteria.length) * levels.length));\n  const level = Math.max(0, filled - 1);\n  const next = criteria.find((_, i) => !met[i]);\n  const visible = settled.trim().length >= minLength;\n\n  function add(criterion: QualityCriterion) {\n    if (!criterion.insert) {\n      textareaRef.current?.focus();\n      return;\n    }\n    const insert = criterion.insert;\n    let nextValue: string;\n    let from: number;\n    if (criterion.at === \"start\") {\n      const rest = value.replace(/^\\s+/, \"\");\n      nextValue = insert + (rest ? \" \" + rest.charAt(0).toLowerCase() + rest.slice(1) : \"\");\n      from = 0;\n    } else {\n      const base = value.replace(/\\s+$/, \"\");\n      nextValue = base + (/^[\\s,.;:]/.test(insert) || !base ? \"\" : \" \") + insert;\n      from = base.length;\n    }\n    onChange(nextValue);\n    // Select the bracketed part so typing replaces it.\n    const open = nextValue.indexOf(\"[\", from);\n    const close = open >= 0 ? nextValue.indexOf(\"]\", open) : -1;\n    requestAnimationFrame(() => {\n      const el = textareaRef.current;\n      if (!el) return;\n      el.focus();\n      if (open >= 0 && close > open) el.setSelectionRange(open, close + 1);\n      else el.setSelectionRange(nextValue.length, nextValue.length);\n    });\n  }\n\n  return (\n    <div className={cn(\"flex w-full flex-col gap-2.5\", className)}>\n      <div className=\"relative rounded-2xl bg-card focus-within:border-foreground/30 border\">\n        <textarea\n          ref={textareaRef}\n          value={value}\n          onChange={(e) => onChange(e.target.value)}\n          onKeyDown={(e) => {\n            if (e.key === \"Enter\" && !e.shiftKey && onSubmit && value.trim()) {\n              e.preventDefault();\n              onSubmit(value);\n            }\n          }}\n          placeholder={placeholder}\n          aria-label=\"Prompt\"\n          aria-describedby={visible ? meterId : undefined}\n          rows={3}\n          className=\"block min-h-24 w-full resize-none bg-transparent px-4 pt-3 pb-12 text-sm leading-relaxed outline-none placeholder:text-muted-foreground\"\n        />\n\n        <div className=\"absolute inset-x-4 bottom-3 flex items-center justify-between gap-3\">\n          <div\n            id={meterId}\n            role=\"meter\"\n            aria-label=\"Prompt detail\"\n            aria-valuemin={0}\n            aria-valuemax={criteria.length}\n            aria-valuenow={visible ? score : 0}\n            aria-valuetext={visible ? `${levels[level]}: ${score} of ${criteria.length} details` : \"Not rated yet\"}\n            className={cn(\"flex items-center gap-2 transition-opacity\", visible ? \"opacity-100\" : \"opacity-0\")}\n          >\n            <div className=\"flex gap-1\" aria-hidden>\n              {levels.map((_, i) => (\n                <span key={i} className=\"relative h-1.5 w-6 overflow-hidden rounded-full bg-muted\">\n                  <motion.span\n                    className=\"absolute inset-0 origin-left rounded-full bg-foreground\"\n                    initial={false}\n                    animate={{ scaleX: visible && i < filled ? 1 : 0 }}\n                    transition={reduceMotion ? { duration: 0 } : { duration: 0.3, delay: i * 0.05, ease: \"easeOut\" }}\n                  />\n                </span>\n              ))}\n            </div>\n            <span className=\"text-xs font-medium tabular-nums\" aria-hidden>\n              {levels[level]}\n            </span>\n          </div>\n\n          {onSubmit && (\n            <button\n              type=\"button\"\n              aria-label=\"Send\"\n              disabled={!value.trim()}\n              onClick={() => onSubmit(value)}\n              className=\"flex size-8 shrink-0 items-center justify-center rounded-full bg-foreground text-background transition-opacity disabled:opacity-30\"\n            >\n              <ArrowUp className=\"size-4\" aria-hidden />\n            </button>\n          )}\n        </div>\n      </div>\n\n      <AnimatePresence initial={false}>\n        {visible && (\n          <motion.div\n            initial={{ opacity: 0, y: -4 }}\n            animate={{ opacity: 1, y: 0 }}\n            exit={{ opacity: 0, y: -4 }}\n            transition={{ duration: 0.18 }}\n            className=\"flex flex-col gap-2 px-1\"\n          >\n            <ul className=\"flex flex-wrap gap-1.5\" aria-label=\"Prompt details\">\n              {criteria.map((criterion, i) => (\n                <li key={criterion.id}>\n                  {met[i] ? (\n                    <span className=\"inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1 text-xs text-foreground\">\n                      <Check className=\"size-3\" aria-hidden />\n                      {criterion.label}\n                      <span className=\"sr-only\">, included</span>\n                    </span>\n                  ) : (\n                    <button\n                      type=\"button\"\n                      onClick={() => add(criterion)}\n                      title={criterion.tip}\n                      className=\"inline-flex items-center gap-1 rounded-full border border-dashed px-2.5 py-1 text-xs text-muted-foreground transition-colors hover:border-solid hover:bg-accent hover:text-foreground\"\n                    >\n                      <Plus className=\"size-3\" aria-hidden />\n                      {criterion.label}\n                      <span className=\"sr-only\">, missing — add it</span>\n                    </button>\n                  )}\n                </li>\n              ))}\n            </ul>\n\n            <p className=\"min-h-4 text-xs text-muted-foreground\" aria-live=\"polite\">\n              <AnimatePresence mode=\"wait\" initial={false}>\n                <motion.span\n                  key={next?.id ?? \"done\"}\n                  initial={{ opacity: 0 }}\n                  animate={{ opacity: 1 }}\n                  exit={{ opacity: 0 }}\n                  transition={{ duration: 0.15 }}\n                >\n                  {next ? `Tip: ${next.tip}.` : \"Specific enough to get a focused answer.\"}\n                </motion.span>\n              </AnimatePresence>\n            </p>\n          </motion.div>\n        )}\n      </AnimatePresence>\n    </div>\n  );\n}\n\nfunction useDebounced<T>(value: T, ms: number) {\n  const [settled, setSettled] = React.useState(value);\n  React.useEffect(() => {\n    const timer = setTimeout(() => setSettled(value), ms);\n    return () => clearTimeout(timer);\n  }, [value, ms]);\n  return settled;\n}\n","dependencies":["lucide-react","motion"],"localImports":["@/lib/utils"]}