Once it writes the code, take that into a new session to fix a bug. Repeat with new sessions. Don’t let it read the buggy code, it will just get worse.
First Variant:
1. User: Asks coding question
2. Ai: Outputs half functioning code
3. User: Asks to fix specific things
4. Ai: Creates buggy code
5. User: asks again to fix things
6. Ai: writes even more buggy code
Proposed second variant with copying code:
Until step 4 everything stays the same, but instead of asking it to fix the code again you copy it into another session, this way, you'll repeat step 3 again, without the LLM "seeing" the code it previously generated for step 4.