liveBook · Manning

Chapter 2: Generating trustworthy responses with prompt engineering

What you will learn in chapter 2 of Building Reliable AI Systems

The introduction explains how prompt engineering guides LLM outputs and why plausible hallucinations make reliable prompting important.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains how prompt engineering guides LLM outputs and why plausible hallucinations make reliable prompting important.

Key terms: prompt engineering, context, instructions, examples, hallucination, provided context

2.1 Choosing the right model

This section establishes model selection as the first and most consequential reliability decision before parameter tuning or prompt design.

Key terms: model selection, cost ceiling, latency floor, capability range, available parameters, reliability requirements

2.1.1 Reasoning vs. nonreasoning models

This section contrasts reasoning and nonreasoning models and gives a practical rule for choosing between them.

Key terms: reasoning models, nonreasoning models, multistep problems, sampling parameters, temperature, top-p, reasoning_effort, non-reasoning model, reasoning model, quality bar

2.1.2 The cost-capability spectrum

This section explains how to balance model capability, cost, and operational control when choosing between model tiers and commercial APIs versus open source models.

Key terms: flagship model, mini variant, high-volume tasks, quality-sensitive tasks, open source models, commercial APIs, model life cycle, self-hosting, fine-tuning

2.1.3 Model selection as a reliability decision

Model selection should remain flexible because available models and their prices change.

Key terms: model selection, configuration parameter, deprecated

2.2 Tailoring LLM settings for maximum reliability

This section introduces configurable LLM settings as a way to steer responses toward greater reliability for a given task.

Key terms: temperature, top-p sampling, maximum output length, stop sequences, penalty strengths, hallucinations

2.2.1 Optimizing temperature for predictable outputs

This section explains how temperature changes response variability, why predictability does not guarantee accuracy, and how to choose settings for a chatbot.

Key terms: temperature, next-token probabilities, deterministic, determinism, factual accuracy, hallucinations, temperature=0, product inquiries, diversity, incoherence

2.2.2 Top-P Sampling technique

This section explains how top-p sampling limits candidate tokens and how to tune it to balance response variety with reliability.

Key terms: top-p, nucleus sampling, candidate tokens, cumulative probability threshold, lower top-p, higher top-p, consistency, creativity, hallucinations, top-p value

2.3 Limiting output and turns to reduce hallucination risk

The section explains how scoping response length and conversation turns can reduce opportunities for unsupported claims without cutting off useful information.

Key terms: unsupported claims, output length, hallucinations, chain-of-thought prompting, maximum token limits, stop sequences, LLM API, postprocessing, maximum number of turns, terminating the interaction

2.4 Applying frequency and presence penalties for balanced content

This section explains how frequency and presence penalties discourage repetition, how they differ, and how they can make chatbot responses more varied.

Key terms: frequency penalty, presence penalty, token, repetitive or redundant outputs, frequency penalties, presence penalties, penalty values, sampling new tokens

2.5 Minimizing intrinsic randomness for stable performance

The section explains why LLM outputs can vary despite low-randomness settings and describes ways to make responses more stable without always eliminating useful diversity.

Key terms: intrinsic randomness, temperature, top-p sampling, token probabilities, fixed random seed, seed parameter, reproducible, majority voting, weighted averaging, ensemble averaging

2.6 Foundations of prompt engineering for reliable LLMs

This section introduces effective prompts as a way to align LLM behavior with reliability goals and reduce hallucinations.

Key terms: effective prompts, reliability goals, hallucinations

2.6.1 Designing components of a prompt for reliability

This section shows how to structure a prompt so the model has a clear task, relevant evidence, predictable output requirements, and instructions against unsupported claims.

Key terms: clear instructions, relevant context, focused input data, expected output format, explicit quality criteria, factual accuracy, unsupported claims

2.6.2 Crafting basic prompts with a focus on dependability

This section shows how specific instructions and deliberate prompt components make LLM responses more targeted and dependable.

Key terms: clarity, specificity, instructions, targeted response, clear instructions, relevant context, structured output, variable input, customer query

2.7 Prompt engineering techniques for preventing hallucinations

The section explains why instructions alone can be insufficient and introduces prompting as a foundation that complements retrieval.

Key terms: instructions, clear examples, reasoning steps, prompt design, prompting, retrieval, RAG, real-time knowledge

2.7.1 Zero-shot prompting

This section explains what zero-shot prompting can do, where it falls short, and why prompts must be tested on the model that will use them.

Key terms: zero-shot prompting, example demonstrations, pretraining, post-training alignment, zero-shot, return policy, context, examples, target model, wording

2.7.2 Few-shot prompting for contextual stability

This section shows how examples in a prompt help an LLM follow a desired pattern and interpret unfamiliar terms, while showing that examples alone may not make multistep reasoning reliable.

Key terms: few-shot prompting, example demonstrations, one-shot generalization, multistep logical reasoning, chain-of-thought prompting

2.7.3 Chain-of-thought prompting for transparent reasoning

This section explains how showing intermediate reasoning steps can help an LLM handle multistep tasks, including when a prompt has no worked example.

Key terms: chain-of-thought (CoT) prompting, intermediate reasoning steps, reasoning chains, few-shot examples, zero-shot variation, Let’s think step by step, few-shot learning, logical conditions, CoT prompting

2.7.4 Enhanced CoT prompt

This section shows how step-by-step chain-of-thought prompts can improve a chatbot’s reasoning and when explicit instructions may be useful.

Key terms: CoT prompting, intermediate steps, chain of thought

2.7.5 Automatic CoT for scalable reasoning

This section explains how Auto-CoT scales step-by-step reasoning through generated or learned intermediate steps, while warning that those steps need validation.

Key terms: Automatic chain of thought, Auto-CoT, reasoner model, main prompt, fine-tune, main model, CoT examples, intermediate steps, validate, generated chains of thought

2.7.6 Self-consistency for cross-verification

This section explains how comparing multiple responses to the same input can reveal inconsistencies and help produce a more reliable answer.

Key terms: self-consistency, random seeds, prompt variations, cross-verifying, common themes, inconsistent or potentially hallucinated claims, synthesized output

2.7.7 Tree-of-thought prompting for structured decision-making

This section explains how tree-of-thought prompting compares possible solutions before producing a focused response to a complex query.

Key terms: tree-of-thought (ToT) prompting, decision tree, thought branches, return within 30-day policy, warranty claim, exception process, internal decision-making process

2.8 Project: Creating a reliable weather assistant with OpenAI’s function calls

The section explains how a weather assistant can use function calls to ground its answers in current external data instead of relying on the model’s training data.

Key terms: function calling, grounding, trusted external API, real-time data, function invocation, function results, final response, required parameters

2.8.1 Building a weather assistant

The section explains that the weather assistant answers current-weather questions by using a function to fetch live data from an external weather API.

Key terms: weather bot, real-time weather data, function, external weather API

2.8.2 Getting started and defining the weather function

This section identifies the setup requirements and introduces a location-based weather function and its JSON description for the language model.

Key terms: weather function, location, weather report, JSON schema, Python 3.7 or later, OpenAI API key, Weather API key, required packages

2.8.3 Integrating the chatbot

This section uses a weather chatbot to show how function calls bring external data into natural-language answers and reduce hallucination risk.

Key terms: function calls, get_weather, weather assistant, trusted external API, factual information retrieval, hallucinations, authoritative data sources, clear function interfaces, up-to-date information

2.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: AI reliability: Building LLMs for the real world Chapter 3: Grounding outputs with RAG →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.