Solutions to Common Challenges When Building GenAI Agents and Applications
Valliappa Lakshmanan and Hannes Hapke

#Generative_AI
#GenAI
#LLM
#GPT
✨ طراحی الگوهای Generative AI
🧠 هوش مصنوعی Generative قابلیتهای خیلی قدرتمندی رو در اختیارمون میذاره، ولی این قابلیتها با محدودیتهای جدی همراهن که باید برطرفشون کنی تا بتونی یه اپلیکیشن یا Agent قابل اعتماد بسازی. خوشبختانه، متخصصهای این حوزه یه کتابخونه از ۳۲ الگوی طراحی امتحانشده و واقعی جمعآوری کردن که دقیقاً چالشهایی رو پوشش میدن که موقع کار با LLMها باهاشون مواجه میشی؛ مثل Hallucination، پاسخهای Nondeterministic و محدودیت دانش مدل.
📘 این کتاب، تحقیقات علمی و تجربیات واقعی دنیا رو به شکل مشاورههای عملی درآورده که میتونی مستقیم توی پروژههات استفاده کنی. هر الگو یه مشکل رو توصیف میکنه، راهحل اثباتشدهش رو با یه مثال کاملاً کدشده نشون میده و trade-offهای مربوط بهش رو هم بررسی میکنه.
نکات کلیدی کتاب:
📋 فهرست مطالب
✍️ از پیشگفتار کتاب:
اگر مهندس هوش مصنوعی هستی و داری اپلیکیشنهای generative ai میسازی، احتمالاً بارها با این شکاف آزاردهنده روبهرو شدی: ساختن prototypeهای جذاب خیلی راحت و سریع انجام میشه، اما deploy کردن و نگهداریشون به صورت قابل اعتماد در محیط production واقعاً پیچیده و چالشبرانگیزه.
مدلهای foundational کار ساخت demoهای چشمگیر رو خیلی آسان کردن، ولی سیستمهای واقعی تولیدی نیاز به حل مسائل اساسی دارن: hallucinationهایی که دقت رو از بین میبرن، خروجیهای ناسازگار که فرآیندهای بعدی رو بههم میریزن، شکافهای دانشی که کاربردهای enterprise رو محدود میکنن و مشکلات reliability که باعث میشن سیستم برای کارهای حیاتی و مهم مناسب نباشه.
این کتاب دقیقاً همین شکاف رو پر میکنه. ۳۲ الگوی طراحی battle-tested رو در اختیارمون میذاره که مشکلات تکراری و رایجی رو که موقع ساخت اپلیکیشنهای GenAI در سطح production باهاشون مواجه میشیم، حل میکنن. این الگوها ایدههای تئوری نیستن؛ راهحلهای واقعی هستن که از تحقیقات پیشرفته میان و توسط مهندسایی که سیستمهای GenAI رو در مقیاس بزرگ deploy کردن، تست و پالایش شدن.
🧠 تفاوت با supervised machine learning:
در supervised ML معمولاً یه مدل مخصوص مسئله رو روی دیتاست بزرگ آموزش میدی، اما اپلیکیشنهای GenAI اغلب فاز آموزش ندارن. در عوض، مستقیم از مدلهای foundational عمومی مثل GPT از OpenAI، Claude از Anthropic، Gemini از گوگل یا Llama از Meta استفاده میکنن. این کتاب دقیقاً روی الگوهای طراحی برای اپهایی تمرکز داره که روی همین مدلهای foundational ساخته شدن.
✍️ این کتاب برای چه کسانیه؟
👥 این کتاب مخصوص software engineerها، data scientistها و enterprise architectهایی نوشته شده که دارن اپلیکیشنهایی مبتنی بر مدلهای foundational GenAI میسازن. اینجا مجموعهای از راهحلهای اثباتشده رو برات جمعآوری کرده که بتونی چالشهای رایج ساخت اپلیکیشنها و agentهای GenAI رو به خوبی حل کنی.
این کتاب رو بخون تا ببینی متخصصهای این حوزه چطور با مشکلاتی مثل hallucination، پاسخهای nondeterministic، knowledge cutoff و نیاز به customize کردن مدل برای صنعت یا سازمان خودتون کنار میان. خیلی از مشکلات قدیمی مهندسی نرمافزار در این دنیای جدید، راهحلهای تازهای پیدا کردن. مثلاً برای کنترل latency و کاهش هزینهها از تکنیکهایی مثل distillation، speculative decoding، prompt caching و template generation استفاده میشه.
🧩 درک الگوهای مختلف کتاب به سطح دانش متفاوتی نیاز داره. مثلاً الگوی Chain of Thought (الگوی ۱۳) فقط دانش برنامهنویسی پایه میخواد، Tool Calling (الگوی ۲۱) درک خوبی از API design لازم داره و Dependency Injection (الگوی ۱۹) تجربه کار روی پروژههای نرمافزاری بزرگمقیاس رو طلب میکنه. اما Content Optimization (الگوی ۵) آشنایی با آمار و machine learning میخواد و Small Language Model (الگوی ۲۴) درک عمیقتری از بهینهسازی سختافزار لازم داره.
تخمین نویسندگان اینه که حدود ۷۵٪ کتاب رو یه junior software engineer یا دانشجوی سال سوم کامپیوتر به راحتی بفهمه. بخش باقیمانده نیاز به دانش یا تجربه تخصصی داره.
نکته مهم:
AI engineering خیلی با software engineering، data engineering و ML همپوشانی داره، ولی این کتاب تمرکز خودش رو فقط روی core AI engineering نگه داشته. پیشنهاد میکنن این کتاب رو به عنوان companion کتاب Machine Learning Design Patterns (انتشارات اُرایلی) بخونی که والیاپا لاکشمانان هم روش کار کرده. اون کتاب الگوهای مربوط به آموزش مدلهای bespoke رو پوشش میده.
در عمل، خیلی وقتها با هر دو نوع مدل کار میکنی: هم foundational modelهای عمومی و هم مدلهای bespoke. گاهی با foundational شروع میکنی ولی برای edge caseها مجبور میشی اون رو customize یا fine-tune کنی. این دو کتاب کاملاً مکمل هم هستن و بهت کمک میکنن با هر دو دسته مدل به بهترین شکل کار کنی. پیشنهاد میکنم هر دو رو بخونی.
✍️ درباره نویسندگان:
والیاپا (لاک) لاکشمانان با تیمهای مدیریتی شرکتهای مختلف همکاری نزدیک داره تا از داده و نوآوری مبتنی بر هوش مصنوعی برای رشد کسبوکارشون استفاده کنن. قبلاً مدیر بخش تحلیل داده و راهحلهای هوش مصنوعی در گوگل کلود و پژوهشگر علمی در NOAA بوده. همبنیانگذار آزمایشگاه راهحلهای پیشرفته گوگل هست و نویسنده چندین کتاب و دوره آموزشی در انتشارات اُرایلی و کورسرا. به خاطر پیشگامی در الگوریتمهای یادگیری ماشین برای پیشبینی آبوهوای شدید، به عنوان Fellow انجمن هواشناسی آمریکا انتخاب شده (بالاترین افتخار این انجمن).
هانس هاپکه مهندس ارشد یادگیری ماشین در شرکت Digits هست و چندین مقاله و کتاب در حوزه یادگیری ماشین نوشته، از جمله «ساخت خطوط لوله یادگیری ماشین» و «سیستمهای تولیدی یادگیری ماشین» از انتشارات اُرایلی. کارهاش رو در کنفرانسهایی مثل ODSC و TensorFlow World ارائه داده و contributor فعال پروژه TFX Addons تنسورفلو هست. علاقه اصلیش مهندسی یادگیری ماشین و استفاده از ماشین لرنینگ در محیط production با جدیدترین پیشرفتهاست.
Generative AI enables powerful new capabilities, but they come with some serious limitations that you'll have to tackle to ship a reliable application or agent. Luckily, experts in the field have compiled a library of 32 tried-and-true design patterns to address the challenges you're likely to encounter when building applications using LLMs, such as hallucinations, nondeterministic responses, and knowledge cutoffs.
This book codifies research and real-world experience into advice you can incorporate into your projects. Each pattern describes a problem, shows a proven way to solve it with a fully coded example, and discusses trade-offs.
Table of Contents
1. Introduction
2. Controlling Content Style
3. Adding Knowledge: Bass
4. Adding Knowledge: Syncopation
5. Extending Model Capabilities
6. Improving Reliability
7. Enabling Agents to Take Action
8. Addressing Constraints
9. Setting Safeguards
10. Composable Agentic Workflows
From the Preface:
If you’re an AI engineer building generative AI (GenAI) applications, you’ve likely experienced the frustrating gap between the ease of creating impressive prototypes and the complexity of deploying them reliably in production. While foundational models make it easy to build compelling demos, production systems demand solutions to fundamental challenges: hallucinations that compromise accuracy, inconsistent outputs that break downstream processes, knowledge gaps that limit enterprise applicability, and reliability issues that make systems unsuitable for critical applications.
This book bridges that gap by providing 32 battle-tested design patterns that address the recurring problems you’ll encounter when building production-grade GenAI applications. These patterns aren’t theoretical constructs—they codify proven solutions that are often derived from cutting-edge research and refined by practitioners who have successfully deployed GenAI systems at scale.
This book bridges that gap by providing 32 battle-tested design patterns that address the recurring problems you’ll encounter when building production-grade GenAI applications. These patterns aren’t theoretical constructs—they codify proven solutions that are often derived from cutting-edge research and refined by practitioners who have successfully deployed GenAI systems at scale.
Supervised machine learning (ML) involves training a problem-specific model on a large training dataset of example inputs and outputs—but GenAI applications rarely include a training phase. Instead, they commonly use general-purpose foundational models. This book is focused on design patterns for AI applications that are built on top of foundational models, such as Open AI’s GPT, Anthropic’s Claude, Google’s Gemini, or Meta’s Llama.
Who Is This Book For?
This book is for software engineers, data scientists, and enterprise architects who are building applications powered by GenAI foundational models. It captures proven solutions you can employ to solve the common challenges that arise when building GenAI applications and agents. Read it to learn how experts in the field are handling challenges such as hallucinations, nondeterministic answers, knowledge cutoffs, and the need to customize a model for your industry or enterprise. The age-old problems of software engineering have new solutions in this realm. For example, ways to meet latency and constrain costs include distillation, speculative decoding, prompt caching, and template generation.
Understanding the different patterns in this book requires different levels of background knowledge. For example, Chain of Thought (Pattern 13) requires no more than a knowledge of basic programming, Tool Calling (Pattern 21) requires an understanding of API design, and Dependency Injection (Pattern 19) requires some experience developing large-scale software. However, Content Optimization (Pattern 5) requires familiarity with statistics and ML, and Small Language Model (Pattern 24) requires an understanding of hardware optimization. We expect that 75% of the book can be read and understood by a junior software engineer or a third-year computer science student. The remainder will require specialized knowledge or experience.
AI engineering overlaps heavily with software engineering, data engineering, and ML—but in this book, we’ve limited our focus to core AI engineering. We encourage you to think of this book as a companion to the literature on patterns in related areas. Specifically, the book Machine Learning Design Patterns (O’Reilly), also co-authored by Valliappa Lakshmanan, covers proven solutions to recurring issues you’ll encounter when training a bespoke machine-learning model for a specific problem.
You’ll also likely find yourself working with both bespoke ML models and general-purpose foundational models, depending on the use case. In some situations, you might start with a foundational model but then find that edge cases require you to customize (or fine-tune) it for your problem. This book and Machine Learning Design Patterns are complementary and will help you work with both models, so we recommend that you read both.
Valliappa (Lak) Lakshmanan works closely with management teams across a range of enterprises to help them employ data and AI-driven innovation to grow their businesses. Previously, he was the Director for Data Analytics and AI Solutions on Google Cloud and a Research Scientist at NOAA. He co-founded Google's Advanced Solutions Lab and is the author of several O'Reilly books and Coursera courses. He was elected a Fellow of the American Meteorological Society (the highest honor offered by the AMS) for pioneering machine learning algorithms in severe weather prediction.
Hannes Hapke is a Senior Machine Learning Engineer at Digits, and has co-authored multiple machine learning publications, including the book Building Machine Learning Pipelines and Machine Learning Production Systems by O'Reilly Media. He has also presented state-of-the-art ML work at conferences like ODSC or O’Reilly’s TensorFlow World and is an active contributor to TensorFlow's TFX Addons project. Hannes is passionate about machine learning engineering and production machine learning use cases using the latest machine learning developments.









