Prompt engineering is like mixing a perfect cocktail—get the right ingredients in the right amounts, and voilà , you get an amazing result! Want your AI model to serve top-tier answers? Let’s shake things up with these effective, easy-to-digest techniques.
🍸 What’s Prompt Engineering Anyway?
In a nutshell, prompt engineering is crafting specific instructions to get spot-on responses from large language models (LLMs). The more crystal-clear your prompt, the better the output.
Boring Prompt:
"Summarize prompt engineering."
Cool Prompt:
"Give a 100-word summary of prompt engineering for non-techies."
đź’ˇ Result: You now have a concise, user-friendly explanation. Nice!
🎯 Key Ingredients for a Perfect Prompt:
1. Context = Clarity!
Think of context as giving your AI a head start. It narrows down the scope.
Example:
Want to modify a C# class? Instead of tossing vague requests, serve some code along.
public class User {
public int UserId { get; set; }
public string Name { get; set; }
}
Prompt:
"Make UserId
and Name
read-only, set them via the constructor."
Output:
public User(int userId, string name) {
UserId = userId;
Name = name;
}
2. Be Super-Specific
General prompts = meh responses. Specific prompts = magic.
Ask for exactly what you want!
Bad: "Create a user class."
Good: "Create a C# user class with fields UserId
, Name
, and Email
. Make UserId
read-only."
3. Guide the Output
Want the AI to produce data or follow a particular style? Show a sample!
Example:
"Generate 3 users with fields UserId
, Name
, and Email
."
Output:
var user1 = new User(1, "Alice", "alice@example.com");
var user2 = new User(2, "Bob", "bob@example.com");
You can try all of these prompts directly with your LLM of choice for free and see how each LLM differs in its outputs using PiecesOS.
🛠️ Techniques to Boost Your Prompt Game:
1. Zero-shot Prompting 🕶️ – No Clues, Just a Direct Ask
Think of this as asking your AI assistant to solve a problem without giving it any prior examples or context—it’s going in “cold.” Zero-shot prompting works well for straightforward tasks where the AI can infer what you want based on its training data.
Example:
"Create a unit test for the User
class using xUnit in C#."
Output:
public class UserTests {
[Fact]
public void UserConstructor_SetsProperties() {
var user = new User(1, "John Doe", "john@example.com", "555-555-5555");
Assert.Equal(1, user.UserId);
Assert.Equal("John Doe", user.Name);
Assert.Equal("john@example.com", user.Email);
Assert.Equal("555-555-5555", user.PhoneNumber);
}
}
đź’ˇ When to use:
Zero-shot prompting works best for tasks where the AI can easily guess your needs, such as generating boilerplate code, creating summaries, or performing simple tasks.
2. Few-shot Prompting 🎯 – Show, Don’t Just Tell
Few-shot prompting is like teaching by example—you provide a few instances of what you want, and the AI picks up the pattern. This is particularly useful when the output requires a specific structure or format.
Example:
"Here are two instances of a User
class. Now generate two more."
Prompt:
// Example instances provided by you
var user1 = new User(1, "Alice", "alice@example.com", "555-555-5555");
var user2 = new User(2, "Bob", "bob@example.com", "555-555-5556");
// Task: Generate two more instances
Output:
var user3 = new User(3, "Charlie", "charlie@example.com", "555-555-5557");
var user4 = new User(4, "Diana", "diana@example.com", "555-555-5558");
đź’ˇ When to use:
Few-shot prompting is ideal when:
- The output needs to follow a specific format.
- You want consistent style across different responses.
- You're working with data that has structured patterns.
Pro Tip: Keep your examples short and clear. Too many examples can overwhelm the AI, while too few may not establish the pattern clearly.
3. Prompt Chaining 🔗 – Divide and Conquer
Prompt chaining is like breaking down a big problem into smaller tasks, solving each one step-by-step. This technique is particularly helpful for complex problems or multi-step workflows.
How it works:
- Start simple: Begin with a basic prompt and get an initial response.
- Iterate: Use follow-up prompts to refine the output or guide the model towards a more complex solution.
Example:
Goal: Create a fully-featured User
class in C# with private properties and a constructor.
- Step 1: Start simple.
Prompt:
"Create a basic User
class in Python."
Output:
class User:
def __init__(self, username, email):
self.username = username
self.email = email
- Step 2: Refine the output.
Prompt:
"Convert this Python class to C#."
Output:
public class User {
public string Username { get; set; }
public string Email { get; set; }
}
-
Step 3: Add additional requirements.
Prompt:
"Make
Username
andEmail
read-only, and add aCreatedAt
property initialized in the constructor." Output:
public class User {
public string Username { get; }
public string Email { get; }
public DateTime CreatedAt { get; }
public User(string username, string email) {
Username = username;
Email = email;
CreatedAt = DateTime.Now;
}
}
đź’ˇ When to use:
Use prompt chaining when:
- The task is too large to handle in a single prompt.
- You want to progressively refine the output.
- You’re iterating on a solution by adding new requirements at each step.
4. Chain-of-Thought Prompting 🧠– Help AI Think Like a Developer
This technique involves explicitly guiding the AI through the steps it should take to solve a problem. It’s like walking the AI through your thought process, ensuring it doesn’t skip any important details.
Example:
"Write unit tests for a User
class, considering key scenarios: constructor validation, edge cases, and valid phone numbers."
Prompt:
"Create unit tests for the following class.
Think step-by-step:
- Identify key scenarios to test.
- Write unit tests using xUnit.
- Consider edge cases."
Output:
public class UserTests {
[Fact]
public void Constructor_ShouldInitializeProperties() {
var user = new User(1, "John Doe", "john@example.com");
Assert.Equal(1, user.UserId);
Assert.Equal("John Doe", user.Name);
Assert.Equal("john@example.com", user.Email);
}
[Theory]
[InlineData("555-555-5555")]
[InlineData("123-456-7890")]
public void ShouldAccept_ValidPhoneNumbers(string phoneNumber) {
var user = new User(2, "Jane Doe", phoneNumber);
Assert.Equal(phoneNumber, user.PhoneNumber);
}
[Fact]
public void ShouldThrowException_WhenPhoneNumberIsNull() {
Assert.Throws<ArgumentNullException>(() => new User(3, "Invalid User", null));
}
}
đź’ˇ When to use:
Chain-of-thought prompting is perfect when:
- The task requires logical, multi-step reasoning.
- You need the AI to think critically and not skip key details.
- You're working on tasks that benefit from explicit step-by-step guidance (e.g., writing complex code or solving mathematical problems).
You can try all of these prompts directly with your LLM of choice for free and see how each LLM differs in its outputs using PiecesOS.
The article was originally written by Jim, head of Devrel at Pieces for Developers. You can find more examples and nuances in this article https://pieces.app/blog/llm-prompt-engineering
Top comments (0)