liveBook · Manning

Chapter 2: Executing prompts programmatically

What you will learn in chapter 2 of AI Agents and Applications

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

Section by section

Introduction

This introduction explains why carefully designed prompts matter to LLM application performance and defines prompt engineering as an iterative practice.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

Concepts explained in this chapter

← Chapter 1: Introduction to AI agents and applications Chapter 3: Summarizing text using LangChain →

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.