Appendix B: Common Prompting Patterns
Appendix B: Common Prompting Patterns
Section titled “Appendix B: Common Prompting Patterns”Production-ready prompt templates for PHP developers. Copy, customize, and use these proven patterns in your applications.
Table of Contents
Section titled “Table of Contents”- Code Generation
- Code Review
- Documentation
- Data Extraction
- Content Generation
- Analysis and Classification
- Debugging and Problem Solving
- Refactoring
- Testing
- System Prompts
Code Generation
Section titled “Code Generation”Generate PHP Class
Section titled “Generate PHP Class”$prompt = <<<PROMPTCreate a PHP 8.4+ class with the following requirements:
Class Name: UserRepositoryPurpose: Manage user data with database interactionsFramework: Laravel 11
Requirements:- Implement dependency injection for database- Include methods: findById(), create(), update(), delete(), search()- Add type hints for all parameters and return types- Include PHPDoc comments- Use modern PHP features (readonly properties, constructor property promotion)- Follow PSR-12 coding standards- Include error handling
Return only the class code, properly formatted.PROMPT;Usage:
use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.3, // Lower for more consistent code 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Generate API Endpoint
Section titled “Generate API Endpoint”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTGenerate a Laravel API endpoint with the following specifications:
Endpoint: POST /api/productsPurpose: Create a new productFramework: Laravel 11 with API resources
Requirements:1. FormRequest validation class with these rules: - name: required, string, max 255 - price: required, numeric, min 0 - description: optional, string - category_id: required, exists in categories table
2. Controller method with: - Dependency injection - Proper response codes (201 for success) - Error handling - API resource transformation
3. Use repository pattern for data access4. Include type hints and return types5. Follow RESTful conventions
Provide the FormRequest, Controller method, and API Resource class.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Generate Database Migration
Section titled “Generate Database Migration”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTCreate a Laravel migration for the following database schema:
Table: ordersColumns:- id (primary key, auto-increment)- user_id (foreign key to users table, cascading delete)- order_number (unique, string)- total_amount (decimal, 2 decimal places)- status (enum: pending, processing, completed, cancelled)- notes (text, nullable)- ordered_at (timestamp)- shipped_at (timestamp, nullable)- created_at, updated_at (timestamps)
Additional requirements:- Add indexes on: user_id, order_number, status, ordered_at- Add foreign key constraint for user_id- Use modern Laravel migration syntax- Include down() method for rollback
Return the complete migration file.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Code Review
Section titled “Code Review”Comprehensive Code Review
Section titled “Comprehensive Code Review”use ClaudePhp\ClaudePhp;
$code = file_get_contents('path/to/code.php');
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTReview the following PHP code and provide detailed feedback:
{$code}
Please analyze:
1. **Code Quality** - PSR-12 compliance - Naming conventions - Code organization
2. **Security Issues** - SQL injection vulnerabilities - XSS vulnerabilities - Authentication/authorization issues - Input validation gaps
3. **Performance** - N+1 query problems - Inefficient algorithms - Memory usage concerns
4. **Best Practices** - SOLID principles violations - Design pattern opportunities - Error handling improvements
5. **Bugs and Logic Errors** - Potential runtime errors - Edge cases not handled - Type safety issues
For each issue found:- Severity (Critical, High, Medium, Low)- Line number(s)- Explanation- Suggested fix with code example
Format as a structured report.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 4096, 'temperature' => 0.2, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Security Audit
Section titled “Security Audit”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTPerform a security audit on this PHP code. Focus exclusively on security vulnerabilities:
```php{CODE_HERE}Check for:
- SQL Injection (raw queries, improper parameterization)
- XSS (unescaped output, unsafe HTML rendering)
- CSRF vulnerabilities
- Authentication bypass opportunities
- Authorization issues
- Insecure file uploads
- Path traversal vulnerabilities
- Insecure cryptography
- Sensitive data exposure
- Missing input validation
For each vulnerability:
- OWASP category
- Risk level (Critical/High/Medium/Low)
- Attack vector
- Code example of the fix
- Prevention best practices
Return findings in order of severity. PROMPT;
client->messages()->create([ ‘model’ => ‘claude-sonnet-4-5-20250929’, ‘max_tokens’ => 4096, ‘temperature’ => 0.1, ‘messages’ => [ [‘role’ => ‘user’, ‘content’ => $prompt] ] ]);
---
## Documentation
### Generate PHPDoc
```php# filename: generate-phpdoc-prompt.phpuse ClaudePhp\ClaudePhp;
$code = <<<'PHP'public function processPayment($amount, $currency, $customerId, array $options = []){ // Implementation here}PHP;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTGenerate comprehensive PHPDoc documentation for this method:
{$code}
Include:- Description of what the method does- @param tags with types and descriptions- @return tag with type and description- @throws tags for possible exceptions- @example with usage example- Any relevant @see or @link references
Follow PSR-5 PHPDoc standard.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Generate README
Section titled “Generate README”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTCreate a comprehensive README.md for a Laravel package with these details:
Package Name: claude-php-sdkPurpose: Official PHP SDK for Anthropic's Claude APIFeatures:- Type-safe API client- Streaming support- Tool use (function calling)- Vision API support- Built-in retry logic- Laravel integration
Include these sections:1. Badge row (build, coverage, version, license)2. Short description3. Features list4. Requirements5. Installation (Composer)6. Quick Start example7. Configuration8. Usage examples (5-6 common scenarios)9. Advanced usage10. Testing11. Contributing guidelines12. Changelog link13. License14. Credits
Use clear formatting, code examples in PHP, and professional tone.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.4, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);API Documentation
Section titled “API Documentation”$prompt = <<<PROMPTGenerate OpenAPI 3.0 documentation for this Laravel API endpoint:
Endpoint: GET /api/v1/products/{id}Controller method:```phppublic function show(Product $product): JsonResponse{ return response()->json([ 'data' => new ProductResource($product) ]);}ProductResource structure:
- id (integer)
- name (string)
- description (string, nullable)
- price (number, format: float)
- category (object with id, name)
- images (array of objects with id, url)
- created_at (string, ISO 8601)
- updated_at (string, ISO 8601)
Include:
- Path definition
- Parameters (path, query)
- Response schemas (200, 404)
- Example responses
- Security requirements (Bearer token)
Return valid OpenAPI YAML. PROMPT;
---
## Data Extraction
### Extract Structured Data
```php# filename: extract-structured-data-prompt.phpuse ClaudePhp\ClaudePhp;
$text = "John Smith ordered 3 MacBook Pros for $6,000 on March 15, 2024...";
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTExtract structured data from this text and return as JSON:
"{$text}"
Extract:- customer_name (string)- product (string)- quantity (integer)- total_amount (float)- order_date (ISO 8601 date)
Rules:- Return ONLY valid JSON- Use null for missing values- Ensure data types are correct- Parse dates to ISO 8601 format
Example output format:{ "customer_name": "John Smith", "product": "MacBook Pro", "quantity": 3, "total_amount": 6000.00, "order_date": "2024-03-15"}PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.1, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Parse Email Content
Section titled “Parse Email Content”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTParse this email and extract key information as JSON:
Email:"""{EMAIL_CONTENT}"""
Extract:{ "type": "order|inquiry|complaint|support", "priority": "low|medium|high|urgent", "customer": { "name": "string", "email": "string", "phone": "string or null" }, "subject": "brief summary", "intent": "what the customer wants", "products_mentioned": ["array of product names"], "order_number": "string or null", "requires_response": boolean, "sentiment": "positive|neutral|negative", "suggested_department": "sales|support|billing|technical"}
Return ONLY the JSON, no explanation.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.1, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Extract Invoice Data
Section titled “Extract Invoice Data”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTExtract all relevant data from this invoice text and structure it as JSON:
Invoice Text:"""{INVOICE_TEXT}"""
Required fields:{ "invoice_number": "string", "invoice_date": "YYYY-MM-DD", "due_date": "YYYY-MM-DD", "vendor": { "name": "string", "address": "string", "tax_id": "string or null" }, "customer": { "name": "string", "address": "string" }, "line_items": [ { "description": "string", "quantity": number, "unit_price": number, "total": number } ], "subtotal": number, "tax": number, "total": number, "currency": "USD|EUR|GBP etc"}
Validate that line_items sum to subtotal.Return ONLY valid JSON.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.1, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Content Generation
Section titled “Content Generation”Product Description
Section titled “Product Description”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTGenerate a compelling product description for an e-commerce website:
Product: {PRODUCT_NAME}Category: {CATEGORY}Key Features:- {FEATURE_1}- {FEATURE_2}- {FEATURE_3}
Target Audience: {TARGET_AUDIENCE}Tone: Professional but friendlyLength: 150-200 words
Include:1. Attention-grabbing opening2. Key benefits (not just features)3. Use case scenarios4. Call to action
Focus on benefits over features. Use persuasive language.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.7, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Blog Post Outline
Section titled “Blog Post Outline”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTCreate a detailed blog post outline on this topic:
Topic: {BLOG_TOPIC}Target Audience: {AUDIENCE}Goal: {GOAL - educate/convince/entertain}SEO Keyword: {KEYWORD}Word Count Target: {WORD_COUNT}
Generate an outline with:1. Compelling title (include keyword)2. Meta description (155 characters max)3. Introduction hook4. 5-7 main sections with: - Section title - 3-4 subsection points - Key takeaways5. Conclusion with call-to-action6. Suggested internal/external links
Make it SEO-optimized and engaging.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.6, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Email Template
Section titled “Email Template”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTWrite a professional email template for:
Purpose: {PURPOSE - welcome, order confirmation, password reset, etc}Recipient: {RECIPIENT_TYPE}Tone: {TONE - formal, friendly, urgent}
Include:- Compelling subject line- Personalization placeholders (\{\{name\}\}, \{\{order_number\}\}, etc)- Clear main message- Action button/CTA- Footer with contact info- Compliance text if needed
Format as HTML email template with inline CSS.Keep it mobile-responsive.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.5, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Analysis and Classification
Section titled “Analysis and Classification”Sentiment Analysis
Section titled “Sentiment Analysis”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTAnalyze the sentiment of this customer review:
Review: "{REVIEW_TEXT}"
Provide analysis as JSON:{ "overall_sentiment": "positive|neutral|negative", "confidence": 0.0-1.0, "aspects": { "product_quality": "positive|neutral|negative", "customer_service": "positive|neutral|negative", "value_for_money": "positive|neutral|negative", "delivery": "positive|neutral|negative" }, "key_phrases": ["array", "of", "important", "phrases"], "actionable_issues": ["array of issues to address"], "emotion": "happy|satisfied|frustrated|angry|disappointed|neutral", "requires_response": boolean}
Return ONLY the JSON.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.1, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Content Classification
Section titled “Content Classification”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTClassify this user-generated content:
Content: "{CONTENT}"
Classify as JSON:{ "categories": ["primary_category", "secondary_category"], "content_type": "question|review|complaint|suggestion|spam|other", "language": "en|es|fr|de|etc", "toxicity": { "is_toxic": boolean, "severity": "none|low|medium|high", "types": ["profanity|hate_speech|harassment|etc"] }, "topics": ["array", "of", "topics"], "requires_moderation": boolean, "suggested_action": "approve|flag|reject|escalate"}
Be conservative with toxicity flagging.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.1, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Topic Extraction
Section titled “Topic Extraction”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTExtract and categorize topics from this article:
Article:"""{ARTICLE_TEXT}"""
Return JSON:{ "main_topic": "string", "subtopics": ["array", "of", "subtopics"], "keywords": ["ranked", "by", "relevance"], "entities": { "people": ["person names"], "organizations": ["org names"], "locations": ["place names"], "technologies": ["tech mentioned"] }, "summary": "2-3 sentence summary", "recommended_tags": ["seo", "friendly", "tags"]}PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.2, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Debugging and Problem Solving
Section titled “Debugging and Problem Solving”Debug Error
Section titled “Debug Error”use ClaudePhp\ClaudePhp;
$error = "Call to undefined method App\Models\User::getFullname()";$code = file_get_contents('User.php');
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTDebug this PHP error:
Error Message:{$error}
Code:{$code}
Stack Trace (if available):{$stackTrace}
Provide:1. Root cause explanation2. Why this error occurred3. Step-by-step fix4. Code example of the solution5. How to prevent similar errors6. Related best practices
Be specific and actionable.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.2, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Performance Optimization
Section titled “Performance Optimization”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTAnalyze this code for performance issues and suggest optimizations:
```php{CODE}```
Current performance metrics:
- Execution time: {TIME}- Memory usage: {MEMORY}- Database queries: {QUERY_COUNT}
Identify:
1. Performance bottlenecks2. N+1 query problems3. Inefficient algorithms4. Memory leaks5. Missing indexes
For each issue:
- Impact (High/Medium/Low)- Explanation- Optimized code example- Expected improvement
Prioritize by impact.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.2, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Refactoring
Section titled “Refactoring”Refactor to Design Pattern
Section titled “Refactor to Design Pattern”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTRefactor this code to use an appropriate design pattern:
Current Code:```php{CODE}```
Issues with current code:
- {ISSUE_1}- {ISSUE_2}
Requirements:
1. Suggest the most suitable design pattern2. Explain why this pattern fits3. Provide refactored code using the pattern4. Show before/after comparison5. List benefits of the refactoring6. Note any trade-offs
Use PHP 8.4+ features where appropriate.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Extract Service Class
Section titled “Extract Service Class”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTThis controller has too much business logic. Extract it into a service class:
Current Controller:```php{CONTROLLER_CODE}```
Create:
1. A service class with appropriate methods2. Interface for the service (dependency inversion)3. Updated controller using the service4. Unit test example for the service
Follow:
- Single Responsibility Principle- Dependency Injection- Type hints and return types- PSR-12 standards PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Testing
Section titled “Testing”Generate Unit Test
Section titled “Generate Unit Test”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTGenerate comprehensive PHPUnit tests for this class:
Class to Test:```php{CLASS_CODE}```
Create tests that:
1. Cover all public methods2. Test happy paths and edge cases3. Test error conditions4. Use data providers for multiple scenarios5. Mock dependencies appropriately6. Include setUp and tearDown if needed7. Follow PHPUnit best practices8. Aim for 100% code coverage
Use PHPUnit 10 syntax.Include descriptive test method names.PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 2048, 'temperature' => 0.3, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);Generate Test Cases
Section titled “Generate Test Cases”use ClaudePhp\ClaudePhp;
$client = new ClaudePhp( apiKey: getenv('ANTHROPIC_API_KEY'));
$prompt = <<<PROMPTGenerate test cases for this user registration feature:
Feature: User RegistrationRequirements:- Email must be unique and valid- Password must be 8+ characters with 1 number and 1 special char- Username must be 3-20 alphanumeric characters- Terms acceptance is required
Generate test cases covering:1. Valid registration (happy path)2. Invalid email formats3. Duplicate email4. Weak passwords5. Invalid usernames6. Missing required fields7. SQL injection attempts8. XSS attempts
Format as a test case table with:- Test ID- Description- Input data- Expected result- Priority (P0/P1/P2)PROMPT;
$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'temperature' => 0.2, 'messages' => [ ['role' => 'user', 'content' => $prompt] ]]);System Prompts
Section titled “System Prompts”System prompts define Claude’s role and behavior. Use these in the system parameter.
Code Review Assistant
Section titled “Code Review Assistant”$systemPrompt = <<<'SYSTEM'You are an expert PHP code reviewer with 10+ years of experience.
Your expertise includes:- PHP 8.4+ features and best practices- Laravel and Symfony frameworks- PSR standards (PSR-1, PSR-12, PSR-4)- SOLID principles and design patterns- Security best practices (OWASP Top 10)- Performance optimization- Database optimization
When reviewing code:1. Be constructive and educational2. Explain WHY something is an issue, not just WHAT3. Provide code examples for fixes4. Prioritize issues by severity5. Acknowledge good practices too6. Consider context and trade-offs7. Focus on actionable feedback
Format reviews clearly with:- Severity levels (Critical, High, Medium, Low)- Line numbers- Explanations- Code examples
Be thorough but concise.SYSTEM;API Documentation Writer
Section titled “API Documentation Writer”$systemPrompt = <<<'SYSTEM'You are a technical documentation specialist for API documentation.
Your responsibilities:- Write clear, accurate API documentation- Follow OpenAPI 3.0 specification- Provide realistic code examples- Document all parameters, responses, errors- Include authentication requirements- Show cURL and PHP examples- Write for developers of all skill levels
Documentation standards:- Use consistent terminology- Explain complex concepts simply- Provide complete, working examples- Document edge cases and errors- Include rate limiting information- Show best practices
Format:- Use markdown for readability- Include code syntax highlighting- Organize logically- Add links to related endpointsSYSTEM;Data Extraction Specialist
Section titled “Data Extraction Specialist”$systemPrompt = <<<'SYSTEM'You are a data extraction and parsing specialist.
Rules:1. Always return valid JSON unless otherwise specified2. Preserve data types (string, number, boolean, null)3. Use null for missing/unavailable data4. Parse dates to ISO 8601 format (YYYY-MM-DD)5. Normalize data (trim whitespace, consistent casing)6. Validate extracted data for consistency7. Never make up data - use null if uncertain8. Handle currency correctly (float with 2 decimals)
For ambiguous data:- Make reasonable inferences based on context- Flag low-confidence extractions- Provide alternative interpretations if needed
Always prioritize accuracy over completeness.SYSTEM;Security Auditor
Section titled “Security Auditor”$systemPrompt = <<<'SYSTEM'You are a security expert specializing in PHP application security.
Focus areas:- OWASP Top 10 vulnerabilities- PHP-specific security issues- Laravel security best practices- Authentication and authorization flaws- Input validation and output escaping- SQL injection, XSS, CSRF- Cryptography and data protection- API security- Dependency vulnerabilities
When auditing code:1. Identify all security risks, even potential ones2. Rate severity: Critical, High, Medium, Low3. Explain the attack vector4. Provide proof-of-concept if relevant5. Suggest specific remediation6. Reference OWASP/CVE when applicable
Be thorough and paranoid - security is critical.Assume all input is malicious until proven otherwise.SYSTEM;Advanced Patterns
Section titled “Advanced Patterns”Prompt Caching
Section titled “Prompt Caching”Prompt caching allows you to cache frequently used system prompts or message prefixes, reducing costs and latency for repeated content.
// Cache a system prompt for 5 minutes$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'system' => 'You are an expert PHP code reviewer. Review code for security, performance, and best practices.', 'messages' => [ ['role' => 'user', 'content' => 'Review this code: ' . $code] ], 'metadata' => [ 'cache_control' => [ 'type' => 'ephemeral', 'ttl_seconds' => 300 // 5 minutes ] ]]);
// For 1-hour cache (important but less frequent)$response = $client->messages()->create([ 'model' => 'claude-sonnet-4-5-20250929', 'max_tokens' => 1024, 'system' => 'You are an expert PHP code reviewer...', 'messages' => [ ['role' => 'user', 'content' => 'Review this code: ' . $code] ], 'metadata' => [ 'cache_control' => [ 'type' => 'ephemeral', 'ttl_seconds' => 3600 // 1 hour ] ]]);Benefits:
- Reduced costs for repeated context
- Lower latency for cached prompts
- Best for system prompts or long prefixes that don’t change
When to Use:
- System prompts that define behavior
- Long document prefixes in RAG applications
- Frequently reused prompt templates
Chain of Thought Prompting
Section titled “Chain of Thought Prompting”For complex reasoning tasks:
$prompt = <<<PROMPTSolve this problem step by step:
Problem: {PROBLEM_DESCRIPTION}
Please:1. First, analyze what the problem is asking2. Break down the problem into smaller steps3. Solve each step, showing your reasoning4. Verify your solution5. Provide the final answer
Think through each step carefully before moving to the next.PROMPT;Few-Shot Learning
Section titled “Few-Shot Learning”Provide examples to establish patterns:
$prompt = <<<PROMPTExtract product information from these listings and format as JSON.
Example 1:Input: "iPhone 14 Pro - $999 - 256GB - Space Black"Output: {"product": "iPhone 14 Pro", "price": 999.00, "variant": "256GB Space Black"}
Example 2:Input: "Samsung Galaxy S23 Ultra (512GB, Phantom Black) - $1,199.99"Output: {"product": "Samsung Galaxy S23 Ultra", "price": 1199.99, "variant": "512GB Phantom Black"}
Now extract from:Input: "{NEW_LISTING}"Output:PROMPT;Role-Based Prompting
Section titled “Role-Based Prompting”$prompt = <<<PROMPTYou are a senior Laravel developer with 8 years of experience.
A junior developer asks:"{QUESTION}"
Provide a comprehensive answer that:- Explains the concept clearly- Shows a code example- Mentions best practices- Points out common pitfalls- Suggests further reading
Be encouraging and educational.PROMPT;Tips for Effective Prompting
Section titled “Tips for Effective Prompting”Be Specific
Section titled “Be Specific”❌ Bad: “Review this code” ✅ Good: “Review this PHP code for security vulnerabilities, focusing on SQL injection and XSS. Provide severity ratings and specific fixes.”
Provide Context
Section titled “Provide Context”$prompt = "Context: This is a payment processing service for an e-commerce platform handling $1M monthly.
Requirements: The code must be PCI-DSS compliant and handle failures gracefully.
Task: Review this payment processing code for security and reliability issues.
Code:```php{$code}“;
### Use Delimiters
```php$prompt = <<<PROMPTAnalyze the code between the XML tags:
<code>{$phpCode}</code>
Provide refactoring suggestions.PROMPT;Specify Output Format
Section titled “Specify Output Format”$prompt = <<<PROMPTExtract user data and return ONLY valid JSON with no explanation.
Required format:{"name": "string", "email": "string", "age": number}
Text: "{$text}"PROMPT;Set Temperature Appropriately
Section titled “Set Temperature Appropriately”// For consistent, deterministic outputs (code generation, data extraction)'temperature' => 0.0
// For balanced outputs (most use cases)'temperature' => 0.7
// For creative outputs (content writing, brainstorming)'temperature' => 1.0Prompt Template Library (PHP)
Section titled “Prompt Template Library (PHP)”Helper function to use templates:
class PromptTemplate{ private string $template;
public function __construct(string $template) { $this->template = $template; }
public function render(array $variables): string { $prompt = $this->template;
foreach ($variables as $key => $value) { $placeholder = '{' . strtoupper($key) . '}'; $prompt = str_replace($placeholder, $value, $prompt); }
return $prompt; }}
// Usage$template = new PromptTemplate(<<<PROMPTGenerate a {TYPE} for:
Name: {NAME}Description: {DESCRIPTION}
Requirements:{REQUIREMENTS}PROMPT);
$prompt = $template->render([ 'type' => PHP class', 'name' => 'ProductRepository', 'description' => 'Manages product data', 'requirements' => "- Use Laravel\n- Include CRUD methods\n- Add type hints"]);Additional Resources
Section titled “Additional Resources”- Anthropic Prompt Engineering Guide - Official guide
- Prompt Library - More examples
- Chapter 05: Prompt Engineering Basics - Full chapter on prompting
::: tip Quick Navigation
- ← Appendix A: API Reference - Complete API reference
- ← Appendix B: Prompting Patterns - Prompt templates
- Appendix C: Error Codes → - Troubleshooting guide
- Appendix D: Resources → - Tools and resources
- Back to Series - Return to main series :::
Last updated: November 2024