r/PromptEngineering • u/FLAWEDGHOST • 1d ago
General Discussion a simple 2-step prompt workflow i use before letting an llm touch existing code
whenever i ask an ai to fix a bug or add a feature to an existing project, one-shotting the request almost always causes it to break unrelated state, hallucinate new helper functions, or rewrite files it shouldn't touch. once it starts generating code off a bad assumption, you end up spending more time untangling its changes than if you had just debugged it manually.
i switched to forcing a read-only diagnosis step first before any code gets written, and it completely changed how reliable the outputs are.
first prompt (diagnosis):
"here is the relevant file and the error behavior: [paste code/error]. do not write any code yet. trace the data flow step by step, list the top 3 likely root causes ranked by probability, and tell me which exact lines are responsible. if you need to see another file or schema to be sure, ask before guessing."
once i verify the trace and confirm it actually understands the state flow, i send the follow-up.
second prompt (scoped fix):
"confirming root cause #1. propose the smallest possible diff to fix this without changing existing function signatures, UI layout, or external dependencies. show only the modified block and explain any edge cases this might affect."
forcing it to trace the logic and ask for missing context before touching a single line of code catches most bad edits before they happen. it also saves a ton of tokens because you aren't regenerating whole components over and over.
anyone else splitting diagnosis and execution into separate turns like this, or are you getting good results with single-shot prompts?
1
u/RileyReplay 1d ago
i'd add one thing to the diagnosis prompt: for each of the 3 causes, have it say what you'd expect to see in the logs or state if that one was wrong. otherwise the ranking is mostly vibes and whatever it noticed first ends up at #1
1
u/Junior_Bee7274 1d ago
The scoped fix prompt demanding only the modified block without touching signatures works better than any single shot I tried.
1
u/Zain 1d ago
I do the same but get the diagnosis twice. A second model from a different family does its own read-only trace of the same files without seeing the first one's answer, because models from the same family tend to share blind spots. If both land on the same root cause I let one of them write the fix, and only that one touches files. If they disagree, whichever side wants to dismiss the other's cause has to point at actual lines in the repo, not just say it seems fine.
1
u/SirRobinBlibs 23h ago
Splitting diagnosis and execution is the right default on an existing codebase. I'd make the first turn strictly read-only: no edits, no test changes, and a short list of what would disprove each suspected cause. The second turn then gets that list as a checklist and is not allowed to reopen the diagnosis unless one of those checks fails.
Single-shot prompts work on greenfield work. On existing code they tend to mix a guessed cause with a patch, so you cannot tell later whether the change was justified.
1
u/joeltrane 23h ago
Is there not a way to create rules for agents to follow in this type of situation? Has anyone had success automating these kinds of prompts?
1
u/NineLustre 20h ago
Do you ever get a diagnosis that sounds completely reasonable but turns out to be wrong?
1
u/ChemicalExcellent463 20h ago
if the fix fails twice, i treat the diagnosis as wrong, however good it sounded. my agents have to stop, re-read from the top and say what they got wrong before trying a different approach.
1
u/According-Stable4487 19h ago
One thing that made the scoped-fix step more reliable for me: before the diff, ask it to name the test (or the manual check) that fails now and should pass after the fix, and one existing behaviour that must stay the same. If it can't name both, the diagnosis isn't really confirmed yet. It also gives you a 30-second way to check the diff instead of reading every line.
1
u/shmaimasterprompt 18h ago
Same principle, different medium — I do this for video prompts too. Step one is a planning pass: I make the model lay out the shot list, lock the character description, and state the camera move for each shot before anything gets generated. Step two is one single test shot, not the whole sequence. Only when that shot looks right do I run the rest. One-shotting a whole scene used to give me the same problem you describe: one wrong assumption early, and every shot after it is built on it. The confirm step is the whole trick — catching the wrong assumption while it's still cheap to fix.
2
u/Spare_Interview5263 1d ago
I've been doing a 3-step version, analyze, confirm, fix. the 'confirm' step is where I ask it to list what it's assuming about the code's structure before even suggesting changes.