From the course: Accelerate DevOps and Software Development with AI: Modern Tools and Workflows for Enhanced Software Delivery

Iterative prompting to guide AI to better code

- So, what about iterative prompting? This is a powerful approach. Think of your prompt as version number one. You look at the response, then refine your request. Now let's make it async, add retry logic, remove third-party dependencies. Can you explain this line by line? Each one builds on the previous output. This is how you collaborate with the model, treating it less like a vending machine and more like a junior developer sitting right next to you. Let's look at a quick example. We're going to switch back to ChatGPT, and I'm going to ask it to implement a function that computes the average of numbers. If numbers is empty, we'll return zero, and we don't want it to use any external libraries. Great. Next, I'm going to ask it to update this function to raise a type error while keeping everything else the same. Lastly, I'm going to ask it to add Google style doc strings with two examples. The AI isn't just guessing randomly, it's reacting to your instructions and the patterns it's seen during training. An important constraint you should remember here is of context windows. Models don't have infinite memory. They read a fixed size context window. If our conversation or code gets long, earlier details can fall out and the model may forget constraints. To help with this, you can pin your constraints at the top. So switching back to ChatGPT, I could add a constraints section right before my next request. Additionally, I can also ask for a self check before any code is generated. So in ChatGPT, I'll say, "Before you modify the code, list the constraints you're following in four bullet points." Now, a few advanced tips. Number one, use examples in your prompt. If you say "Generate JSON in this format" and show an example block, you'll get much better results. Avoid ambiguous language. Words like optimize or make better are open-ended. Be specific. For example, reduce time complexity from O of N squared to O and log N using a heap. And finally, ask for constraints. Say things like under 20 lines, or no external libraries, or works in a serverless environment. So, let's recap. Prompt engineering is about being intentional. You are shaping the AI's behavior by being clear about what you want, how you want it, and why. Whether you are debugging, refactoring, or generating code from scratch, the quality of your prompt often determines the quality of your output. And like any other skill, it improves with practice. Don't be afraid to rephrase, retry, and experiment, because once you get the hang of it, these tools stop feeling random and start feeling like an actual partner in your workflow.

Contents