0Pricing
AI Prompt Engineering · Lesson

Header-Body-Footer Prompt Pattern

Consistent prompt structure that scales to complex multi-task prompts.

Header-Body-Footer Prompt Pattern is a free AI Prompt Engineering lesson on CoddyKit — lesson 3 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.

Prompts as Documents

Long-form documents — reports, articles, emails — use a Header/Body/Footer structure because it is cognitively easy to navigate. Readers know what to expect in each zone.

The same principle applies to prompts. When prompts grow beyond a few sentences, a Header/Body/Footer structure makes them scannable, maintainable, and consistent.

This pattern works across models and scales from simple to highly complex prompts.

The Header Section

The Header answers two questions: Who are you? and What is the task?

It sets the model's identity (persona or role) and states the primary goal of the prompt. Everything else builds on this foundation.

A strong header is concise — typically 2-4 sentences. It does not include examples, constraints, or output rules.

header = '''
<header>
You are an expert technical writer specializing in API documentation.
Your task is to write clear, developer-friendly documentation
for the API endpoint described in the body of this prompt.
</header>
'''

print(header)

The Body Section

The Body contains the content or context the model needs to perform the task. This is the main data zone:

  • Source documents to process
  • Conversation history
  • Structured data (JSON, CSV snippets)
  • Reference material
  • The user's actual input

The body is the largest section and is most often dynamically injected at runtime.

def build_body(endpoint_spec):
    return f'''
<body>
<endpoint_specification>
{endpoint_spec}
</endpoint_specification>
</body>
'''

spec = 'POST /api/v2/users\nRequest body: {email: string, role: admin|user}\nReturns: {id: string, created_at: ISO8601}'
print(build_body(spec))

The Footer Section

The Footer contains format instructions and the output schema. It appears last because:

  • The model reads top-to-bottom and applies formatting rules to what it just processed
  • Format instructions close to the end are less likely to be ignored
  • The footer is often reusable across prompts with the same output type

The footer should be explicit: Return a JSON object with these fields, not format nicely.

footer = '''
<footer>
Format your response as a JSON object with these fields:
{
  "endpoint": "string",
  "summary": "string (one sentence)",
  "parameters": [{"name": "string", "type": "string", "required": true|false, "description": "string"}],
  "response_example": "string (JSON)",
  "error_codes": [{"code": "number", "meaning": "string"}]
}
Do not include any text outside the JSON object.
</footer>
'''

print(footer)

Assembling the Full Pattern

Combining Header, Body, and Footer into a complete, reusable prompt function:

def build_api_doc_prompt(endpoint_spec):
    header = '<header>\nYou are an expert technical writer. Document the API endpoint in the body.\n</header>'
    body = f'<body>\n<endpoint>\n{endpoint_spec}\n</endpoint>\n</body>'
    footer = '<footer>\nReturn JSON: {"summary": str, "parameters": [...], "response_example": str}\nNo text outside JSON.\n</footer>'
    return '\n\n'.join([header, body, footer])

spec = 'GET /api/products/:id\nReturns product details by ID'
print(build_api_doc_prompt(spec))

Why This Order Works

The Header/Body/Footer order mirrors how models process context:

  1. Header first — establishes identity and goal, priming the model for what follows
  2. Body second — the model now processes content through the lens of the established role and task
  3. Footer last — format instructions come right before the model generates its response, maximizing their influence on output structure

Putting format instructions in the header (first) causes them to be partially forgotten by the time the model writes its response.

Scaling to Complex Prompts

The Header/Body/Footer pattern scales naturally when prompts become complex:

def build_complex_prompt(persona, task, context_docs, constraints, output_schema):
    header = f'<header>\n{persona}\nTask: {task}\n</header>'

    docs = '\n'.join(f'<document id="{i+1}">\n{d}\n</document>' for i, d in enumerate(context_docs))
    body = f'<body>\n{docs}\n</body>'

    constraint_list = '\n'.join(f'- {c}' for c in constraints)
    footer = f'<footer>\n<constraints>\n{constraint_list}\n</constraints>\n<output_schema>\n{output_schema}\n</output_schema>\n</footer>'

    return '\n\n'.join([header, body, footer])

Reusing Footer Templates

Because the footer defines output structure, it is the most reusable section. Common footer templates:

FOOTER_JSON = '<footer>\nReturn a valid JSON object. No text outside JSON. No markdown code fences.\n</footer>'

FOOTER_MARKDOWN = '<footer>\nFormat your response as markdown.\nUse ## for section headers.\nUse bullet points for lists.\nMaximum 500 words.\n</footer>'

FOOTER_STRUCTURED = '<footer>\nRespond using this exact structure:\n1. Summary (1 sentence)\n2. Key Findings (bullet list)\n3. Recommendation (1 paragraph)\n</footer>'

print('Footer templates ready for reuse.')

Injecting Dynamic Content in the Body

The body is the section most often built dynamically at runtime. Best practices for dynamic body injection:

  • Always wrap injected content in named XML tags
  • Sanitize user input before injection to prevent prompt injection
  • Truncate very long documents and note the truncation in the body
  • Label each injected piece clearly (document_1, user_query, chat_history)
def safe_inject(user_content, max_chars=3000):
    safe = user_content.replace('<', '&lt;').replace('>', '&gt;')
    if len(safe) > max_chars:
        safe = safe[:max_chars] + '... [TRUNCATED]'
    return f'<user_input>\n{safe}\n</user_input>'

raw = 'User provided text here. Could be very long.'
print(safe_inject(raw))

Consistent Structure Across the Codebase

The real power of Header/Body/Footer emerges when your entire application uses the same pattern consistently. Benefits:

  • New team members understand any prompt instantly
  • Prompt review in code review is easier — reviewers know exactly where to look
  • Automated testing can validate each section independently
  • Prompt migration between models is straightforward — only content changes, not structure

Common Mistakes in Header/Body/Footer

Mistakes that break the pattern's effectiveness:

  • Format instructions in the header — too far from response generation; model partially ignores them
  • Task description buried in the body — model may treat it as data to process, not a directive
  • Constraints split across sections — hard to audit, leads to contradictions
  • No footer — output structure becomes unpredictable as prompt complexity increases

Quick Check

Where should output format instructions be placed in the Header/Body/Footer pattern, and why?

Header/Body/Footer — Key Takeaways

The Header/Body/Footer pattern brings document structure discipline to prompt engineering:

  • Header: Who you are + the task — sets the frame for everything that follows
  • Body: Content and context — dynamically injected, wrapped in semantic XML tags
  • Footer: Format instructions and output schema — placed last for maximum influence on output structure
  • The pattern scales naturally from simple to highly complex prompts
  • Footer templates are reusable across prompts with the same output type
  • Consistency across your codebase makes prompts reviewable, testable, and maintainable

Frequently asked questions

Is the “Header-Body-Footer Prompt Pattern” lesson free?

Yes — the full text of “Header-Body-Footer Prompt Pattern” 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 “Header-Body-Footer Prompt Pattern”?

Consistent prompt structure that scales to complex multi-task prompts. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Header-Body-Footer Prompt Pattern” 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