0
0
GenaiHow-ToBeginner · 3 min read

How to Use Constraints in Prompts for Better AI Responses

To use constraints in prompts, clearly specify rules or limits within the prompt text that guide the AI's response. Constraints can include format, length, style, or content restrictions to ensure the output meets your needs.
📐

Syntax

When adding constraints in prompts, use clear and direct language to specify what the AI should or should not do. Common parts include:

  • Instruction: What you want the AI to do.
  • Constraint: Rules or limits to follow.
  • Example (optional): Show the desired output style or format.

Example syntax pattern:

"Please generate a summary of the text. Limit the summary to 3 sentences. Use simple language."
text
Please write a poem about nature. Limit the poem to 4 lines. Use only simple words.
💻

Example

This example shows how to add constraints to get a short, formal email reply from an AI model.

python
prompt = (
    "Write a professional email response to a meeting request. "
    "Keep it under 50 words and use a polite tone."
)

# Simulated AI response function

def ai_response(prompt_text):
    # This is a placeholder for an actual AI call
    if "under 50 words" in prompt_text and "polite tone" in prompt_text:
        return (
            "Dear Alex,\nThank you for your meeting request. "
            "I am available on Tuesday at 10 AM. Looking forward to our discussion.\nBest regards,\nJamie"
        )
    return ""

response = ai_response(prompt)
print(response)
Output
Dear Alex, Thank you for your meeting request. I am available on Tuesday at 10 AM. Looking forward to our discussion. Best regards, Jamie
⚠️

Common Pitfalls

Common mistakes when using constraints in prompts include:

  • Vague constraints: Not specifying clear limits leads to unpredictable outputs.
  • Conflicting instructions: Giving contradictory rules confuses the AI.
  • Too complex constraints: Overloading the prompt with many rules can reduce response quality.

Always keep constraints simple, clear, and consistent.

python
wrong_prompt = "Write a story. Make it very short but also very detailed and long."

right_prompt = "Write a story that is exactly 100 words long."

print("Wrong prompt may confuse AI:", wrong_prompt)
print("Clear constraint example:", right_prompt)
Output
Wrong prompt may confuse AI: Write a story. Make it very short but also very detailed and long. Clear constraint example: Write a story that is exactly 100 words long.
📊

Quick Reference

Constraint TypeExample PhrasePurpose
LengthLimit to 3 sentencesControl output size
StyleUse formal toneSet writing style
FormatProvide answer as a listDefine output structure
ContentExclude personal opinionsRestrict content
LanguageUse simple words onlyControl vocabulary complexity

Key Takeaways

Always state constraints clearly and simply within the prompt text.
Use constraints to control length, style, format, or content of AI outputs.
Avoid vague or conflicting constraints to get reliable results.
Providing examples can help the AI understand constraints better.
Test and refine prompts to improve how constraints affect responses.