Prompt engineering is less about crafting the perfect sentence and more about understanding the underlying statistical patterns a language model has learned.

Let’s see this in action. Imagine we want to extract specific information from a block of text.

Here’s our raw text:

Customer ID: 12345
Order Date: 2023-10-27
Item: Widget Pro
Quantity: 2
Price: $59.99
Shipping Address: 123 Main St, Anytown, CA 90210

A naive prompt might be: "Extract the customer ID."

The model might respond: "The customer ID is 12345."

This works, but it’s brittle. What if the text format changes slightly? What if we want multiple pieces of information?

A more robust approach involves structuring the prompt to guide the model’s extraction process. We can use a "few-shot" learning approach, providing examples of what we want.

Consider this prompt:

Extract the following information from the text:
Customer Name:
Order Date:
Total Items:

Text:
Customer ID: 67890
Order Date: 2023-10-26
Item: Gizmo
Quantity: 1
Price: $19.99
Item: Widget
Quantity: 3
Price: $29.99
Shipping Address: 456 Oak Ave, Otherville, NY 10001

Extracted Info:
Customer Name: (Not available)
Order Date: 2023-10-26
Total Items: 4

Text:
Customer ID: 12345
Order Date: 2023-10-27
Item: Widget Pro
Quantity: 2
Price: $59.99
Shipping Address: 123 Main St, Anytown, CA 90210

Extracted Info:
Customer Name: (Not available)
Order Date: 2023-10-27
Total Items: 2

Notice how we’ve defined the output structure and provided two examples. The model learns from these examples to:

  1. Identify relevant fields: It sees "Order Date" and "Quantity" are important.
  2. Handle missing information: It learns to output "(Not available)" when a field isn’t present.
  3. Perform calculations: Crucially, it infers that "Total Items" requires summing up all "Quantity" values.

The core problem this solves is bridging the gap between human intent and the model’s statistical understanding of language. Models don’t "understand" in the human sense; they predict the next most probable token based on their training data. Prompt engineering is the art of shaping that probability distribution to yield desired outputs.

Internally, when you provide a prompt, the model tokenizes it and processes it through its transformer layers. Each layer refines the representation of the input, attending to different parts of the text. For few-shot prompts, the model identifies the pattern across the examples and applies it to the new input. The "magic" happens in the attention mechanisms and the learned weights that allow it to generalize from the examples.

The levers you control are:

  • Instruction clarity: Directly telling the model what to do.
  • Output format: Specifying the desired structure (JSON, bullet points, plain text).
  • Few-shot examples: Demonstrating the task with input/output pairs.
  • Contextual information: Providing relevant background data.
  • Negative constraints: Explicitly stating what not to do.

A key aspect often overlooked is the impact of tokenization on complex instructions. If your prompt uses jargon or technical terms that are split into multiple tokens by the model’s tokenizer (e.g., "hyperparameter" might become "hyper" and "##parameter"), the model might struggle to grasp the unified concept as effectively as it would if the term were a single token. This can lead to subtle inaccuracies where the model seems to understand the words but misses the nuance of the combined term, requiring more explicit or rephrased instructions to compensate.

The next logical step is exploring how to dynamically construct prompts based on user input and model capabilities.

Want structured learning?

Take the full Prompt-engineering course →