You're supposed to write a detailed spec first (ask the AI for help with that part of the job too!) so that it's less likely to go off track when writing the code. Then just ask it to write the code and switch to something else. Review the result when the work is done. The spec then becomes part of your documentation.