Prompt Engineering Tutorial 0/120 lessons ~6 min read Lesson 24

    Constraints in Prompts

    Constraints tell the model what NOT to do, how long to be, what to refuse, what voice to avoid.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Constraints tell the model what NOT to do, how long to be, what to refuse, what voice to avoid. They're the guardrails that keep output usable.

    Beginner analogy: Like dietary restrictions on a recipe — 'no nuts, gluten-free, under 20 minutes'. Constraints shape the dish without prescribing it.

    Learning objective: Apply Constraints in Prompts in a TechLearningPRO AI product — with measurable output quality, token awareness, and failure modes you can explain in review.

    Treat every prompt like production code: versioned, evaluated on a golden set, and shipped with guardrails. Prefer explicit roles, schemas, and stop conditions over vague adjectives.

    Purpose of this lesson

    Apply Constraints in Prompts in a TechLearningPRO AI product — with measurable output quality, token awareness, and failure modes you can explain in review.

    Understanding the topic

    Core concepts to understand:

    • Length: 'under 100 words', 'exactly 3 bullets'.
    • Tone: 'professional, no emojis', 'casual, friendly'.
    • Content: 'no medical advice', 'no personal opinion'.
    • Format: 'no markdown', 'JSON only'.
    • Refusals: 'if unsure, reply EXACTLY: I cannot answer'.

    Step-by-step explanation

    1. State the user job Constraints in Prompts must accomplish and the failure cost if the LLM drifts.
    2. Read the workflow diagram and map each box to system message, user turn, tools, or eval.
    3. Write the smallest correct prompt (or template) for a TechLearningPRO Orders / support scenario.
    4. Run a 10-case smoke eval: note hallucination, format break, and refusal edge cases.

    Syntax reference

    Visual workflow / architecture:

    bash
    Constraints checklist:
    ☑ length ≤100 words
    ☑ tone professional
    ☑ content no claims about effectiveness
    ☑ format JSON, no comments
    ☑ refusal say "I don't know" if unsure

    Informative example

    Worked prompt sketch:

    markdown
    # TechLearningPRO prompt sketch — Constraints in Prompts
    System: You are a careful assistant for a learning platform.
    User: Given order {{order_id}} with status {{status}}, explain the next safe action in ≤3 bullets.
    Constraints: no PII invention; cite only provided fields; JSON keys: summary, next_steps.

    Real-world use

    Legal, medical and financial AI assistants use heavy constraint sections to stay compliant.

    Security implications

    Prompts are a trust boundary. Untrusted input must never silently override system policy.

    • Separate system instructions from user content; never concatenate raw tickets into system roles.
    • Redact secrets and PII before they enter the prompt or logs.
    • Add output validators / allowlists for actions that can mutate state or spend money.

    Best practices

    • Constrain less than you think — too many constraints reduce quality.
    • Always include a refusal pattern.
    • Test constraints with adversarial inputs.

    Common mistakes

    • Shipping a prompt with zero golden-set evals and hoping production traffic is kind.
    • Stuffing the context window with unused docs instead of retrieving the right slice.
    • Letting untrusted user text redefine system instructions (prompt injection).

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    4 questions
    1BeginnerQuestionQ1. Why are constraints important in production?+

    Answer

    Constraints = guardrails for output.
    2IntermediateQuestionQ2. Where do constraints go in the prompt?+

    Answer

    Less is more — over-constraining hurts quality.
    3IntermediateQuestionQ3. How can constraints reduce hallucinations?+

    Answer

    Refusals are a constraint too.
    4AdvancedQuestionQ4. How do you test that constraints hold?+

    Answer

    Refusals are a constraint too.

    Hands-on exercise

    Hands-on + interview drill:

    • Ship a minimal evaluated prompt for Constraints in Prompts with a JSON schema and 5 failing cases.
    • Q1. Why are constraints important in production?
    • Q2. Where do constraints go in the prompt?
    • Q3. How can constraints reduce hallucinations?
    • Q4. How do you test that constraints hold?

    Summary

    Constraints in Prompts is a production prompt skill: specify the job, constrain the output, and prove quality with evals — not with a single lucky chat.

    Ready to mark this lesson complete?Track your journey across the entire course.