Introduction
In Chapter 1, we learned the fundamentals of prompt engineering. This chapter covers basic techniques that form the foundation for all advanced prompting strategies. These techniques are essential building blocks that you'll use in virtually every interaction with LLMs.
By the end of this chapter, you will be able to:
- Use Role Prompting to set LLM behavior and expertise
- Provide effective context for complex tasks
- Specify output formats including Markdown, JSON, and structured data
- Use JSON Mode and Structured Output for reliable parsing
- Set appropriate constraints for quality control
- Decompose complex tasks into manageable sub-tasks
Role Prompting
Role Prompting (also called persona prompting) is the technique of assigning a specific role or persona to the LLM. This fundamentally changes how the model approaches problems and generates responses.
Why Role Prompting Works
When you assign a role, you're effectively:
- Activating relevant knowledge: The model draws on patterns associated with that expertise
- Setting behavioral expectations: Communication style, level of detail, and approach
- Establishing authority: The model responds with appropriate confidence and depth
Basic Role Prompting
Simple Role Assignment
You are a senior Python developer with 10 years of experience.
Review the following code and suggest improvements for readability and performance:
```python
def process_data(data):
result = []
for item in data:
if item > 0:
result.append(item * 2)
return result
```
Advanced Role Prompting
For more nuanced outputs, provide detailed role specifications:
Detailed Role Specification
You are Dr. Sarah Chen, a data scientist with the following background:
- PhD in Machine Learning from MIT
- 8 years of industry experience at leading tech companies
- Specialist in NLP and transformer architectures
- Known for clear explanations that bridge theory and practice
Communication style:
- Use analogies to explain complex concepts
- Provide practical code examples
- Acknowledge limitations and edge cases
- Balance technical accuracy with accessibility
Explain how attention mechanisms work in transformers.
Multi-Expert Role
For complex problems, you can invoke multiple perspectives:
Multi-Expert Panel
I want you to act as a panel of three experts reviewing a startup idea:
1. **Venture Capitalist**: Focus on market size, scalability, and investment potential
2. **Technical CTO**: Evaluate feasibility, technical challenges, and architecture
3. **Marketing Director**: Assess brand positioning, customer acquisition, and messaging
Startup idea: An AI-powered personal nutrition coach that creates meal plans based on continuous glucose monitoring data.
Each expert should provide their analysis, then synthesize a collective recommendation.
Role Prompting Best Practices
| Do | Don't |
|---|---|
| Be specific about expertise level | Use vague descriptions ("be smart") |
| Include relevant experience/background | Assign contradictory roles |
| Define communication style | Expect perfect role-playing for niche domains |
| Match role to task requirements | Over-specify irrelevant details |
Context Setting
Context Setting provides the LLM with background information necessary to understand and complete the task accurately. Without proper context, even well-structured prompts can produce irrelevant results.
Types of Context
1. Domain Context
Information about the field or subject area:
Domain Context Example
**Domain**: Enterprise SaaS B2B Sales
**Industry**: Healthcare technology
**Regulatory environment**: HIPAA compliance required
**Typical sales cycle**: 6-12 months
**Key stakeholders**: CTO, CISO, Procurement
Write a follow-up email after a product demo.
2. Situational Context
Information about the specific situation or scenario:
Situational Context Example
**Current situation**:
- We're a 50-person startup scaling to 200 employees
- Our monolithic Python application is becoming difficult to maintain
- We have 2 senior engineers and 4 junior developers
- Budget is limited; cloud costs are a concern
- We need to maintain 99.9% uptime for existing customers
Recommend a strategy for migrating to microservices.
3. Historical Context
Previous interactions, decisions, or outcomes:
Historical Context Example
**Previous decisions**:
- We chose PostgreSQL over MongoDB 2 years ago
- Our API uses REST; we considered GraphQL but decided against it
- Previous attempt to implement caching with Redis failed due to complexity
**Current challenge**: Database queries are becoming slow as data grows
Based on our history, what would you recommend?
The CRISP Context Framework
Use this framework to ensure comprehensive context:
CRISP Framework
- Constraints: Limitations, requirements, non-negotiables
- Resources: Available tools, budget, team, time
- Intent: Ultimate goal, why this matters
- Situation: Current state, recent events
- Preferences: Desired approach, style, values
CRISP in Practice
**Constraints**:
- Must complete within 2-week sprint
- Cannot break existing API contracts
- Must pass security review
**Resources**:
- 2 full-stack developers available
- AWS infrastructure with $500/month budget
- Existing CI/CD pipeline
**Intent**:
- Improve user onboarding completion rate from 40% to 70%
**Situation**:
- Current onboarding has 8 steps
- Users drop off primarily at email verification (Step 3)
- Mobile users have 50% lower completion than desktop
**Preferences**:
- Prefer progressive enhancement over complete redesign
- Want to A/B test changes
Design an improved onboarding flow.
Output Format Specification
Specifying output format ensures responses are structured exactly as needed. This is crucial for downstream processing, consistency, and usability.
Markdown Formatting
Markdown Output Specification
Analyze the following text and provide your response in Markdown format:
## Required Sections
- **Summary** (2-3 sentences)
- **Key Points** (bulleted list, 3-5 items)
- **Sentiment** (Positive/Negative/Neutral with explanation)
- **Recommendations** (numbered list)
Use `code formatting` for technical terms.
Use **bold** for emphasis on important points.
Text to analyze:
"Our Q3 customer satisfaction scores dropped 15% compared to Q2..."
JSON Output
JSON Format Specification
Extract product information from the following text and return as JSON:
Expected JSON structure:
```json
{
"product_name": "string",
"price": {
"amount": number,
"currency": "string"
},
"features": ["string"],
"availability": "in_stock" | "out_of_stock" | "pre_order",
"rating": {
"score": number,
"review_count": number
}
}
```
Text: "The new iPhone 15 Pro is available for $999. Key features include
A17 Pro chip, titanium design, and 48MP camera. Currently in stock.
Rated 4.7 stars based on 1,247 reviews."
Structured Output / JSON Mode (2025)
Modern LLMs (GPT-4, Claude, Gemini) support JSON Mode or Structured Output that guarantees valid JSON responses. This is essential for production applications.
API-Level JSON Mode
Most LLM APIs now support response format parameters:
- OpenAI:
response_format: { type: "json_object" } - Anthropic: Tool use with JSON schemas
- Google:
response_mime_type: "application/json"
OpenAI JSON Mode Example
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=[
{
"role": "system",
"content": "Extract entities and return as JSON with keys: persons, organizations, locations"
},
{
"role": "user",
"content": "Apple CEO Tim Cook announced new products at their Cupertino headquarters."
}
]
)
# Guaranteed valid JSON
import json
data = json.loads(response.choices[0].message.content)
print(data)
# {"persons": ["Tim Cook"], "organizations": ["Apple"], "locations": ["Cupertino"]}
Structured Output with Schemas (2025)
For even stricter validation, use JSON Schema definitions:
Structured Output with Schema
from openai import OpenAI
from pydantic import BaseModel
from typing import Literal
class ProductAnalysis(BaseModel):
product_name: str
sentiment: Literal["positive", "negative", "neutral"]
confidence: float
key_features: list[str]
recommendation: str
client = OpenAI()
response = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "Analyze product reviews"},
{"role": "user", "content": "This laptop is amazing! Fast processor, great screen, but battery could be better."}
],
response_format=ProductAnalysis
)
# Fully typed, validated response
analysis = response.choices[0].message.parsed
print(f"Sentiment: {analysis.sentiment}")
print(f"Confidence: {analysis.confidence}")
Tabular Output
Table Format Specification
Compare the following programming languages. Return as a Markdown table.
Languages: Python, JavaScript, Rust, Go
| Language | Typing | Performance | Learning Curve | Best Use Case |
|----------|--------|-------------|----------------|---------------|
| ... | ... | ... | ... | ... |
Include a brief summary paragraph after the table.
Constraint Setting
Constraints define boundaries and requirements for LLM outputs. Well-defined constraints prevent common issues like verbosity, irrelevant content, and inappropriate responses.
Types of Constraints
Length Constraints
Length Control
Summarize this article in exactly 3 sentences.
Each sentence should be 15-20 words.
Total summary: 45-60 words.
[Article text...]
Content Constraints
Content Requirements
Write a product description with these requirements:
**Must Include:**
- Product name mentioned at least twice
- Three specific features with benefits
- Call-to-action at the end
**Must Avoid:**
- Superlatives (best, greatest, amazing)
- Comparisons to competitors
- Unverifiable claims
- Technical jargon
**Tone:** Professional, confident, accessible
Behavioral Constraints
Behavioral Guidelines
You are a customer support assistant.
**Behavioral rules:**
1. Never admit fault on behalf of the company
2. Always offer to escalate unresolved issues
3. Use the customer's name at least once
4. End every response with a follow-up question
5. If asked about competitors, redirect to our strengths
**Response style:**
- Maximum 150 words
- Use empathetic language
- Provide specific next steps
Negative Constraints (What NOT to Do)
Sometimes it's more effective to specify what to avoid:
Negative Constraints
Explain machine learning to a high school student.
DO NOT:
- Use mathematical equations
- Reference academic papers
- Use terms like "gradient descent," "loss function," "backpropagation"
- Assume prior programming knowledge
- Use more than 200 words
Instead, use everyday analogies and simple examples.
Task Decomposition
Task Decomposition breaks complex tasks into smaller, manageable steps. This improves accuracy and allows for verification at each stage.
Sequential Decomposition
Step-by-Step Task
Complete the following task in three separate steps.
Complete each step fully before moving to the next.
**Task**: Create a marketing email for our new product launch
**Step 1: Research & Planning**
- Identify the target audience characteristics
- List 3 key pain points our product solves
- Define the primary call-to-action
**Step 2: Draft Creation**
- Write subject line (A/B test options)
- Write email body following AIDA framework
- Include social proof element
**Step 3: Review & Optimize**
- Check for spam trigger words
- Verify mobile readability
- Suggest personalization tokens
Please complete Step 1 now.
Parallel Decomposition
For tasks with independent components:
Parallel Sub-tasks
Analyze this business proposal from three independent perspectives.
Each analysis should be self-contained.
**Financial Analysis**
- ROI projection
- Risk assessment
- Cash flow implications
**Technical Analysis**
- Feasibility assessment
- Resource requirements
- Integration challenges
**Market Analysis**
- Competitive landscape
- Market timing
- Customer validation
Present each analysis separately with a confidence score (1-10).
Hierarchical Decomposition
Nested Task Structure
Create a project plan for building a mobile app.
**Level 1: Major Phases**
1. Discovery
2. Design
3. Development
4. Testing
5. Launch
**Level 2: Sub-tasks** (for each phase)
- List 3-5 specific tasks
- Identify dependencies
- Estimate effort (S/M/L)
**Level 3: Details** (for key sub-tasks)
- Define acceptance criteria
- List required resources
- Note potential blockers
Start with Level 1, then expand Phase 1 to Level 2 and Level 3.
Prompt Template Library
Universal Template
All-Purpose Prompt Template
# Role
You are [role/expertise].
# Context
[Background information relevant to the task]
# Task
[Clear description of what needs to be done]
# Input
[Data, text, or information to process]
# Output Requirements
- Format: [Markdown/JSON/Table/etc.]
- Length: [word count or structure]
- Style: [tone and voice]
# Constraints
- [Constraint 1]
- [Constraint 2]
# Examples (optional)
[Input example] -> [Output example]
Code Review Template
Code Review Prompt
# Role
You are a senior software engineer conducting a code review.
# Context
- Language: [Python/JavaScript/etc.]
- Project type: [Web app/API/Library]
- Team guidelines: [Link or summary]
# Task
Review the following code for:
1. **Correctness**: Logic errors, edge cases
2. **Performance**: Time/space complexity issues
3. **Security**: Potential vulnerabilities
4. **Maintainability**: Readability, documentation
5. **Best Practices**: Language/framework idioms
# Code
```[language]
[code to review]
```
# Output Format
For each issue found:
- **Location**: Line number(s)
- **Severity**: Critical/Major/Minor/Suggestion
- **Issue**: Description
- **Fix**: Recommended solution with code example
Content Creation Template
Content Generation Prompt
# Role
You are a content creator specializing in [domain].
# Context
- Platform: [Blog/LinkedIn/Twitter/etc.]
- Audience: [Demographics, expertise level]
- Brand voice: [Tone description]
# Task
Create [content type] about [topic].
# Requirements
- Length: [word/character count]
- Keywords: [SEO keywords to include]
- Structure: [Outline or sections]
- CTA: [Desired action]
# Examples of Successful Content
[Reference examples]
# Constraints
- Avoid: [Topics, words, or approaches to skip]
- Must include: [Required elements]
Exercises
Exercise 1: Role Prompting (Difficulty: Easy)
Task: Create a role prompt for explaining cryptocurrency to:
- A 10-year-old child
- A retiree considering investment
- A software developer interested in blockchain
Each explanation should use appropriate language and examples for the audience.
Exercise 2: Context Setting (Difficulty: Medium)
Task: Using the CRISP framework, write a prompt asking for recommendations on choosing a cloud provider for your startup.
Include realistic constraints, resources, and preferences.
Exercise 3: JSON Output (Difficulty: Medium)
Task: Design a prompt that extracts structured data from restaurant reviews.
Define the JSON schema including: restaurant name, cuisine type, ratings (food, service, ambiance), price range, and recommended dishes.
Exercise 4: Constraint Setting (Difficulty: Medium)
Task: Write a prompt for generating product descriptions for an e-commerce site.
Include at least 5 constraints covering length, tone, required elements, and forbidden content.
Exercise 5: Task Decomposition (Difficulty: Advanced)
Task: Create a hierarchical task decomposition prompt for planning a company hackathon.
Include at least 3 levels of detail and identify dependencies between tasks.
Chapter Summary
Key Points
- Role Prompting: Assign specific roles to activate relevant knowledge and set behavioral expectations
- Context Setting: Use the CRISP framework (Constraints, Resources, Intent, Situation, Preferences) for comprehensive context
- Output Format: Be explicit about expected format; use JSON Mode for programmatic consumption
- Structured Output: Modern LLMs support schema-based validation for guaranteed valid responses
- Constraints: Define both positive (must include) and negative (must avoid) constraints
- Task Decomposition: Break complex tasks into sequential, parallel, or hierarchical sub-tasks
Next Steps
In Chapter 3, we'll explore advanced techniques including:
- Tree of Thought prompting for complex reasoning
- Self-Consistency for improved accuracy
- ReAct pattern for reasoning with actions
- Prompting for o1/o3 reasoning models
- Multi-turn conversation design
References
- OpenAI. (2024). Structured Outputs Documentation
- Anthropic. (2025). Claude Prompt Engineering Guide
- Wei, J., et al. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- White, J., et al. (2023). A Prompt Pattern Catalog to Enhance Prompt Engineering with ChatGPT
Update History
- 2026-01-12: v2.0 Initial release with Structured Output and JSON Mode