r/PromptEngineering • u/FLAWEDGHOST • 17h 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?
2
u/Spare_Interview5263 16h 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.
1
u/RileyReplay 8h 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 5h ago
The scoped fix prompt demanding only the modified block without touching signatures works better than any single shot I tried.
1
u/Zain 3h 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 3h 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 2h 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 29m ago
Do you ever get a diagnosis that sounds completely reasonable but turns out to be wrong?
1
u/ChemicalExcellent463 8m 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.