Prompt Engineering: Techniques, Formats, and Best Practices

Master Prompt Engineering. Learn about Zero-shot vs. Few-shot prompting, Chain of Thought (CoT), System Prompts, and structural formatting.

Introduction

In classical software development, we write instructions using code compilation languages (Java, Python, C++). In the era of Large Language Models, the programming language is simple human English.

Prompt Engineering is the practice of structuring text inputs to an LLM to guide its output toward a desired behavior, format, or tone. Because LLMs are probabilistic prediction networks, minor changes in how a question is formatted can be the difference between a correct calculation and a completely hallucinated response.

What You Will Learn

  • The structure of a professional prompt.
  • The differences between Zero-Shot, One-Shot, and Few-Shot learning.
  • How Chain of Thought (CoT) forces logical reasoning.
  • The role of System Prompts in setting behavioral constraints.
  • How to structure prompts for machine-readable outputs (JSON/YAML).

Why This Topic Matters

As developers, you will connect LLMs to software applications via APIs. If your app expects a structured JSON output but the LLM randomly returns a friendly conversational paragraph, your parser will crash. Prompt engineering is the primary tool to enforce output consistency, limit hallucinations, and implement custom business rules.

Prerequisites

Detailed Explanation

A prompt is not just a question; it is a compilation of inputs designed to shape predictions.


The Anatomy of a Prompt

A production-grade prompt consists of four distinct components:

| Component | Description | Example | | :--- | :--- | :--- | | System Instructions| Establishes the behavior, boundaries, and persona. | "You are a database administrator. Return only raw SQL queries." | | Context | Background information the model needs to know. | "Here is the database schema: Users(id, name, email)" | | Task / Input Data | The specific action you want the model to perform. | "Create a query to find users with gmail addresses." | | Output Formatting | Details on how the final output must look. | "Format the SQL inside a markdown code block." |


Few-Shot Prompting

LLMs are excellent at pattern matching. If you want a model to perform a complex task, instead of just describing it, provide examples (shots).

  • Zero-Shot Prompting: Ask the model to perform a task with no examples:
    • Prompt: "Classify this review: 'The charger broke in 2 days.'"
  • Few-Shot Prompting: Provide several examples of inputs and desired outputs first:
    • Prompt:
      Review: "The screen is beautiful." -> Sentiment: Positive
      Review: "Delivery took three weeks." -> Sentiment: Negative
      Review: "The charger broke in 2 days." -> Sentiment:
      

Chain of Thought (CoT) Prompting

If you ask an LLM a complex math or logic question, it might guess the wrong answer immediately because it tries to generate tokens in a single forward pass. Chain of Thought forces the model to generate its reasoning steps before stating the final answer.

  • The Trick: Simply append: "Let's think step by step" to your prompt.
  • Why it works: This forces the model to write out its calculations, creating a computational scratchpad where intermediate tokens guide the prediction of the final result.
graph TD
    A[Complex Query] --> B{Standard Prompting}
    B -->|Immediate Prediction| C[Incorrect Guess / Hallucination]
    A --> D{Chain of Thought}
    D -->|Step-by-step reasoning| E[Intermediate Calculations]
    E -->|Final Token Prediction| F[Correct Answer]

Enforcing Structured Outputs (JSON)

To connect an LLM to an API parser, you must use system formatting.

  1. Assign Persona: Tell the model it is a JSON API.
  2. Provide Schema: Show it the exact JSON keys and structures.
  3. Suppress Conversation: Instruct it to write only JSON, without greetings like "Here is your JSON".

Python Code Examples

We will write a python script showing how to query an LLM (using pseudo API structures) with system, context, and formatting variables.

# Simulating a Prompt Construction Class in Python
class PromptTemplate:
    def __init__(self, system_prompt, user_template):
        self.system_prompt = system_prompt
        self.user_template = user_template

    def format(self, **kwargs):
        user_content = self.user_template.format(**kwargs)
        
        # Structure the payload for API calls (matching OpenAI/Llama API schemas)
        payload = [
            {"role": "system", "content": self.system_prompt},
            {"role": "user", "content": user_content}
        ]
        return payload

# Configure a structured text summarizer prompt
system_config = "You are a summarizing bot. Output ONLY valid JSON matching this schema: {'summary': 'text', 'word_count': int}."
user_config = "Summarize this article in 1 sentence:\n'{article}'"

template = PromptTemplate(system_config, user_config)

# Format for a specific article input
article_input = "Artificial Intelligence is transforming recruiting by scanning resumes and matching candidates to profiles."
formatted_payload = template.format(article=article_input)

print("System Message:")
print(formatted_payload[0]['content'])
print("\nUser Message:")
print(formatted_payload[1]['content'])

Industry Use Cases

  • Automated Customer Service Routing: Prompting LLMs to extract ticket sentiments and classify them into predefined departments.
  • Named Entity Extraction: Extracting details (dates, names, values) from unstructured emails to populate CRM databases.
  • Software Code Translation: Prompting models to rewrite legacy Cobol code into modern Python, using few-shot formatting patterns.

Summary

Prompt Engineering is the programming paradigm of foundation models. By structuring inputs to contain system parameters, context details, few-shot examples, and logical Chain of Thought commands, developers can guide LLM responses, prevent hallucinations, and extract structured JSON schemas for software integrations.

Next Topic

LLMs are locked to the knowledge they acquired during pre-training. How do we feed them real-time, proprietary company documents to answer questions accurately? Let's check: Retrieval-Augmented Generation (RAG): Architecture and Vector DBs.