Chapter 2: Executing prompts programmatically
This introduction explains why carefully designed prompts matter to LLM application performance and defines prompt engineering as an iterative practice.
After this chapter you will be able to
- Prompts as model guidance. Explain that AI applications interact with LLMs mainly through prompts, and that clear, task-specific prompts help produce accurate and relevant outputs.
- Iterative prompt engineering. Describe prompt engineering as designing, testing, and refining prompts to guide LLM outputs toward reliable, high-quality results.
- Programmatic Prompts in Workflows. Explain that an LLM application uses prompts to generate completions that can be passed to the next component, and that LangChain typically constructs and sends those prompts programmatically rather than through a manual chat interface.
- Project environment and dependencies. Describe how to prepare the chapter project: create or obtain the ch02 folder, create and activate a venv virtual environment, and install Jupyter and the OpenAI packages. Explain why a from-scratch setup should use the package versions in requirements.txt.
- Notebook creation and execution. Describe how to start Jupyter, create a notebook from its browser interface, and run code in a cell.
- Chat completion inputs. Explain that chat.completions.create submits prompts to an OpenAI LLM using a model choice and a list of messages, each with a role and content.
- Reusable parameterized prompt templates. Explain why prompt templates use parameters for user input and what LangChain’s PromptTemplate offers when building LLM applications.
- Python Summarization Prompt Template. Describe how a Python function can generate a text-summarization prompt using the text, desired summary length, and preferred tone, which is then executed through LangChain’s ChatOpenAI wrapper.
- PromptTemplate execution sequence. Describe how PromptTemplate replaces a manually written prompt-template function: create a template instance, format it with parameters, then invoke ChatOpenAI with the formatted prompt.
- Task-Specific Prompt Design. Explain why an effective prompt should be tailored to the application’s task, such as classification, summarization, text generation, or question answering.
- Predefined Text Categories. Explain that text classification assigns an input text to one of the categories named in the instruction, such as assigning the headphones sentence to tech and the Julius Caesar sentence to history.
- Classification Prompt Components. Describe the instruction, input Text, and Output specification in the standard prompt, and explain how specifying “only the category” changes an overly detailed response into a category-only response.
- Three-way sentiment classification. Explain that sentiment analysis classifies text by whether it is perceived as positive, neutral, or negative, and illustrate the distinction with the section’s examples.
- Text summarization prompt essentials. Describe how to prompt an LLM to summarize supplied text at a preferred length, and how the request changes when the LLM is already familiar with the subject.
- Structured prompts for output control. Describe how the diver-watch prompt was revised to specify the desired audience, task, facts, tone, vocabulary, and length, and why those details were added.
- Focused answers from supplied text. Explain that question answering uses an LLM to retrieve specific information from supplied text rather than summarize the whole passage. Describe how the home-computer prompt asks for names and requests a list as the output.
- Palindrome reasoning failure. Explain why the shown palindrome answer was wrong and what it illustrates about LLM reasoning.
- Zero-shot prompting and limitations. Explain that zero-shot learning gives an LLM instructions without examples, and that it may work for some tasks but produce inaccurate results on more intricate reasoning tasks such as identifying palindromes.
- One-shot prompt example. Explain that one-shot learning means providing one example in a prompt, as in the instruction to sum palindromes paired with the example that 33 is a palindrome.
- Two-shot palindrome example outcome. Explain that the revised prompt gave two examples of palindromes, but the model still omitted 99 from the sequence and reported an incorrect sum.
- Explicit steps in prompts. Explain how the amended prompt directs the model to identify the palindromes before adding them, and why specifying that order can help with deterministic tasks.
- Few-shot learning from examples. Explain why the number-classification prompt needs examples and how the revised prompt lets the LLM infer rules for new numbers without stating those rules directly.
- Abra Kadabra classification rules. Describe the divisibility rules illustrated by the examples and apply them to a new number.
- Separating few-shot examples. Explain why the section favors separating few-shot examples from the prompt template, even though the hardcoded prompt produces the correct result.
- Chain of thought examples. Explain how the strange-sequence prompt combines several examples with logical steps, and trace why the tested sequence is classified as strange.
- In-context learning distinction. Explain how a prompt can introduce an unfamiliar task through examples or steps, and distinguish that from fine-tuning as described here.
- Role context task and input. Distinguish the persona, context, instruction, and input in a structured prompt: the role the LLM should adopt, the background it needs, the action it should take, and the material it should work with.
- Flexible prompt section labels. Explain why a prompt might mark its sections with XML-style tags and why not every section needs a label.
Section by section
Introduction
This introduction explains why carefully designed prompts matter to LLM application performance and defines prompt engineering as an iterative practice.
- Prompts as model guidance: Explain that AI applications interact with LLMs mainly through prompts, and that clear, task-specific prompts help produce accurate and relevant outputs.
Why it matters: The prompt is a major way an application influences the response it gets from an LLM. - Iterative prompt engineering: Describe prompt engineering as designing, testing, and refining prompts to guide LLM outputs toward reliable, high-quality results.
Why it matters: Testing and revising prompts helps an LLM application deliver more reliable results.
Key terms: prompts, structured inputs, LLMs, prompt engineering, creating, testing, iterating
2.1 Running prompts programmatically
This section explains how prompts and completions fit into an LLM workflow and contrasts programmatic prompting with entering prompts manually.
- Programmatic Prompts in Workflows: Explain that an LLM application uses prompts to generate completions that can be passed to the next component, and that LangChain typically constructs and sends those prompts programmatically rather than through a manual chat interface.
Why it matters: This distinction explains why the chapter runs prompts from code rather than only through a chat interface.
Key terms: prompts, completions, LangChain, programmatically, chain
2.1.1 Setting up the environment for this chapter
This section sets up a virtual environment and Jupyter Notebook for the chapter’s prompt-execution examples.
- Project environment and dependencies: Describe how to prepare the chapter project: create or obtain the ch02 folder, create and activate a venv virtual environment, and install Jupyter and the OpenAI packages. Explain why a from-scratch setup should use the package versions in requirements.txt.
Why it matters: This setup gives the chapter’s examples the environment and packages they expect. - Notebook creation and execution: Describe how to start Jupyter, create a notebook from its browser interface, and run code in a cell.
Why it matters: The notebook is where the reader enters and runs the chapter’s prompt examples.
Key terms: ch02, venv, virtual environment, Jupyter, OpenAI packages, requirements.txt, Jupyter Notebook, File > New > Notebook, notebook cells, Shift-Enter
2.1.2 Minimal prompt execution
This section introduces a minimal programmatic prompt call using OpenAI’s chat completions interface and explains its key inputs and setup choices.
- Chat completion inputs: Explain that chat.completions.create submits prompts to an OpenAI LLM using a model choice and a list of messages, each with a role and content.
Why it matters: These inputs are the basis for submitting a prompt from code. - Secure API key handling: Describe the section’s instruction to obtain the OpenAI API key securely before setting up the client, rather than hardcoding it.
Why it matters: Keeping the key out of source code reduces the risk of its misuse. - Model cost and accuracy tradeoff: Distinguish the model options described in the section by their speed, cost, and accuracy tradeoffs, including why the example uses GPT-5-nano.
Why it matters: The model choice affects both execution cost and the quality or speed you can expect. - Completions API choice: Explain why the book primarily uses the Completions API despite OpenAI preferring the Responses API, and identify LangChain’s ChatOpenAI wrapper as a common indirect route.
Why it matters: This explains why later LangChain examples may use a different interface from OpenAI’s current preference.
Key terms: chat.completions.create, model, messages, role, content, OpenAI API key, OpenAI client, GPT-5-nano, GPT-5-mini, GPT-5
2.2 Running prompts with LangChain
This section introduces a LangChain version of the earlier prompt example, but the supplied text does not include the code or explain how it works.
2.3 Prompt templates
This section introduces parameterized prompt templates as a way to incorporate user input into reusable LLM prompts, and identifies LangChain’s PromptTemplate as a way to manage them without custom functions.
- Reusable parameterized prompt templates: Explain why prompt templates use parameters for user input and what LangChain’s PromptTemplate offers when building LLM applications.
Why it matters: This is the reason the chapter introduces prompt templates before showing how to implement them.
Key terms: prompt templates, parameters, PromptTemplate, parametrized prompts
2.3.1 Implementing a prompt template with a Python function
This section illustrates a Python-function prompt template for text summarization and describes sending the generated prompt through LangChain’s ChatOpenAI wrapper.
- Python Summarization Prompt Template: Describe how a Python function can generate a text-summarization prompt using the text, desired summary length, and preferred tone, which is then executed through LangChain’s ChatOpenAI wrapper.
Why it matters: It connects reusable prompt generation to programmatic LLM execution.
Key terms: prompt template, text summarization, ChatOpenAI wrapper
2.3.2 Using LangChain’s PromptTemplate
Shows how to use LangChain’s PromptTemplate to format a parametrized prompt and pass it to ChatOpenAI.
- PromptTemplate execution sequence: Describe how PromptTemplate replaces a manually written prompt-template function: create a template instance, format it with parameters, then invoke ChatOpenAI with the formatted prompt.
Why it matters: This is the basic sequence for using a reusable prompt template in a LangChain call.
Key terms: PromptTemplate, parametrized templates, formatted prompt, ChatOpenAI
2.4 Prompt types
This section explains why prompts should be designed for the task an LLM application needs to perform.
- Task-Specific Prompt Design: Explain why an effective prompt should be tailored to the application’s task, such as classification, summarization, text generation, or question answering.
Why it matters: The task your application performs should guide how you write its prompt.
Key terms: prompts, text classification, sentiment analysis, summarization, text generation, question answering
2.4.1 Text classification
This section shows how to classify text into predefined categories and how an output specification keeps the response to a category name.
- Predefined Text Categories: Explain that text classification assigns an input text to one of the categories named in the instruction, such as assigning the headphones sentence to tech and the Julius Caesar sentence to history.
Why it matters: It lets you recognize what the prompt is asking the model to do with the text. - Classification Prompt Components: Describe the instruction, input Text, and Output specification in the standard prompt, and explain how specifying “only the category” changes an overly detailed response into a category-only response.
Why it matters: The output specification helps make classification responses concise and consistent.
Key terms: classification, predefined categories, input text, Instruction, Text, Output specification
2.4.2 Sentiment analysis
This section shows how to prompt an LLM to classify the sentiment of individual texts or a list of texts, including a programmatically usable table format.
- Three-way sentiment classification: Explain that sentiment analysis classifies text by whether it is perceived as positive, neutral, or negative, and illustrate the distinction with the section’s examples.
Why it matters: The three labels tell you what kind of answer a sentiment prompt is asking for. - Collective sentiment prompt output: Describe how a single prompt can request sentiment classifications for multiple texts and specify a table of stock names and sentiments as output.
Why it matters: An explicit table format makes a batch of classifications easier for code to use.
Key terms: sentiment analysis, text classification, positive, neutral, negative, single prompt, stock reports, stock name, sentiment, table
2.4.3 Text summarization
This section shows how to request a summary at a specified length, either from supplied text or from a work the LLM already knows.
- Text summarization prompt essentials: Describe how to prompt an LLM to summarize supplied text at a preferred length, and how the request changes when the LLM is already familiar with the subject.
Why it matters: This gives you a simple way to control the length of a summary request.
Key terms: text summarization prompt, input text, preferred length
2.4.4 Composing text
This section shows how an LLM can turn a list of facts into prose and how a structured prompt can better control the result.
- Text composition from facts: Explain how an LLM can generate a piece of writing from facts supplied in a text prompt, as illustrated by the diver-watch article.
Why it matters: This is the basic composing task that the more detailed prompt is meant to guide. - Structured prompts for output control: Describe how the diver-watch prompt was revised to specify the desired audience, task, facts, tone, vocabulary, and length, and why those details were added.
Why it matters: Explicit writing requirements give the model more guidance than the facts alone.
Key terms: generative AI, facts, Persona, Instruction, Tone, Output Format
2.4.5 Question answering
This section shows how to ask an LLM focused questions about supplied text and how to express a question using Q and A shorthand.
- Focused answers from supplied text: Explain that question answering uses an LLM to retrieve specific information from supplied text rather than summarize the whole passage. Describe how the home-computer prompt asks for names and requests a list as the output.
Why it matters: Focused questions let an application get the particular information it needs from a passage. - Q and A prompt shorthand: Describe how the section uses Q and A as shorthand for Question and Answer: a question follows Q:, and the LLM completes the answer after A:.
Why it matters: The shorthand offers a simple way to frame a focused question in a prompt.
Key terms: question answering, Instruction, Text, Output, Q and A, Question and Answer, Q:, A:
2.4.6 Reasoning
This section uses two number-summing prompts to show how LLM reasoning can work and where it can fail.
- Selecting numbers before summing: Trace how ChatGPT answered the square-number prompt by identifying the numbers that met the condition and then adding them.
Why it matters: It shows that the answer depends on choosing the right numbers, not just doing the arithmetic. - Palindrome reasoning failure: Explain why the shown palindrome answer was wrong and what it illustrates about LLM reasoning.
Why it matters: A correct-looking sum is not enough if the model overlooked an item it should have included.
Key terms: reasoning, square numbers, palindrome, prompt engineering
2.5 Reasoning in detail
Defines zero-shot prompting and explains why instructions alone may be unreliable for complex reasoning tasks.
- Zero-shot prompting and limitations: Explain that zero-shot learning gives an LLM instructions without examples, and that it may work for some tasks but produce inaccurate results on more intricate reasoning tasks such as identifying palindromes.
Why it matters: Recognizing this limitation helps you judge when instructions alone may not be enough for a reasoning task.
Key terms: zero-shot learning, prompt techniques, reasoning tasks
2.5.1 One-shot learning
The section introduces one-shot learning through a palindrome-summing prompt and shows that a single example did not fix the model’s error.
- One-shot prompt example: Explain that one-shot learning means providing one example in a prompt, as in the instruction to sum palindromes paired with the example that 33 is a palindrome.
Why it matters: It gives you a concrete way to recognize a one-shot prompt. - Persistent palindrome error: Describe how the response still missed a palindrome despite the one-shot example: it listed 1331, 121, 232, and 7 but omitted 99 from the sequence and sum.
Why it matters: The example shows that supplying one demonstration did not necessarily produce a correct answer.
Key terms: one-shot learning, example, prompt, palindromes
2.5.2 Two-shot learning
A two-shot prompt for summing palindromes still produces an incorrect answer because the model overlooks 99.
- Two-shot palindrome example outcome: Explain that the revised prompt gave two examples of palindromes, but the model still omitted 99 from the sequence and reported an incorrect sum.
Why it matters: The example shows why adding examples alone was not enough for this task.
Key terms: two-shot learning, palindromes
2.5.3 Providing steps
The section shows how specifying steps in a prompt guides a palindrome-summing task and explains why explicit steps can remain useful even when newer models reason without them.
- Explicit steps in prompts: Explain how the amended prompt directs the model to identify the palindromes before adding them, and why specifying that order can help with deterministic tasks.
Why it matters: Explicit steps help you guide a model when the order of operations matters. - Changing need for prompt guidance: Describe how the original, shorter prompt now produces a step-by-step answer, while explicit steps may still help on more complex deterministic tasks.
Why it matters: Prompting techniques should be judged against the model you are using, not assumed to be necessary forever.
Key terms: Steps, palindromes, step-by-step, reasoning models, minimal prompts, prompt engineering
2.5.4 Few-shot learning
The section shows how several worked examples can let an LLM infer and apply a classification rule that the prompt never states directly.
- Few-shot learning from examples: Explain why the number-classification prompt needs examples and how the revised prompt lets the LLM infer rules for new numbers without stating those rules directly.
Why it matters: Examples can communicate a new task when a bare instruction leaves its rules unclear. - Abra Kadabra classification rules: Describe the divisibility rules illustrated by the examples and apply them to a new number.
Why it matters: Applying the rule to unseen numbers shows what the LLM inferred from the worked examples.
Key terms: few-shot learning, examples, double forward slash (//), Abra, Kadabra, Abra Kadabra, None, divisible by 5, divisible by 7
2.5.5 Implementing few-shot learning with LangChain
This section contrasts a working hardcoded few-shot prompt with LangChain’s cleaner approach of separating examples from the prompt template and injecting them later.
- Separating few-shot examples: Explain why the section favors separating few-shot examples from the prompt template, even though the hardcoded prompt produces the correct result.
Why it matters: It explains the improvement LangChain makes here without suggesting that the original prompt failed.
Key terms: few-shot prompt, training examples, prompt template
2.5.6 Chain of Thought
The section demonstrates chain-of-thought prompting with worked examples, places it within in-context learning, and briefly contrasts it with two approaches to reasoning.
- Chain of thought examples: Explain how the strange-sequence prompt combines several examples with logical steps, and trace why the tested sequence is classified as strange.
Why it matters: Worked reasoning examples can help an LLM apply a rule it was not given in advance. - In-context learning distinction: Explain how a prompt can introduce an unfamiliar task through examples or steps, and distinguish that from fine-tuning as described here.
Why it matters: This distinction clarifies what prompt examples accomplish without retraining a model. - Beyond CoT reasoning approaches: Distinguish the problems Tree of Thought and Thread of Thought address beyond ordinary chain-of-thought reasoning.
Why it matters: The contrast shows that step-by-step reasoning is not the only way to address difficult reasoning tasks.
Key terms: Chain of Thought, CoT, few-shot learning, strange, in-context learning, one-shot, two-shot, fine-tuning, Tree of Thought, ToT
2.6 Prompt structure
This section presents a customizable checklist of prompt elements and explains how labeling or tagging sections can make a prompt’s structure clearer.
- Role context task and input: Distinguish the persona, context, instruction, and input in a structured prompt: the role the LLM should adopt, the background it needs, the action it should take, and the material it should work with.
Why it matters: Separating these parts makes it easier to give the LLM a clear task and the information needed to carry it out. - Process style format and examples: Describe how steps, tone, output format, and examples can guide the LLM’s processing and answer, including when examples are especially useful.
Why it matters: These elements help shape not just what the LLM says, but how it produces and presents the answer. - Flexible prompt section labels: Explain why a prompt might mark its sections with XML-style tags and why not every section needs a label.
Why it matters: Clear boundaries can help organize a prompt without forcing every task into an identical template.
Key terms: Persona, Context, Instruction, Input, Steps, Tone, Output format, Examples, XML-style tags, prompt structure
2.7 Summary
The chapter summary recaps how to design clear prompts, improve them with examples, and execute reusable templates programmatically.
Where this is used later
- Setting up the project (chapter 4) · Earlier environment setup provides background for creating and activating a Python environment.
- Summary (chapter 3) · Creating prompts with LangChain's PromptTemplate.
- Summary (chapter 4) · Using LangChain components to construct prompt-driven operations.
- Summarizing a document bigger than the context window (chapter 3) · Summarizing longer documents builds on the basic request to summarize supplied text.
- A basic Q&A chatbot over a single document (chapter 6) · Applies question answering to a chatbot over a single document.
- Generating a structured SQL query (chapter 10) · Assumes familiarity with few-shot prompting.
- Generating queries for a graph database (chapter 10) · the use of examples in a few-shot prompt
- Prompt engineering (chapter 4) · Provides a checklist for crafting the research engine’s prompts.
Concepts explained in this chapter
- promptA system prompt is an instruction message supplied to a language model to establish high-level behavior or constraints.
- few-shot learningPrompt examples are illustrative instances supplied in a prompt to guide a language model toward a desired task behavior or output pattern.
- output formatAn output format is a specification of the structure or representation in which a language model should return its answer.
- chain-of-thought promptingChain-of-thought prompting is a technique that elicits or guides a model to produce or follow intermediate reasoning steps before a final answer.
- language modelA large language model is a neural language model trained on extensive text data to understand and generate language and perform text-based reasoning.
- xml-style tagXML-style prompt tags are markup-like delimiters that organize prompt content into named sections.
- Tree-of-thoughtTree of thought is a reasoning technique that lets a language model explore, evaluate, refine, and backtrack across multiple reasoning paths.
- thread of thoughtThread of thought is a reasoning technique that decomposes large or chaotic contexts while filtering irrelevant details and maintaining focus on relevant information.
- brand voiceBrand voice is the characteristic style, tone, and manner consistently expressed in an organization’s communications.
- contextPrompt context is background text or information supplied alongside a request to help a language model interpret it and formulate an answer.
- personaA persona is a prompt specification of the role, expertise, or perspective that a language model should adopt.
- templateA prompt template is a reusable structure that combines user input, context, and optionally examples into a model request.
- GPT-5-nanoGPT-5-nano is a small GPT-5 model variant intended for fast, lower-cost language processing.
- LangChainLangChain is an open-source framework for building applications that combine language models with data sources, tools, and multi-step workflows.
- prompt engineeringPrompt design is the practice of formulating instructions and context to influence a language model's behavior and output.
- summarizationSummarization is the generation of a shorter representation that preserves the important information in source content.
- LLM applicationAn LLM application is software that provides a use-case-specific interface or workflow around a large language model.
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.
