Constraint-based prompting is a technique where you define boundaries and limitations for AI output rather than prescribing exact solutions. By specifying what the code must or must not do, you guide AI toward correct solutions while allowing flexibility in implementation details.
Constraint-based prompting shifts focus from telling AI what to do toward telling it what conditions must be satisfied. This approach often produces better results for complex tasks.
| Instructions | Constraints |
|---|---|
| "Use bcrypt to hash" | "Passwords must be securely hashed" |
| "Add try-catch block" | "Must handle all errors gracefully" |
| "Return null on error" | "Must never throw unhandled exceptions" |
Constraints define the goal; instructions prescribe the method.
Must have (hard constraints):
Must not (prohibitions):
Should have (soft constraints):
Result<T, Error> type" is specific