Prompting
Constraint Engineering
Add precise constraints to eliminate unwanted outputs and keep AI responses focused on what matters.
Intermediate Free Published: April 15, 2026
Compatible Tools claude-codechatgptgeminicopilotcursorwindsurfuniversal
The Problem
Without explicit boundaries, AI produces verbose, unfocused responses that wander into irrelevant territory. You ask for a database query and get a lecture on normalization theory. You ask for a code fix and receive an entire refactoring plan. Constraints act as guardrails that channel the model’s output into exactly the shape and scope you need.
The Prompt
Answer the following question with these constraints:
SCOPE CONSTRAINTS:
- Only address [specific topic/technology/version]
- Do not mention [excluded topics]
- Limit response to [X] lines/paragraphs/items
FORMAT CONSTRAINTS:
- Use [specific format: code only / bullets only / table]
- Include [required elements: examples, sources, metrics]
- Exclude [unwanted elements: disclaimers, alternatives, caveats]
QUALITY CONSTRAINTS:
- All code must be production-ready, not pseudocode
- All recommendations must work with [specific version/environment]
- Assume the reader is a [experience level] developer
QUESTION:
[Your question]
Example Output
PostgreSQL 16 query optimization for your users table (12M rows):
1. Add partial index for active users:
CREATE INDEX idx_users_active ON users (email) WHERE status = 'active';
2. Replace sequential scan on login:
-- Before: 2400ms
SELECT * FROM users WHERE LOWER(email) = LOWER($1);
-- After: 3ms (with expression index)
CREATE INDEX idx_users_email_lower ON users (LOWER(email));
SELECT * FROM users WHERE LOWER(email) = LOWER($1);
3. Add covering index for the dashboard query:
CREATE INDEX idx_users_dashboard ON users (created_at DESC) INCLUDE (name, email, plan);
When to Use
Use constraint engineering when past prompts returned too-broad or too-vague responses, when you need output that plugs directly into a workflow without editing, or when you are iterating on prompt quality and need to systematically eliminate specific failure modes.
Pro Tips
- Stack constraints progressively — start with a minimal prompt, identify what went wrong, then add one constraint per iteration until the output matches your needs.
- Use “MUST” and “MUST NOT” — these keywords create harder boundaries than “try to” or “prefer.”
- Constrain length explicitly — “maximum 5 bullet points” is more effective than “be concise.”
- Test with adversarial inputs — try questions that might tempt the model to break your constraints, then tighten the ones that fail.