Overview
2 Generating Trustworthy Responses with Prompt Engineering
This chapter explains how prompt engineering and model configuration work together to make large language model applications more reliable, predictable, and resistant to hallucinations. It emphasizes that reliability begins with choosing the right model for the task: smaller non-reasoning models are often sufficient for routine, high-volume work, while larger or reasoning-focused models may be appropriate for complex analysis, planning, coding, or safety-sensitive tasks. Model choice affects cost, latency, available parameters, and long-term maintainability, so systems should treat model names and settings as configurable rather than hardcoded.
The chapter then describes practical controls for shaping model behavior. Temperature and top-p sampling influence randomness and diversity, with lower values generally producing more focused outputs and higher values increasing creativity but also risk. Output limits, stop sequences, and turn limits help keep responses scoped to what the model can answer reliably, while frequency and presence penalties reduce repetition and make conversations feel more natural. The chapter also notes that even with deterministic settings, some randomness can remain, so developers may use fixed seeds, multiple generations, or majority-vote approaches to improve consistency.
The chapter’s main prompt engineering guidance centers on clear instructions, relevant context, focused input data, explicit output formats, and quality criteria that discourage speculation. It compares zero-shot prompting, few-shot prompting, chain-of-thought prompting, automatic reasoning chains, self-consistency, and tree-of-thought methods, showing how each can improve reliability for different tasks. Finally, it demonstrates how function calling can ground responses in trusted external data, using a weather assistant as an example: the model interprets the user’s request, calls an external API for factual information, and then produces a natural response based on verified results. This pattern reduces hallucinations by separating language generation from authoritative data retrieval.
Summary
- Prompt Engineering: Iterative development of prompts to direct LLMs, enhancing output quality through refinement. Includes techniques like zero-shot, few-shot, and chain-of-thought prompting.
- Temperature Setting: Influences response variability. Begin with mid-range settings (around 0.5) and adjust for desired specificity or creativity.
- Top P Parameter: Dictates output diversity; start with moderate values (near 0.8) to balance predictability with variety.
- Maximum Output Length and Stop Sequences: Set limits to manage response brevity or detail and implement clear end points for model responses.
- Frequency and Presence Penalties: Use initial moderate values to prevent repetitive language while retaining coherence.
- Few-shot Learning: Providing a couple of examples in the prompt to prime the LLM for more accurate task performance, especially useful for adapting to new concepts or specific task formats.
- Chain-of-Thought (CoT) Prompting: Demonstrating intermediate reasoning steps in prompts to enable complex problem-solving and improve transparency in model reasoning.
- Automatic Chain-of-Thought (Auto-CoT): Techniques for automatically generating reasoning chains, making complex reasoning more scalable and adaptable.
- Tree-of-Thought (ToT) Prompting: An advanced technique that enables models to explore multiple solution pathways simultaneously.
- Self-Consistency: Generating multiple outputs for the same input and cross-verifying results to increase confidence and reduce hallucinations.
- Function Calls: Allow conversational AI models to invoke external functions and APIs on-demand, enabling chatbots to execute real-world logic or access external data sources during a dialogue, grounding responses in up-to-date information.
FAQ
What is prompt engineering, and why is it important for reliable LLM systems?
Prompt engineering is the iterative process of constructing, analyzing, and refining prompts so large language models produce higher-quality outputs. It is one of the main ways developers influence LLM behavior. Good prompts provide clear instructions, relevant context, examples, output constraints, and quality criteria. Poorly designed prompts can increase hallucinations, where a model generates plausible but false or unsupported information.
How should I choose between reasoning and non-reasoning models?
Start with a non-reasoning model for most tasks because it is usually faster, cheaper, and supports more sampling parameters such as temperature and top-p. If the task involves complex multi-step reasoning, math, coding, planning, or careful logical analysis, try a reasoning model. For high-volume straightforward tasks, use the smallest model that meets your quality bar. Model choice is a reliability decision because it affects cost, latency, capability, available parameters, and long-term maintainability.
What does temperature control in an LLM, and does setting it to zero prevent hallucinations?
Temperature controls randomness in token selection. Lower values make responses more predictable and reproducible, while higher values make outputs more diverse and creative. However, temperature=0 does not guarantee factual accuracy. A deterministic model can still be consistently wrong. Very high temperatures can produce incoherent or nonsensical output. For reliable outputs, the chapter recommends using a low temperature such as 0.3, while creative tasks may use values around 0.7.
What is top-p sampling, and how is it different from temperature?
Top-p, or nucleus sampling, limits token choices to the smallest set of likely tokens whose cumulative probability exceeds a threshold. For example, top_p=0.9 means the model considers only the most probable tokens that together account for 90% probability mass. Lower top-p values create more focused and deterministic responses; higher values allow more variety but increase the risk of irrelevant or incorrect details. Temperature changes the probability distribution, while top-p restricts the candidate token pool. The chapter notes that OpenAI recommends adjusting either temperature or top-p, not both at the same time.
How can limiting output length and conversation turns reduce hallucination risk?
Longer responses and extended conversations create more opportunities for the model to add unsupported details. Limiting output with maximum token limits, stop sequences, and clear conversation-ending rules helps keep answers focused. For example, a return-policy answer can be constrained to one or two accurate sentences rather than a long response that invents exceptions or extra conditions. The goal is not always to make responses short, but to scope them to what the model can reliably answer.
What are frequency and presence penalties used for?
Frequency and presence penalties discourage repetition. A frequency penalty reduces the likelihood of reusing a token based on how often it has already appeared, while a presence penalty discourages reuse simply because the token has appeared before. These settings can make chatbot conversations less repetitive and more natural. For example, instead of recommending the same laptop multiple times, the chatbot can vary its suggestions and mention different product features. The chapter suggests values around 0.5 to 1.0 for many cases.
Why can LLM outputs vary even with low temperature, and how can I improve reproducibility?
LLMs have intrinsic randomness caused by implementation details such as floating-point operations, parallel processing, and stochastic generation. Even with a low temperature, the same prompt may produce small differences in wording, ordering, or emphasis. To improve reproducibility, use a fixed seed parameter when the API supports it. Another strategy is to generate multiple outputs and use majority voting, averaging, or selection of the most representative answer.
What components should a reliable prompt include?
A reliable prompt should include clear instructions, relevant context, focused input data, an expected output format, and explicit quality criteria. Clear instructions tell the model exactly what to do. Context grounds the answer in the right domain. Focused input prevents distraction. Output format constraints make the response predictable. Quality criteria tell the model to prioritize factual accuracy, logical coherence, and avoidance of speculation or unsupported claims.
What is the difference between zero-shot, few-shot, and chain-of-thought prompting?
Zero-shot prompting gives the model an instruction without examples. It works for many simple tasks but can be inconsistent for complex ones. Few-shot prompting includes example inputs and outputs so the model can learn the expected style, format, or task pattern. Chain-of-thought prompting adds intermediate reasoning steps, helping the model break complex problems into smaller steps. CoT is especially useful for standard non-reasoning models on multi-step logic, while reasoning models may already perform internal reasoning and may not need explicit CoT prompts.
How do function calls help reduce hallucinations in AI applications?
Function calling lets the model request external tools or APIs instead of inventing factual information from its training data. In the chapter’s weather assistant project, the model parses the user’s weather query, calls a trusted weather API through a function, receives real-time data, and then generates a natural final response. This separates responsibilities: the LLM handles language understanding and response composition, while authoritative external systems provide factual data. The same pattern can be used for finance, scheduling, travel, retail, entertainment, and other domains where up-to-date accuracy matters.