Generate Multiple Solutions with Template-Driven Hooks

John Lindquist
InstructorJohn Lindquist

Social Share Links

Tweet

Template-driven hooks turn one instruction into multiple structured solutions. Load templates from disk, replace placeholders, and get consistent output formats every time.

The manual variation problem

Getting multiple solutions requires:

  • Repeatedly prompting with different angles
  • Inconsistent output formats
  • Lost context between attempts

Template + hook solution

Store a reusable template with placeholders:

.claude/prompts/variations.md:

Generate $count possible solutions for the following instruction:

Format each solution with this template:

Steps:
1. ...
2. ...
3. ...

Reasoning:
...

Tradeoffs:
...

Instruction: $instruction

Hook to load and fill template:

.claude/hooks/UserPromptSubmit.ts:

import { type UserPromptSubmitHookInput } from "@anthropic-ai/claude-code"

const input = await Bun.stdin.json() as UserPromptSubmitHookInput

// Match v(N) pattern
const match = input.prompt.match(/v\((\d+)\)/)
if (match) {
  const count = match[1]
  const template = await Bun.file(".claude/prompts/variations.md").text()

  // Strip command, keep instruction
  const instruction = input.prompt.replace(match[0], "").trim()

  // Fill template
  const prompt = template
    .replace("$count", count)
    .replace("$instruction", instruction)

  console.log(prompt)
}

Use it

Type your request with the command:

Build a desktop dictation app v(5)

Claude receives the full template with your values filled in, returning 5 structured solutions.

Debug with transcript

View the transformed prompt:

  • Press Ctrl+O to toggle detailed view
  • See exactly what Claude received
  • Refine templates based on results

Try it

Prompts:

desktop app for capturing dictation v(3)
CLI tool for file organization v(5)

[00:00] So a concept I use a lot when prompting is essentially... I'm going to create a Cloud Prompts directory, call it variations.md. Generate three possible solutions for the following instruction, and I'll replace 3 with count. Then we'll drop in a format the instructions with the following template. And let's drop in a template like so.

[00:24] I'm just going to clean this up a bit. Steps one, two, three, reasoning, and trade-offs. Then just kind of leave it at that for now. We could modify this forever. And if we go back to our prompt, instead of matching for load, I'm just going to match for V for variations, swap username for count, and then we're going to bun file and load in this file.

[00:52] We'll call .txt on it. Make sure to await this. So prompt await bun file. And then just clear out the input prompt by input prompt replace, then we'll replace the match with nothing, and then we can just console.log, and then our new prompt will be the prompt that we loaded in, where we replace the instruction with the input prompt, we replace the count with the count. We'll just cast this as string right now to get rid of the error and then log out our new prompt.

[01:23] So if we clear this out and I say a desktop app for capturing dictation I can drop in a v5 at the end or really anywhere in the prompt. You'll see that the result is five different solutions, all with steps in them, all offering reasoning and trade-offs, so that you can pick how you want to continue from your original prompt. Now at any point if you want to see how the hook rewrote it hit ctrl-o and looks like that messed up formatting, but I'll scroll up and hopefully it'll clear up. You'll see we got the original prompt with the v5 and then our hook which took over and said this with all of this text inside of the user prompt submit hook tag. So anytime you need to inspect that just can toggle ctrl-o on and off to go back and inspect how the hooks were impacting the conversation.

[02:17] And again all of this came from this file where we essentially gave it a couple variables to replace. So count was replaced with the number 5 and then the instruction was replaced with the original prompt.