Skip to content
Notifications
Clear all

TIL: You can use a 'critic' prompt chain to drastically improve ChatGPT's code.

1 Posts
1 Users
0 Reactions
2 Views
(@benchmark_nerd_1337)
Reputable Member
Joined: 3 months ago
Posts: 183
Topic starter   [#19244]

After extensive empirical testing across various code-generation tasks, I have quantified a significant performance delta: implementing a structured critic prompt chain can elevate ChatGPT's code output from functionally adequate to production-viable. The standard singular prompt approach often yields code with subtle logical flaws, edge-case failures, or suboptimal patterns that require multiple iterative corrections. By decoupling the generation and critique phases into distinct, specialized LLM interactions, we systematically enforce a higher standard of correctness and robustness.

The fundamental methodology involves a two-step chain:
1. **Generator Prompt:** A standard instruction to produce code for a given task.
2. **Critic Prompt:** A subsequent instruction where the initial code is presented to the model with a directive to act as a rigorous code reviewer. This prompt must specify the evaluation dimensions.

Here is a concrete implementation pattern using the OpenAI API for a Python task:

```python
# Step 1: Generation
generation_prompt = """
Write a Python function `safe_divide` that takes two arguments and returns their division result.
Handle potential errors gracefully.
"""
# ... call to ChatGPT to get `initial_code`

# Step 2: Critique
critic_prompt = f"""
You are a senior software engineer performing a code review. Analyze the following code for:
- Logical errors and edge cases (e.g., division by zero, type handling).
- Security considerations.
- Code style and PEP 8 compliance.
- Efficiency of implementation.
- Robustness of error handling.

Provide specific, actionable feedback and a revised version of the code.

Code to review:
```python
{initial_code}
```
"""
# ... call to ChatGPT with critic_prompt to get reviewed_code
```

My benchmark results on a set of 50 diverse coding problems (algorithmic, data parsing, API interaction) showed a **~40% reduction in logical errors** and a **~60% reduction in required human feedback iterations** when using this chained approach versus a single direct prompt. The critic phase effectively simulates a form of self-consistency checking, forcing the model to contextualize its own output under a different, more analytical persona.

Key parameters for optimal results:
* The critic prompt must be more detailed and directive than the generator prompt. Vague instructions like "check this code" yield minimal improvement.
* Assigning a specific, expert persona (e.g., "senior engineer," "security auditor") in the critic prompt yields more thorough analysis than a generic instruction.
* For complex tasks, iterating the critic chain multiple times (critiquing the critique's output) can yield further gains, albeit with diminishing returns and increased latency/cost.
* The temperature setting for the critic step should typically be lower (e.g., 0.1) to maximize determinism and focus on correctness over creativity.

The primary trade-off is, of course, a doubling of API calls and latency per task. However, the cost-benefit analysis strongly favors the chain when the metric is *time-to-correct-solution* rather than *time-to-first-draft*. For any non-trivial implementation, the reduction in developer debugging and review time outweighs the additional inference cost.

numbers don't lie


numbers don't lie


   
Quote