Skip to main content
< All Topics
Print

Chapter 19: Prompt Engineering

Chapter 19: Prompt Engineering

Last Updated: 2026-03

## 19.1 Overview

Prompt engineering is the practice of designing inputs to language models to reliably produce useful outputs. In the ITI context, prompts are assembled from multiple sources and injected into Claude at runtime. Understanding the assembly process is essential for debugging AI behavior and improving product quality.

19.2 The Four Components of a Complete Prompt

Every ITI AI interaction assembles a prompt from up to four components:


┌─────────────────────────────────────────────┐
│  1. SYSTEM PROMPT                            │
│     Role definition, capabilities,           │
│     behavioral constraints, output format    │
├─────────────────────────────────────────────┤
│  2. KNOWLEDGE BASE CONTEXT (RAG)             │
│     Retrieved document chunks injected       │
│     based on user query similarity           │
├─────────────────────────────────────────────┤
│  3. USER CONTEXT                             │
│     Dynamic user-specific data:              │
│     profile, history, preferences            │
├─────────────────────────────────────────────┤
│  4. USER MESSAGE                             │
│     The actual user request or question      │
└─────────────────────────────────────────────┘

Components 2 and 3 are optional. Not all products use RAG. Not all interactions include user context.


19.3 System Prompts

The system prompt establishes what the AI is, how it behaves, and what it can and cannot do. It is the most important component for controlling AI behavior.

Anatomy of an effective system prompt


You are [ROLE]: [BRIEF DESCRIPTION].
You work for [ORGANIZATION/PRODUCT].

## Your Capabilities
- [What you can do]
- [What you can do]

## Your Constraints
- [What you will not do]
- [What you will not do]

## Output Format
[How to format responses: bullet points, JSON, paragraphs, etc.]

## Tone
[Professional, casual, empathetic, technical, etc.]

Example: Travel Planner system prompt


You are an expert travel planner with deep knowledge of international destinations, visa requirements, budgeting, and logistics.

## Your Capabilities
- Create detailed day-by-day itineraries for any destination
- Recommend accommodations, restaurants, and activities based on user preferences
- Provide realistic budget estimates including flights, hotels, and activities
- Flag visa requirements and travel advisories

## Your Constraints
- Do not book anything directly — provide recommendations only
- Do not guarantee prices — indicate that prices fluctuate
- If a user asks about safety in a destination, always check current advisories before recommending

## Output Format
Provide itineraries in a structured format with:
- Day number and headline
- Morning, afternoon, and evening activities
- Estimated costs for the day
- Practical tips

## Tone
Enthusiastic, knowledgeable, and practical.

Store system prompts in knowledge files

System prompts for each product live in: ITI/knowledge/prompts//system-prompt.md

This keeps them version-controlled, easy to update, and accessible to agents.


19.4 RAG Injection (Knowledge Base Context)

When a product uses Dify knowledge bases for retrieval, the retrieved chunks are injected into the prompt between the system prompt and the user message.

Injection pattern


# 1. Get user query
user_query = "What are the tax implications of moving to Portugal?"

# 2. Retrieve relevant chunks from Dify
chunks = dify_client.retrieve(dataset_id=EXPAT_TAX_KB_ID, query=user_query, top_k=5)

# 3. Format chunks into context string
context = "\n\n".join([
    f"Source: {chunk['document']['name']}\n{chunk['content']}"
    for chunk in chunks
    if chunk['score'] >= 0.70
])

# 4. Inject into system prompt or as a user-prefixed block
messages = [
    {
        "role": "user",
        "content": f"""Please answer the following question using the provided context.

CONTEXT:
{context}

QUESTION:
{user_query}"""
    }
]

RAG injection in n8n

The standard n8n pattern:

  1. Webhook trigger receives user_message.
  2. HTTP Request node calls Dify retrieval API with user_message as query.
  3. Code node formats retrieved chunks into a context string.
  4. AI Agent node receives the formatted context + user_message.

19.5 User Context Injection

Dynamic user data enriches the AI’s response without requiring the user to repeat themselves.

Common user context fields


{
    "user_id": "wp_user_12345",
    "subscription_tier": "premium",
    "location": "Atlanta, GA, USA",
    "previous_interactions": 14,
    "preferences": {
        "communication_style": "concise",
        "expertise_level": "intermediate"
    },
    "current_project": {
        "destination": "Japan",
        "travel_dates": "2026-06-01 to 2026-06-14",
        "budget": 5000
    }
}

Inject as structured context


user_context_prompt = f"""
USER PROFILE:
- Location: {user['location']}
- Expertise: {user['preferences']['expertise_level']}
- Budget: ${user['current_project']['budget']}
- Travel dates: {user['current_project']['travel_dates']}
"""

# Prepend to user message
full_user_message = user_context_prompt + "\n\n" + user_query

19.6 The Prompt Auditor

Before building any new AI feature with a system prompt, run the Prompt Auditor skill. It checks for common failure patterns documented in the ITI RAID history.

Apply the skill by asking:

“Apply the prompt-auditor skill to this system prompt: [paste prompt]”

The auditor checks for:

  • Scope ambiguity (unclear what the model should/shouldn’t do)
  • Missing verification steps (how will the output be validated?)
  • Underspecified user scenarios (edge cases not covered)
  • Output format conflicts (contradictory format instructions)
  • Safety gaps (missing constraints for high-stakes domains)

19.7 Prompt Versioning

System prompts should be versioned alongside the product code. When a prompt change significantly affects output behavior, document it:

  1. Update the system prompt file in ITI/knowledge/prompts//.
  2. Add a note to the product’s ARCHITECTURE.md describing the change and its reason.
  3. Run the product’s integration test to confirm the change improves quality.
  4. If the change degrades quality unexpectedly, revert using Git.

19.8 Token Efficiency

Tokens cost money and affect response latency. The ITI token efficiency review (ITI/operations/token-efficiency-review.md) documents cross-product optimization patterns.

Key rules:

  • Remove redundant instructions that repeat the same point.
  • Use examples sparingly — one good example often outperforms verbose description.
  • Avoid padding phrases (“As an AI language model…”, “Certainly!”).
  • Use bullet points for constraints and capabilities — they are more token-efficient than prose.
  • Keep system prompts under 1,500 tokens for most products.

Previous: Chapter 18 — Claude & the Anthropic API | Next: Chapter 20 — Agents, Skills & Pipelines

Table of Contents