Skip to content

Chapter 5 of 6

Agents, Tools, and Program-Aided Reasoning

Function calling lets a model output structured arguments matching a user-defined schema, which the application then executes against real APIs, databases, or code, returning results for the model to use. It solves two problems at once: LLMs cannot directly act in the world, and downstream systems need strictly typed data. Toolformer-style training teaches the model to insert API calls into its text output and fold results back into its reasoning; at inference, structured outputs guarantee parseable JSON through constrained decoding.

Agentic prompting turns the model into a planner that decides the next action—calling tools, reading results, planning steps—rather than following a fixed prompt sequence. The agentic loop interleaves reasoning with action until a goal is met, bounded by max steps, tool scopes, and a stop condition to prevent runaway loops and excessive cost. ReAct (Reason + Act) interleaves natural-language reasoning traces with external actions like tool calls or searches: the model thinks step by step, decides an action, observes the result, and continues reasoning. Plan-and-execute agents instead draft a full plan first and then execute it, gaining transparency and fewer redundant steps at the risk of committing to a plan that may become stale.

For computational problems, program-aided language models (PAL) prompt the model to emit executable code—typically Python—that is then run by a deterministic interpreter, offloading arithmetic and logic. Self-debugging prompts the model to run its generated code, interpret error messages, and iteratively fix bugs without human intervention. Speculative decoding accelerates inference by using a small draft model to propose several tokens that a larger model verifies in parallel, while prefix caching stores key-value attention states for repeated prompt prefixes to cut latency and cost on long system prompts and RAG contexts. Semantic caching takes a similar idea further by indexing cached responses by embedding similarity so paraphrased queries can return stored answers.

All chapters
  1. 1Foundations of Prompting
  2. 2Prompting Patterns and Techniques
  3. 3Sampling, Decoding, and API Controls
  4. 4Retrieval-Augmented Generation and Grounding
  5. 5Agents, Tools, and Program-Aided Reasoning
  6. 6Safety, Reliability, and Evaluation

Drill it

Reading is not remembering. These come from the Chatgpt Prompting Essentials deck:

Q

What is a prompt in the context of ChatGPT?

A prompt is the natural-language input—question, instruction, or context—a user provides to a language model to elicit a response. It frames the task and guides...

Q

What does the role pattern in a prompt do?

The role pattern assigns the model a persona or function (e.g., 'You are a senior copyeditor'). It conditions tone, vocabulary, and reasoning style, often impro...

Q

How does few-shot prompting work?

Few-shot prompting supplies a small number of input–output examples within the prompt before the new query, demonstrating the desired pattern, format, or reason...

Q

What is zero-shot prompting?

Zero-shot prompting asks the model to perform a task using only the instructions in the prompt, with no examples. It relies on the model's pretrained knowledge...