0Pricing
AI Prompt Engineering · Lesson

Modular Prompt Sections

Separating context, instructions, constraints, and output format cleanly.

Modular Prompt Sections is a free AI Prompt Engineering lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Problem with Monolithic Prompts

A monolithic prompt stuffs everything into one paragraph: background, task, rules, examples, and output format all mixed together. As prompts grow in complexity, monolithic structure causes:

  • Models misapplying rules to the wrong sections
  • Difficulty debugging which part caused a bad output
  • Impossible-to-maintain prompt strings
  • Inconsistent behavior across model versions

Modular prompts solve all of these problems.

The Five Core Prompt Sections

A well-structured prompt has five distinct sections, each with a clear purpose:

  • Context — Background the model needs to understand the situation
  • Task — What the model must do, stated clearly
  • Constraints — Rules and limits on the response
  • Output Format — Structure, length, and schema of the expected response
  • Examples — Demonstrations of correct behavior

Not every prompt needs all five. Use only what is necessary.

Context Section

The context section provides background information without telling the model what to do. Good context includes:

  • Who the end user is
  • What platform or product this is for
  • Relevant domain knowledge
  • Any prior state the model should be aware of
context_section = '''
<context>
You are assisting a customer support agent at a B2B SaaS company.
The company sells project management software used by engineering teams.
Customers are typically CTOs, engineering managers, or senior developers.
The support agent is handling a live chat conversation with a customer.
</context>
'''

print(context_section)

Task Section

The task section states exactly what the model must produce. It should be specific, action-oriented, and free of background noise.

task_section = '''
<task>
Draft a response to the customer message below.
The response should:
1. Acknowledge the customer's issue
2. Provide a concrete next step
3. Set a realistic expectation for resolution time
</task>

<customer_message>
Our Gantt chart view stopped loading after the last update.
This is blocking our sprint planning session today.
</customer_message>
'''

print(task_section)

Constraints Section

Constraints define the guardrails — what the model must NOT do and explicit limits it must respect.

constraints_section = '''
<constraints>
- Do not promise a fix by a specific date unless you are certain.
- Do not mention competitor products by name.
- Keep the response under 100 words.
- Use a professional but empathetic tone.
- Do not ask the customer for information already provided in their message.
</constraints>
'''

print(constraints_section)

Output Format Section

The output format section tells the model exactly how to structure its response — JSON schema, markdown headers, plain text, numbered lists, etc.

output_format_section = '''
<output_format>
Respond with a JSON object containing:
{
  "subject": "string (email subject line)",
  "body": "string (email body, plain text, under 150 words)",
  "priority": "high | medium | low",
  "escalate": true | false
}
Do not include any text outside the JSON object.
</output_format>
'''

print(output_format_section)

Examples Section

Examples show the model a correct input-output pair. Place examples after constraints so the model sees the rules before the demonstration.

examples_section = '''
<examples>
  <example>
    <input>Customer: My invoices are not downloading.</input>
    <output>{
      "subject": "Invoice Download Issue",
      "body": "Thank you for reaching out. We see the issue with invoice downloads and our team is investigating. We expect a fix within 2 hours. We will email you once resolved.",
      "priority": "high",
      "escalate": true
    }</output>
  </example>
</examples>
'''

print(examples_section)

Assembling a Modular Prompt

The modular approach makes assembly and editing straightforward. Each section is independently modifiable.

def build_support_prompt(customer_message):
    context = '<context>B2B SaaS customer support for project management software.</context>'
    task = f'<task>Draft a JSON response to this customer message.</task>\n<customer_message>{customer_message}</customer_message>'
    constraints = '<constraints>Under 100 words. Professional tone. No competitor names.</constraints>'
    output_fmt = '<output_format>Return JSON: {"subject": str, "body": str, "priority": str, "escalate": bool}</output_format>'
    return '\n\n'.join([context, task, constraints, output_fmt])

message = 'The Gantt chart stopped loading after your last update.'
print(build_support_prompt(message))

Benefits for Debugging

When a modular prompt produces bad output, you can isolate the problem section:

  • Wrong task performed? — Check the Task section
  • Violated a rule? — Check the Constraints section
  • Wrong output structure? — Check the Output Format section
  • Poor quality? — Check the Examples section or add more context

You can change one section at a time and re-test without rewriting the entire prompt.

Benefits for Maintenance

Modular prompts stored as structured objects or functions are far easier to maintain than long string literals:

  • Update the context when your product changes without touching task logic
  • Swap output format from JSON to markdown without changing constraints
  • A/B test different example sets while keeping everything else constant
  • Version control individual sections independently
class Prompt:
    def __init__(self):
        self.context = ''
        self.task = ''
        self.constraints = []
        self.output_format = ''
        self.examples = []

    def build(self):
        parts = []
        if self.context:
            parts.append(f'<context>\n{self.context}\n</context>')
        if self.task:
            parts.append(f'<task>\n{self.task}\n</task>')
        if self.constraints:
            c = '\n'.join(f'- {r}' for r in self.constraints)
            parts.append(f'<constraints>\n{c}\n</constraints>')
        if self.output_format:
            parts.append(f'<output_format>\n{self.output_format}\n</output_format>')
        return '\n\n'.join(parts)

When to Skip Sections

Not every prompt needs all five sections. Use this guide:

  • Skip Context when the task is self-explanatory (e.g., translate this sentence)
  • Skip Constraints for simple extraction tasks with no edge cases
  • Skip Examples when the task is straightforward or the model already performs it well zero-shot
  • Always include Task and Output Format — these are the minimum viable sections

Over-specifying simple prompts adds noise without benefit.

Quick Check

A modular prompt produces incorrect output structure. Which section should you check first?

Modular Sections — Key Takeaways

Modular prompt sections are the foundation of maintainable, debuggable prompt engineering:

  • Five core sections: Context, Task, Constraints, Output Format, Examples
  • Each section has a single responsibility — mixing them creates confusion
  • Modular structure enables section-by-section debugging and A/B testing
  • Store prompts as structured objects or functions, not raw strings
  • Skip sections that are not needed — over-specifying adds noise
  • Always include at least Task and Output Format for every non-trivial prompt

Frequently asked questions

Is the “Modular Prompt Sections” lesson free?

Yes — the full text of “Modular Prompt Sections” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.

What will I learn in “Modular Prompt Sections”?

Separating context, instructions, constraints, and output format cleanly. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Prompt Engineering?

No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modular Prompt Sections” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Prompt Engineering lesson?

Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Using XML Tags as Delimiters
  2. Modular Prompt Sections
  3. Header-Body-Footer Prompt Pattern
  4. Prompt Organization Best Practices
← Back to AI Prompt Engineering