Try a prompt that helps claude iterate until it can verify the result.
For example, if you tell it to compile and run tests, you should never be in a situation with syntax errors.
But if you don’t give a prompt that allows to validate the result, then it’s going to get you whatever.