chapter nine

9 Evaluation and performance for LLMs and agents

 

This chapter covers

  • Measuring and identifying hallucinations
  • Implementing red-teaming and stress testing strategies for robust AI systems
  • Building production-ready monitoring with frameworks
  • Implementing core architectural patterns
  • Evaluating agents

Throughout this book, we’ve built increasingly sophisticated large language model (LLM) applications. We started with basic prompt engineering, moved to structured outputs and function calling, and added retrieval-augmented generation (RAG) for grounding responses in real data. In chapter 8, we constructed multi-agent systems in which specialized agents collaborate on complex tasks. Each layer added capability.

Each layer also added ways for things to go wrong. A prompt can be poorly written. A structured output can fail to parse. A retrieval system can fetch irrelevant documents. An agent can call the wrong tool or loop indefinitely. But the most insidious failure—hallucination—cuts across all these problems. Unlike crashes and error messages, hallucinations don’t announce themselves. They slip past users and damage trust before anyone notices.

9.1 Identifying and measuring hallucinations

9.1.1 Four steps to identify and measure hallucinations

9.1.2 FActScore: fine-grained factual evaluation

9.1.3 ROUGE for summarization evaluation

9.1.4 LLM-as-a-judge: A holistic approach

9.1.5 Red teaming and stress testing

9.1.6 Using monitoring frameworks to detect hallucinations

9.2 Essential architectural patterns for performance

9.2.1 Token streaming: Presenting answers incrementally or at the same time

9.2.2 Handling surges with batching: System-level vs. OpenAI’s Batch API

9.2.3 Caching for efficiency

9.2.4 Multimodel fallback: Matching each query to the right model

9.2.5 Project: Building an e-commerce LLM service with batching, caching, and model fallback

9.2.6 Further improvements

9.3 Evaluating agent performance

9.3.1 Core metrics for evaluating LLMs

Summary