Prompt Engineering

Prompt engineering is the practice of designing and optimizing inputs (prompts) to effectively communicate with Large Language Models (LLMs) and elicit desired outputs. It encompasses techniques ranging from simple instruction formatting to complex reasoning strategies.12


Prompting Fundamentals

A well-structured prompt typically contains four components:

ComponentDescription
InstructionClear specification of the task guiding the model’s response
ContextBackground information relevant to the domain or task
Input DataThe specific data to be processed
Output FormatThe desired structure and format of the response

Example:

Classify the text into neutral, negative, or positive.
Text: I think the food was okay.
Sentiment:

Basic Techniques

Zero-shot Prompting

Direct prompting without examples. The model relies entirely on its pre-trained knowledge to complete the task.

Extract keywords from the below text.
Text: {text}
Keywords:

Few-shot Prompting

Providing examples to guide the model’s output format and reasoning pattern. This technique leverages in-context learning.3

Extract keywords from the corresponding texts below.

Text 1: Stripe provides APIs that web developers can use to integrate 
payment processing into their websites and mobile applications.
Keywords 1: Stripe, payment processing, APIs, web developers, websites, 
mobile applications

Text 2: OpenAI has trained cutting-edge language models that are very 
good at understanding and generating text.
Keywords 2: OpenAI, language models, text processing, API

Text 3: {text}
Keywords 3:

Best Practices2

  1. Use the Latest Model: More recent models typically offer improved instruction following.
  2. Structure Instructions: Place instructions at the beginning and use delimiters (###, “”") to separate instruction from context.
  3. Be Specific and Descriptive: Clearly articulate the desired context, outcome, length, format, and style.
  4. Specify Output Format with Examples: Demonstrate the expected output structure through examples.
  5. Start with Zero-shot: Begin simple, then add examples (few-shot) if needed, and consider fine-tuning as a last resort.
  6. Avoid Vague Descriptions: Use precise language rather than fluffy or ambiguous terms.
  7. Provide Positive Guidance: State what the model should do rather than what it should not do.
  8. Use Zero-shot, Few-shot, and Fine-tune Approach: Begin with a zero-shot approach, followed by a few-shot approach (providing examples). If neither works, consider fine-tuning the model.
  9. Avoid Fluffy Descriptions: Reduce vague and imprecise descriptions. Instead, use clear instructions and avoid unnecessary verbosity.
  10. Provide Positive Guidance: Instead of stating what not to do, clearly state what actions should be taken in a given situation, offering positive guidance.
  11. Code Generation Specific - Use “Leading Words”: When generating code, utilize “leading words” to guide the model toward a specific pattern or language, improving the accuracy of code generation.

Advanced Prompting Techniques

Step-by-Step Modular Decomposition

These are the methods that break down problems into smaller, manageable steps giving LLMs a guidance via sequence of intermediate steps.

Chain-of-Through (CoT) Prompting

The LLM is guided to reason through each example step by step, providing intermediate reasoning before arriving at the final answer. The output shows that the model successfully solves the problem by considering the odd numbers and their sums.

**Model Input**
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. Howmany tennis balls does he have now?
A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.

Q: The cafeteria had 23 applies. If they used 20 to make lunch and bought 6 more, how many apples do they have?

**Model Output**
A: The cafeteria had 23 apples originally. They used 20 to make lunch. So they had 23 - 20 = 3. They bought 6 more apples, so they have 3 + 6 = 9. The answer is 9.

Zero-shot/Few-shot CoT Prompting

Few-shot CoT
**Model Input**
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. Howmany tennis balls does he have now?
A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.

Q: A juggler can juggle 16 balls. Half of the balls are fold balls, and half of the golf balls are blue. How many blue golf balls are there?
A:

**Model Output**
(Output) The juggler can juggle 16 balls. Half of the balls are golf balls. So there are 16 / 2 = 8 golf balls. Half of the golf balls are blue. So thre are 8 / 2 = 4 blue golf balls. The answer is 4
Zero-shot CoT
Q: A juggler can juggle 16 balls. Half of the balls are fold balls, and half of the golf balls are blue. How many blue golf balls are there?
A: **Let's think step by step.**

**Model Output**
(Output) There are 16 balls in total. Half of the balls are golf balls. That means that there are 8 golf balls. Half of the golf balls are blue. That means that there are 4 blue golf balls.

Automatic Chain-of-Thought (Auto-CoT) 4

Auto-CoT was designed to automate the generation of reasoning chains for demonstrations. Instead of manually crafting examples, Auto-CoT leverages LLMs with a “Let’s think step by step” prompt to automatically generate reasoning chains one by one.

Auto-CoT process involves two main stages:

  1. Questioning Clustering: Partition questions into clusters based on similarity.
  2. Demonstration Sampling: Select a representative question from each cluster and generate its reasoning chain using Zero-shot-CoT with simple heuristics.

The goal here is to eliminate manual work in creating examples as well as ensuring diversity in demonstrations, the heuristic approach ensures that the model generates simple yet accurate reasoning claims.

Tree-of-Thoughts (ToT) Prompting 5

This is a technique that extends CoT. ToT allows 1for a deliberate decision making and consider multiple reasoning paths while serving self-evaluating choices, model can also backtrack as needed during the reasoning process.

Key Characteristics
  • Coherent Units (“Thoughts”): ToT prompts LLMs to consider coherent units of text as intermediate reasoning steps.
  • Deliberate Decision-Making: Enables models to make decisions intentionally and evaluate different reasoning paths.
  • Backtracking and Looking Ahead: Allows models to backtrack or look ahead during the reasoning process, providing flexibility in problem-solving.

https://github.com/aishwaryanr/awesome-generative-ai-resources/raw/main/free_courses/Applied_LLMs_Mastery_2024/img/prompting_4.png

Graph of Thought Prompting 6

Graph of Thought (GoT) prompting is an advanced prompting technique for LLMs where reasoning steps are structured as a graph rather than a linear chain. Each node represents an intermediate idea or partial solution, and edges represent logical or contextual relationships. This allows the model to explore multiple reasoning paths in parallel, merge insights, and reach more robust, explainable conclusions — improving complex reasoning, planning, and problem-solving compared to standard chain-of-thought methods.

Comprehensive Reasoning and Verification

Automatic Prompt Engineering (APE) 7

This is a technique that treats instructions as programmable elements and seeks to optimize them by conducting a search across a pool of prompt candidates proposed by an LLM. To simply put, this is a technique in which the LLM creates the most aligning prompt for a given task.

Chain of Verification (CoVe) 8

CoVe is a prompt verification technique designed to reduce hallucinations in LLMs. CoVe uses an initial draft response to a user query, potentially containing inaccuracies, then tries to independently verify by questioning itself, fact-checking the initial response without bias and create a final response while correcting and improving via the verification process.

Self Consistency 9

The idea behind Self Consistency is to generate multiple diverse responses by having multiple reasoning paths each evaluated individually and to output the most consistent answer between them.

ReAct (Reasoning and Acting)10

ReAct interleaves reasoning traces with actions, allowing LLMs to generate verbal reasoning steps while also taking actions (like searching or computing) to gather information. This creates a synergy between reasoning and acting for more grounded responses.


Tool-Augmented Prompting11

These methods enhance LLM capabilities by integrating external tools, knowledge bases, or aggregation techniques:

  • Active Prompting: Aggregates multiple reasoning paths to select the most consistent answer
  • ART (Automatic Reasoning and Tool-use): Automatically selects and uses appropriate tools based on the task

Security Considerations

Prompt engineering introduces several security risks that practitioners must address:

RiskDescriptionMitigation
Prompt InjectionMalicious content injected to override instructionsInput sanitization, instruction isolation
Prompt LeakingExtraction of system prompts or sensitive informationAvoid including secrets in prompts
JailbreakingBypassing safety and moderation featuresLayered defenses, output filtering
Bias AmplificationPrompts that elicit or reinforce biasesCareful prompt design, bias testing

Technique Selection Guide

ScenarioRecommended Technique
Simple, well-defined tasksZero-shot
Format-specific outputsFew-shot with examples
Mathematical reasoningChain-of-Thought
Complex multi-step problemsTree-of-Thoughts
Fact-checking requiredChain-of-Verification
Tool integration neededReAct

  • Transformers - Architecture underlying modern LLMs
  • RAG Systems - Combining retrieval with generation
  • Fine-Tuning - When prompting is insufficient

References