Iterating and refining prompts in AI for Everyone - Time & Space Complexity
When working with AI prompts, it is important to understand how the time to get a good answer grows as you try more versions.
We want to know how the effort changes when you keep improving your prompt step by step.
Analyze the time complexity of the following code snippet.
prompt = initial_prompt
for attempt in range(n):
response = AI.generate(prompt)
prompt = refine(prompt, response)
This code sends a prompt to an AI, gets a response, then improves the prompt based on that response, repeating this process n times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a prompt to the AI and refining it.
- How many times: Exactly n times, once per loop iteration.
Each new attempt adds one more prompt sent and one refinement step.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prompt sends and refinements |
| 100 | 100 prompt sends and refinements |
| 1000 | 1000 prompt sends and refinements |
Pattern observation: The work grows directly in proportion to the number of attempts.
Time Complexity: O(n)
This means the time grows in a straight line as you try more prompt versions.
[X] Wrong: "Refining the prompt multiple times will take the same time as just one prompt."
[OK] Correct: Each new prompt requires a new AI call and refinement, so the total time adds up with each attempt.
Understanding how repeated attempts affect time helps you plan and explain your approach when working with AI tools or iterative processes.
"What if each refinement step itself involved a loop over m items? How would the time complexity change?"