Back to Blog
Prompt Engineering for Developers: Getting Better AI Results
Prompts Are Programs
A prompt is essentially a program written in natural language. Like any program, it needs clear instructions, well-defined inputs, and expected output formats. Poor prompts lead to unpredictable results.
The Anatomy of a Good Prompt
$prompt = <<
Key Techniques
1. Be Specific
// Vague
"Write a function to process orders"
// Specific
"Write a PHP function that:
- Takes an Order model and array of items
- Validates inventory availability
- Calculates total with tax
- Returns OrderResult with status and message"
2. Provide Examples (Few-Shot Learning)
$prompt = <<= DATE_TRUNC('month', CURRENT_DATE)
Example 2:
Input: "Count orders by status"
Output: SELECT status, COUNT(*) FROM orders GROUP BY status
Now convert:
Input: "{$userQuery}"
Output:
PROMPT;
3. Chain of Thought
$prompt = <<
4. Output Formatting
// Force structured output
$prompt .= <<
Temperature and Parameters
- Temperature 0: Deterministic, best for factual/coding tasks
- Temperature 0.3-0.7: Balanced creativity and coherence
- Temperature 1.0+: Creative, unpredictable outputs
Testing Prompts
class PromptTest extends TestCase
{
public function test_sql_generation_prompt(): void
{
$testCases = [
['input' => 'Get all active users', 'expected_contains' => 'WHERE'],
['input' => 'Count by category', 'expected_contains' => 'GROUP BY'],
];
foreach ($testCases as $case) {
$result = $this->ai->generate($this->buildPrompt($case['input']));
$this->assertStringContainsString($case['expected_contains'], $result);
}
}
}
Conclusion
Prompt engineering is a skill that improves with practice. Be explicit, provide examples, and test thoroughly. Your prompts are code—treat them with the same rigor.
Related Articles
Need Help With Your Project?
I respond to all inquiries within 24 hours. Let's discuss how I can help build your production-ready system.
Get In Touch