Prompt Engineering

5. Prompt Engineering#

In an information system, a prompt is code: it determines what the model does and what shape its output takes. This chapter covers designing clear prompts, getting structured output that other software can rely on, and managing prompts as versioned, tested assets.

  • Prompt Design: Clear instructions, examples, and roles make model behavior more predictable.

  • Structured Output: JSON output checked against a schema lets other programs consume model results safely.

  • Prompt Management: Templates, version control, and tests treat prompts like the code they are.

Chapter Video

This tutorial shows how clearer instructions, context, and examples change what a model returns, the habits behind treating a prompt as code.

Prompt engineering essentials: Getting better results from LLMs | Tutorial (9:02) by GitHub

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Write prompts that combine instructions, context, examples, and roles.

  2. Request JSON output from a model and define the schema it must follow.

  3. Validate model output against a schema and handle invalid responses.

  4. Manage prompts as versioned templates with test cases.

  5. Extract structured fields from emails and validate them in Python.

Chapter Flow

Glossary

#

Term

Explanation

1

Prompt

The input text and instructions sent to a language model.

2

System prompt

Standing instructions that set a model’s role and rules for a conversation.

3

Zero-shot prompting

Asking a model to do a task without giving examples.

4

Few-shot prompting

Including worked examples in the prompt to guide the model.

5

Role

The label on each message, such as system, user, or assistant.

6

Structured output

Model output in a machine-readable format such as JSON.

7

JavaScript Object Notation (JSON)

A text format for structured data made of objects, arrays, strings, and numbers.

8

JSON Schema

A standard way to describe the fields and types a JSON document must have.

9

Output validation

Checking that model output matches the expected format and values.

10

Prompt template

A reusable prompt with placeholders that code fills in.

11

Prompt versioning

Tracking prompt changes in version control like source code.

12

Prompt injection

Input crafted to override a prompt’s instructions.

13

Output contract

The structure and meaning a consumer expects.

14

Delimiter

A marker separating task data from surrounding instructions.

15

Semantic validation

Checking values against evidence or business meaning.

16

Repair request

A bounded request to correct specified output errors.

17

Release manifest

A record linking template, schema, configuration, and evidence.

18

Content fingerprint

A digest identifying particular content.

19

Regression case

A test preserving required behavior after a change.

20

Field-level accuracy

The share of reviewed fields extracted correctly.

21

Fixture

Controlled example data used to test application behavior.

22

System message

A message that sets application instructions or role.