May 20, 2026

Prompt Engineering Patterns

Effective prompt engineering follows repeatable patterns. Here are the most useful ones.

The Specification Pattern

Start with what you want, then add constraints:

def process_user_input(text: str) -> str:
    """
    Clean and normalize user input.
    - Strip whitespace
    - Convert to lowercase
    - Remove special characters
    """
    cleaned = text.strip().lower()
    return ''.join(c for c in cleaned if c.isalnum() or c.isspace())

The Example Pattern

Show examples of input and expected output:

# Example usage:
# process_user_input("  Hello, World!  ") -> "hello world"
# process_user_input("Test@123") -> "test123"

The Constraint Pattern

Explicitly state what NOT to do:

# Do NOT use regex for this - keep it simple
# Do NOT modify the original string
# Do NOT raise exceptions for invalid input

These patterns lead to more predictable results.