Prompting - An Art to Master In The Modern Era


Understanding how to effectively prompt has emerged as one of the fundamental skills for developers aspiring to create AI applications and agents. In this article, we will embark on an in-depth exploration of several crucial prompting techniques. These techniques are essential for harnessing the full potential of AI and ensuring that your applications perform optimally. By mastering these methods, developers can significantly enhance their ability to interact with AI systems, leading to more innovative and intelligent solutions.
Zero Shot Prompting
It is a technique in Natural Language Processing (NLP) where a model generates a response to a task without any prior training examples or specific fine-tuning for that task. For example :
from google import genai
from google.genai import types
client = genai.Client(api_key='')
response = client.models.generate_content(
model='gemini-2.0-flash-001', contents='Why is the sky blue?'
)
print(response.text)
In the example above, it's a simple prompt or query to the LLM without providing many details or instructions for the response.
Few Shot Prompting
It is a technique that uses a small number of examples within a prompt to guide a language model's response. For example:
from google import genai
from google.genai import types
client = genai.Client(api_key='')
system_prompt = """
Generate a rhyming couplet about a tree:
Input: "cat"
Output: The curious cat, so sleek and fat,
Curled up cozy on the welcome mat.
Input: "sky"
Output: Look up high into the endless sky,
Where birds and clouds go drifting by.
"""
response = client.models.generate_content(
model='gemini-2.0-flash-001',
config=types.GenerateContentConfig(
system_instruction=system_prompt),
contents='Generate a rhyming couplet about a river'
)
print(response.text)
So in the above, we give the llm some examples about how the response should be when there is an input. The working for it is as given below:
Pattern recognition: The model analyzes the provided examples, identifying patterns in how inputs are transformed into outputs.
Task inference: From these patterns, the model infers the nature of the task it's being asked to perform.
Generalization: The model then attempts to generalize from the given examples to new, unseen inputs.
Application: Finally, the model applies this learned pattern to the new input provided at the end of the prompt.
It is advantageous in many ways, such as improved performance and reduced data requirements.
Chain of thought (CoT) Prompting
It is a technique in prompt engineering that guides a language model to break down complex tasks into a series of logical steps, mimicking human reasoning. It becomes very useful when there are lot of steps involved in order to resolve the query, for example :
import json
import os
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(
api_key=API_KEY
)
system_prompt = """
You are an AI assistant who is expert in breaking down complex problems and then resolve the user query.
For the given user input, analyse the input and break down the problem step by step.
Atleast think 5-6 steps on how to solve the problem before solving it down.
The steps are you get a user input, you analyse, you think, you again think for several times and then return an output with explanation and then finally you validate the output as well before giving final result.
Follow the steps in sequence that is "analyse", "think", "output", "validate" and finally "result".
Rules:
1. Follow the strict JSON output as per Output schema.
2. Always perform one step at a time and wait for next input
3. Carefully analyse the user query
Output Format:
{{ step: "string", content: "string" }}
Example:
Input: What is 2 + 2.
Output: {{ step: "analyse", content: "Alright! The user is intersted in maths query and he is asking a basic arthermatic operation" }}
Output: {{ step: "think", content: "To perform the addition i must go from left to right and add all the operands" }}
Output: {{ step: "output", content: "4" }}
Output: {{ step: "validate", content: "seems like 4 is correct ans for 2 + 2" }}
Output: {{ step: "result", content: "2 + 2 = 4 and that is calculated by adding all numbers" }}
"""
messages = [
{ "role": "system", "content": system_prompt },
]
query = "What is love?"
messages.append({ "role": "user", "content": query })
while True:
response = client.chat.completions.create(
model="gpt-4o",
response_format={"type": "json_object"},
messages=messages
)
if not response:
print("No response found !")
continue
print(response.choices[0].message.content)
parsed_response = json.loads(response.choices[0].message.content)
messages.append({ "role": "assistant", "content": json.dumps(parsed_response) })
if parsed_response.get("step") != "output":
print(f"🧠: {parsed_response.get("content")}")
continue
print(f"🤖: {parsed_response.get("content")}")
break
In the above example, you can observe that we want the LLM to perform the set of steps on every query it goes through each step and understanding the reasoning behind the model’s output. It involves generating a series of sub problems or sub steps that lead us to the answer.
Self Consistency Prompting
It is a prompt engineering method that enhances the reasoning capabilities of Large Language Models (LLMs) by generating multiple outputs and selecting the most consistent answer among them. For example:
import openai
import os
from collections import Counter
# Set your API key
openai.api_key = "your-api-key"
def get_multiple_answers(prompt, n=5, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
responses = []
for _ in range(n):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=1.0 # randomness to encourage diverse outputs
)
content = response['choices'][0]['message']['content'].strip()
responses.append(content)
return responses
def majority_vote(responses):
cleaned = [resp.split("\n")[0] for resp in responses] # Simplify comparison
most_common = Counter(cleaned).most_common(1)
return most_common[0] if most_common else ("No consensus", 0)
# Example prompt
prompt = "A train travels 60 miles per hour for 2 hours, then 40 miles per hour for 1 hour. What is the average speed of the entire trip?"
# Get multiple answers
answers = get_multiple_answers(prompt, n=7)
# Print responses
for i, ans in enumerate(answers, 1):
print(f"Answer {i}: {ans}")
# Aggregate via majority vote
result, count = majority_vote(answers)
print(f"\n✅ Most common answer: '{result}' (appeared {count} times)")
Answer 1: The average speed is 53.33 mph. Answer 2: The average speed is 53.33 mph. Answer 3: The average speed is 53.33 mph. Answer 4: The average speed is 50 mph. ... ✅ Most common answer: 'The average speed is 53.33 mph.' (appeared 4 times)
So it generates multiple output for the same prompt and then aggregates them and improves the reliability of the output. It is specifically very helpful in the areas of math, code and large complex problems.
Instruction Prompting
It involves providing a language model with explicit instructions on how to perform a task, instead of relying solely on context or examples. This approach helps the model understand the user's intentions and improves its ability to follow specific guidelines, potentially enabling it to handle complex tasks without extensive fine-tuning or labeled datasets. For example:
import openai
openai.api_key = "your-api-key"
prompt = """Convert the following paragraph into bullet points:
Artificial Intelligence (AI) is a branch of computer science that aims to create intelligent machines. It has become an essential part of the technology industry. Research in AI is highly technical and specialized, and the core problems of AI include programming computers for certain traits such as knowledge, reasoning, problem-solving, perception, learning, and planning.
"""
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # or "gpt-4"
messages=[
{"role": "user", "content": prompt}
],
temperature=0.7
)
print(response['choices'][0]['message']['content'].strip())
In the above example, we clearly state the llm to convert the para into series of bullet points meaning we are clearly instructing it to follow it.
Direct Answer Prompting
They are simple, clear instructions that you send to an AI text generator to get the content you want. For example:
import openai
openai.api_key = "your-api-key"
prompt = "What is the boiling point of water in Celsius?"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
print(response['choices'][0]['message']['content'].strip())
In the above example, we simply ask it to convert the temperature to Celsius. This means we are asking straightforward questions to get direct, clear answers without any reasoning, explanation, or extra formatting.
Persona Based Prompting
Instructing an AI model to take on a specific role or character can shape its responses. It acts as if someone else is addressing the query. This is particularly useful in scenarios like a food delivery chatbot, where we want to mimic the speaking style of customer service. For example:
import openai
openai.api_key = "your-api-key"
messages = [
{"role": "system", "content": "You are a friendly and helpful customer service agent for a food delivery app called QuickEats. Always be polite, empathetic, and efficient in resolving customer issues."},
{"role": "user", "content": "Hi, my order arrived late and the food was cold. What can you do about it?"}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0.7
)
print(response['choices'][0]['message']['content'].strip())
In the above example, we want the model to act as a friendly customer care service team and respond to a query.
Contextual Prompting
It is a technique in prompt engineering that involves providing the AI model with background information or context within the prompt to guide its response. This is one of most widely used prompting techniques where ai is given background information or the context in which we are talking about. It helps the model to understand the scenario and generates a relevant response for it. For example:
import openai
openai.api_key = "your-api-key"
messages = [
# System message sets up persona
{"role": "system", "content": "You are a professional and empathetic customer support agent for ShopVerse, an e-commerce platform."},
# Context: Previous customer complaint
{"role": "user", "content": "Hi, I was told I’d receive my refund for a returned jacket last week, but I haven’t seen it yet."},
{"role": "assistant", "content": "I’m really sorry about the delay. I’ve escalated the issue, and you should receive your refund within 3-5 business days."},
# New follow-up message from user
{"role": "user", "content": "Hey, it’s been 6 days now and I still don’t see the refund. What’s going on?"}
]
response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages,
temperature=0.7
)
print(response['choices'][0]['message']['content'].strip())
In the above example it is about a user who has contacted customer support multiple times about a delayed refund. We want the AI to remember that and respond accordingly. It is very helful in
Multi Modal Prompting
It involves providing AI models with a combination of different input formats, such as text, images, audio, or video, to enhance the richness and relevance of the AI-generated output. Chatgpt is an example for it.
Phew… Lot of prompting techniques and knowing them gives you the superpower to interact with the models and build your ai agents…
Subscribe to my newsletter
Read articles from Krish Kalaria directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
