A Code-First Guide to RAG, Agents, Guardrails, And Evals
A comprehensive guide to building production AI applications using LangChain, LangGraph, and Python. This book is designed for Python-aware graduates and working professionals looking to master AI engineering.
Updated for 2026: All content reflects LangChain 1.0, LangGraph 1.0, latest industry trends, and production best practices including OWASP Top 10 for LLM Apps, NIST AI RMF, and EU AI Act compliance.
Chapter
Title
Description
-
A Letter from the Author
A personal note on the transition from prompts to agentic systems
0
First Things First
Setting up the environment, API keys, and understanding the code-first approach
1
The Non-Deterministic Revolution
Determinism vs non-determinism, LLM internals, transformers, why AI engineering is different
2
Prompt Engineering with LangChain
19 prompt patterns from zero-shot to self-reflection, plus production implementations
3
LCEL and Runnables
Chain composition, branching, parallelism, reliability patterns, and runtime configuration
4
Context Engineering
Context strategies, memory management, and token budgeting for production systems
5
Retrieval-Augmented Generation (RAG)
Chunking, vector stores, retrieval strategies, hybrid search, grounded generation, RAG architectures reference, and vector databases deep dive
6
Agents and Tool Calling
Tools, agent loops, human-in-the-loop patterns, and LangGraph orchestration
7
Agent Safety & Guardrails
Input/output guardrails, prompt injection defense, PII detection, and compliance
8
Model Context Protocol (MCP)
Standardized tool integration via MCP servers/clients
9
Evaluations
Measuring quality with eval datasets, metrics, LLM-as-judge, and A/B testing
10
LangGraph
Persistence, streaming, interrupts, time travel, memory store, and subgraphs
11
Multi-Agent Systems
Orchestration patterns (Supervisor, Hierarchical, Network), Agentic RAG, and A2A protocol
12
Deep Agents
Planning with todos, context management, subagent spawning, and long-term memory
13
Observability
MELT pillars, token tracking, latency metrics, LangSmith integration, cost optimization
14
Production Systems and Ops
Architecture, reliability patterns, observability, and day-2 operations for production agents
15
The Present and Future of Agents
Integrations (Canva/Coursera), SLMs, Agentic Thinking, and the 2030 Agentic Economy
16
Interview Preparation
35+ tactical questions, system design, and practical AI engineering scenarios
Appendix
Title
Description
A
RAG Architectures
Reference architectures for production RAG systems
B
Knowledge Graphs
Neo4j integration, Cypher queries, GraphRAG, and knowledge graph agents
C
Vector Databases
Deep dive into vector storage, indexing, and retrieval mechanics
LLM Fundamentals : Non-determinism, transformers, sampling strategies, context windows
Prompt Engineering : 19 patterns including CoT, ToT, GoT, ReAct, self-consistency
RAG Systems : Chunking, embeddings, vector stores, hybrid search, reranking
Agent Development : Tool calling, agent loops, LangGraph workflows, human-in-the-loop
Production Concerns : Safety guardrails, observability, evaluation, cost optimization
Advanced Topics : Multi-agent systems, deep agents, knowledge graphs
Python 3.11+
UV package manager
# Install UV
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone and setup
git clone https://github.com/PythonGuruGlobal/ai-engineering-in-python-code.git
cd ai-engineering-in-python-code
uv sync
# Configure API key
cp .env.example .env
# Edit .env and add your GOOGLE_API_KEY (or create `.env` manually)
All code examples are located in the code/ directory. Each example is standalone and includes a description at the top of the file.
Run any specific example using uv:
uv run python code/example_01_basic_invoke.py
Chapter 1: Non-Deterministic Revolution
Example
Description
example_01_basic_invoke.py
Basic LLM invocation
example_01_b_basic_chain.py
Your First Chain (LCEL)
Chapter 2: Prompt Engineering Patterns
Example
Description
example_02_temperature.py
Temperature effects on output
example_03_system_messages.py
System messages and personas
example_04_zero_shot.py
Zero-shot classification
example_05_few_shot.py
Few-shot learning
example_06_structured_output.py
Pydantic structured outputs
example_07_chain_of_thought.py
CoT reasoning
example_08_tree_of_thought.py
ToT exploration
example_09_graph_of_thought.py
GoT dependencies
example_10_step_back.py
Step-back prompting
example_11_least_to_most.py
Least-to-most prompting
example_12_program_of_thought.py
Program-of-thought prompting
example_13_self_consistency.py
Self-consistency sampling
example_14_chain_of_verification.py
Chain-of-verification
example_15_self_reflection.py
Draft-critique-revise
example_16_negative_prompting.py
Negative prompting / constraints
example_17_contrastive.py
Contrastive prompting
example_18_skeleton_of_thought.py
Skeleton-of-thought prompting
example_19_react.py
ReAct prompting
Chapter 3: LCEL and Runnables
Example
Description
example_20_chain_composition.py
Basic pipe operator composition
example_21_passthrough.py
RunnablePassthrough for data enrichment
example_22_parallel.py
RunnableParallel for concurrent execution
example_23_lambda.py
RunnableLambda for custom functions
example_24_branch.py
RunnableBranch for conditional routing
example_25_reliability.py
Retries, fallbacks, streaming, batching
example_26_itemgetter.py
itemgetter pattern for clean inputs
example_27_bind.py
.bind() for runtime configuration
example_28_configurable.py
Configurable fields for runtime flexibility
Chapter 4: Context Engineering
Example
Description
example_29_context_budget.py
Token budget management
example_30_selective_retrieval.py
Filtering context by relevance
example_31_memory_window.py
Windowed conversation memory
example_31b_context_chat_app.py
Context-Aware Chat Application
Example
Description
example_32_rag_basic.py
Basic RAG pipeline
example_33_rag_chroma.py
RAG with ChromaDB (production pattern)
example_34_hybrid_retrieval.py
Hybrid retrieval (BM25 + vectors)
example_35_code_rag.py
Codebase RAG
example_36_reranking.py
Reranking for retrieval quality
example_36b_rag_personal_assistant.py
Personal Documentation Assistant (Complete RAG)
Example
Description
example_37_tools.py
Defining and binding tools
example_38_agent_loop.py
Basic agent loop
example_39_langgraph.py
LangGraph agent with tools
example_40_human_in_loop.py
Human-in-the-loop patterns
example_41_dynamic_agents.py
Dynamic agents that adapt at runtime
Example
Description
example_42_guardrails.py
Individual guardrail implementations
example_43_unified_guardrails.py
Unified guardrail system
example_44_middleware.py
Middleware stack with auth + guardrails
Example
Description
example_45_mcp_server.py
Building an MCP server
example_46_mcp_client.py
Consuming MCP tools in a client
example_46b_mcp_mysql_server.py
MySQL Employee Database MCP Server
Example
Description
example_47_textual_evals.py
Textual quality evaluation
example_48_classification_evals.py
Classification metrics
Example
Description
example_49_langgraph_persistence.py
Persistence and checkpointing
example_50_langgraph_streaming.py
Streaming patterns
example_51_langgraph_interrupts.py
Interrupts and human-in-the-loop
example_52_langgraph_time_travel.py
Time travel and debugging
example_53_langgraph_memory_store.py
Cross-session memory (Store)
example_54_langgraph_subgraphs.py
Subgraph composition
Chapter 11: Multi-Agent Systems
Example
Description
example_55_multi_agent_patterns.py
Multi-agent orchestration patterns
Example
Description
example_56_deep_agent_basic.py
Deep-agent planning patterns
example_57_deep_agent_subagents.py
Spawning and coordinating subagents
Chapter 13: Observability
Example
Description
example_58_observability.py
Token tracking, latency metrics, cost optimization
Chapter 14: Production Systems and Ops
Example
Description
example_59_production_safeguards.py
Production Safeguards (Control & Perception Planes)
example_60_production_reliability.py
Production Reliability (Circuit Breakers & Gateways)
LLM : Google Gemini 2.5 (Pro/Flash)
Framework : LangChain (v1.x), LangGraph (v1.x) (see pyproject.toml for exact constraints)
Vector Store : ChromaDB
Graph Database : Neo4j (optional, for Appendix B)
Package Manager : UV
Python : 3.11+
AI Engineering = Input Engineering + Output Engineering
Before the LLM (Input Engineering):
Prompt Engineering
System Messages
Context Injection
RAG
Few-Shot Examples
Input Guardrails
After the LLM (Output Engineering):
Parsing
Validation
Tool Calling
Output Guardrails
Chaining
Six Sources of LLM Non-Determinism
Token sampling (temperature)
Decoding algorithms (top-k, top-p)
Agent loop variability
External tool variance
Hardware non-determinism
Prompt sensitivity
Each major chapter includes a comprehensive Best Practices: Do's and Don'ts section covering:
Prompt Engineering : System messages, few-shot examples, structured output, CoT patterns, debugging
RAG : Chunking strategies, retrieval optimization, generation quality, security (prompt injection defense), performance tuning, evaluation metrics
Agents : Tool design principles, agent loops, autonomy levels, memory management, security best practices
Guardrails : Defense-in-depth strategies, prompt injection detection, PII handling, compliance (HIPAA, PCI-DSS, GDPR), behavioral controls
LangGraph : State design patterns, node composition, persistence strategies, streaming modes, error handling
Multi-Agent : Specialization patterns, orchestration (Supervisor/Hierarchical/Network), context management, scaling considerations
Observability : MELT pillars (Metrics, Events, Logs, Traces), token tracking, latency optimization, LangSmith integration, cost management
Evaluation : Dataset construction, metric selection, LLM-as-judge patterns, A/B testing, human evaluation workflows
Chapter 16 provides comprehensive interview preparation with 35+ hands-on questions in the style real interviewers use:
Core Concepts : LLM fundamentals, prompt engineering, RAG systems, agents
Practical Scenarios : "Your PM asks why...", "How do you debug...", "Walk me through..."
System Design : Customer service bots, document processing pipelines, code generation systems
Coding Challenges : RAG chains, agent loops, guardrail implementations
Behavioral Questions : STAR format examples for debugging stories and staying current
Quick Reference : Key numbers, trade-offs, and red flags to avoid
Preparation Checklist : Final steps before your interview
All answers are concise (5-15 lines) and demonstrate production-ready thinking.
MIT