Skip to main content
Back to Library
Prompt Engineering Guide

Mastering Debug code
on Grok-1

Stop guessing. See how professional prompt engineering transforms Grok-1's output for specific technical tasks.

The "Vibe" Prompt

"Hey Grok, I've got some code that's not working right. Can you take a look and debug it for me? It's Python, and I'm getting an error about an undefined variable. Here's the code: ```python def calculate_discount(price, discount_percentage): if discount_percentage > 0: final_price = price * (1 - discount_percentage / 100) print(f"Final price: {final_price}") return final_price calculate_discount(100, 10) calculate_discount(50, 0) ``` What's going on?"
Low specificity, inconsistent output

Optimized Version

STABLE
You are an expert Python debugger. Your task is to identify and fix bugs in Python code. Here's the code that requires debugging: ```python def calculate_discount(price, discount_percentage): if discount_percentage > 0: final_price = price * (1 - discount_percentage / 100) print(f"Final price: {final_price}") return final_price calculate_discount(100, 10) calculate_discount(50, 0) ``` **Thought Process:** 1. **Analyze the error:** The user mentioned an 'undefined variable' error. I need to look for variable dereferencing before assignment under certain conditions. 2. **Examine variable scopes:** Specifically, `final_price` is defined within an `if` block. 3. **Identify conditional execution:** If `discount_percentage` is not greater than 0, the `final_price` assignment inside the `if` block will be skipped. 4. **Trace execution paths:** For `calculate_discount(50, 0)`, the `if` condition `0 > 0` is `False`. Therefore, `final_price` is never assigned a value. 5. **Locate the point of failure:** The `print(f"Final price: {final_price}")` and `return final_price` statements are outside the `if` block but depend on `final_price` being defined. 6. **Formulate a fix:** Initialize `final_price` before the `if` block, or ensure all execution paths assign a value to it. Assigning `price` as a default value when no discount applies is logical. **Please provide:** 1. A clear explanation of the bug. 2. The corrected Python code block. 3. A brief explanation of how the fix resolves the problem.
Structured, task-focused, reduced hallucinations

Engineering Rationale

The optimized prompt leverages several powerful techniques. Firstly, it establishes a clear 'expert' persona, guiding the model's approach. Secondly, it explicitly outlines the task. Most importantly, it incorporates a 'Thought Process' section using Chain-of-Thought (CoT) reasoning. This CoT breaks down the debugging process into discrete, logical steps, forcing the model to emulate human problem-solving. This structured thinking helps the model systematically identify the root cause of the error (unassigned variable due to conditional logic) before attempting a solution. It also specifies the desired output format, ensuring a comprehensive and actionable response (explanation, fixed code, fix explanation). The naive prompt is conversational and lacks direction, relying solely on the model's general understanding without guiding its thought process, which can lead to less precise or incomplete answers.

0%
Token Efficiency Gain
The optimized prompt explicitly asks for a 'clear explanation of the bug'.
The optimized prompt explicitly asks for 'The corrected Python code block'.
The optimized prompt explicitly asks for 'A brief explanation of how the fix resolves the problem'.

Ready to stop burning tokens?

Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.

Optimize My Prompts