Refactor Loop Prompt for API Endpoint
Prompt text
Build a refactor loop for an API endpoint that keeps tests green at every step and rejects any change that grows [METRIC].
What to provide
The code or system, the failing behaviour, and the definition of green
Make it yours. Replace the bracketed inputs with your own details, adjust the constraints to your situation, and change the output format to suit your purpose. Copy the text and edit it freely.
When to use this prompt
This prompt turns the code or system into a refactor loop, with API endpoint as the context. It fits prompt-engineering work that needs the same structure every time, particularly when a single pass will not be good enough.
Inputs to supply
Paste the code or system, the failing behaviour, and the definition of green into the model's context first; the answer is only as specific as the material it can see.
- Replace the bracketed values:
[METRIC]. - Context to supply: contract compatibility, error semantics, auth and versioning.
Worked example with sample inputs
Fill the bracketed slots like this (illustrative values, not real data):
[METRIC]→ activation rate- Filled-in inputs: the code or system → a 40-line diff in orders/views.py.
- The details that change the answer: contract compatibility, error semantics, auth and versioning.
What good output looks like
The response should match the requested output and work from the material you supplied.
- Each recommendation points back to something in the input, so you can check it line by line.
- Each pass has a stopping condition and the change between passes is explained.
Two common mistakes
- Accepting an answer that drifts from the requested output. Re-ask with one short example of the shape you need.
- Leaving the API endpoint context implicit. Without it the response reads as generic refactor loop advice.
Best-suited models
Good fits for this kind of work, as of November 2026: Kimi K3.1 Pro or Qwen4-Max for open-weight iteration; Claude Fable 5.2, GPT-6.1 Sol or Gemini 4 Argon for multi-pass critique. Model versions change quickly — check the AI tool directory before relying on a specific version.
Other versions of this prompt
The same template exists for other stacks and contexts; open one to see how the wording changes:
Related Build-Test-Fix prompts
Last reviewed: · Report a problem