liveBook · Manning

Chapter 2: The brain of AI agents: LLMs

What you will learn in chapter 2 of Build an AI Agent (From Scratch)

Introduces the research agent built throughout the book and identifies the LLM as its decision-making brain.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the research agent built throughout the book and identifies the LLM as its decision-making brain.

Key terms: research agent, large language model (LLM), decision-making brain

2.1 Choosing LLMs for agents

This section frames LLM selection as an early agent-development decision that affects performance, cost, and development speed.

Key terms: LLM, agent’s performance, cost, development speed

2.1.1 Starting with closed LLMs

Explains the difference between open and closed LLMs and why closed LLMs are a practical starting point for agent development.

Key terms: open LLMs, closed LLMs, weights, APIs, rapid experimentation and validation, API key, agent logic, prototype, production, LLM intelligence

2.1.2 Expanding to open LLMs

This section explains why and how to consider open LLMs after validating an agent with closed LLMs.

Key terms: self-hosting, classification, extraction, core reasoning, auxiliary tasks, external APIs, self-hosted open LLMs, sensitive data, closed LLMs, open LLMs

2.1.3 Essential LLM capabilities for agents

Identifies LLM capabilities agents need and explains why flexible model selection matters as models change.

Key terms: tool calling, function calling, structured output, JSON, schemas, long context support, context window, context engineering, model agility, LiteLLM

2.2 LLM API basics for building agents

This section introduces the upcoming hands-on work with LLM APIs and the capabilities needed for agent development.

2.2.1 Setting up the development environment

This section sets up the companion notebooks and explains how they obtain the API keys needed for LLM calls.

Key terms: uv, pyproject.toml, virtual environment, Jupyter Lab, .env, OpenAI API key, HF_TOKEN, python-dotenv, find_dotenv(), environment variables

2.2.2 Getting started with the OpenAI API

This section explains the basic inputs and outputs of an OpenAI Chat Completions call, common generation controls, and how the API compares with another provider’s.

Key terms: chat.completions.create, model, messages, role, content, developer, user, assistant, response.choices[0].message.content, choices

2.2.3 Unifying providers with LiteLLM

This section explains how LiteLLM simplifies comparing and switching LLM providers through one interface.

Key terms: LiteLLM, unified interface, completion function, OpenAI format, environment variables

2.2.4 Conversation management: Handling stateless APIs

Explains why LLM API conversations require the caller to resend history, and why growing agent context has a cost.

Key terms: stateless, API call, messages list, conversation history, user role, assistant role, context, tool-call results, search results, intermediate reasoning steps

2.2.5 Generating structured output

This section explains why agents need structured LLM responses and how to request a validated output format with Pydantic.

Key terms: structured output, tool calling, arguments, JSON, Pydantic, BaseModel, schema, response_format, content attribute, ExtractedInfo

2.2.6 Asynchronous calls

This section shows how to run LLM requests concurrently while limiting API calls and handling failures.

Key terms: async def, coroutines, await, asyncio.gather, acompletion, asyncio.Semaphore, async with, num_retries, exponential backoff, return_exceptions=True

2.3 Enhancing agent intelligence: Prompt engineering

The section distinguishes user prompts from system prompts and explains why developer-defined system prompts help an LLM act consistently as an agent.

Key terms: user prompts, system prompts, system prompt, personality, permissions, constraints, tool-use policies

2.3.1 The role of system prompts

This section explains how system prompts guide an agent’s decisions by defining its identity, response style, behavioral boundaries, and knowledge limits.

Key terms: system prompt, context, behavioral rules, product identity, role, capabilities, output format, style, tone, bullet points

2.3.2 Guidelines for agent prompts

Explains how agent prompts should encourage autonomous action, guide when to use or avoid tools, and provide clear tool information.

Key terms: agent, chatbot, intent, system prompt, clarification, tool-use guidelines, default attitude, trigger patterns, linguistic signals, conversation search tool

2.4 Experiencing LLM limitations with GAIA

This section introduces an upcoming experiment using real problems to observe LLM capabilities and limitations and motivate the need for agent tools.

2.4.1 Why GAIA: Setting goals for agent development

This section explains why GAIA provides a measurable, user-relevant target for developing a research agent and introduces its difficulty levels and problem fields.

Key terms: benchmark, test cases, verifiable answers, difficulty levels, GAIA, simulated user inputs, verified answers, level-1, level-2, level-3

2.4.2 Experiment setup

This section sets up a GAIA experiment that collects comparable LLM answers, manages API calls, checks correctness, and records results across models.

Key terms: structured output, is_solvable, unsolvable_reason, final_answer, ground truth, semaphore, solve_problem, async with, acompletion, num_retries

2.4.3 Results and analysis: Why LLMs need tools

The GAIA results show why tool-free LLM performance is limited, why apparent successes can be unreliable, and what external capabilities agents need.

Key terms: level-1 GAIA problems, theoretical maximum accuracy, without tools, Judged solvable, calibration, external information, training data, confabulating, authoritative sources, web search

2.5 Summary

This summary brings together the chapter’s practical guidance on choosing and using LLMs for agents, prompting them, and recognizing when they need tools.

Where this is used later

Concepts explained in this chapter

← Chapter 1: What is an AI agent? Chapter 3: Enabling actions: Tool use →

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.