Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quick Reference

This chapter consolidates key equations, architecture specifications, API references, and failure mode diagnostics for rapid lookup during development and debugging.

Core RL & Alignment Equations

\[ \begin{aligned} \text{PPO Clip:}&\quad L = \mathbb{E}[\min(r_t\hat{A}_t, \text{clip}(r_t,1{\pm}\epsilon)\hat{A}_t)], \quad r_t = \pi_\theta(a_t|s_t)/\pi_{\text{old}}(a_t|s_t) \\ \text{DPO:}&\quad L = -\mathbb{E}[\log\sigma(\beta\log\tfrac{\pi_\theta(y_w|x)}{\pi_\text{ref}(y_w|x)} - \beta\log\tfrac{\pi_\theta(y_l|x)}{\pi_\text{ref}(y_l|x)})] \\ \text{GRPO:}&\quad \hat{A}_i = (r_i - \mu_G)/\sigma_G, \quad \text{then PPO clip update (no critic)} \\ \text{KTO:}&\quad L = \lambda_w(1 - v(y_w)) + \lambda_l \cdot v(y_l), \quad v = \sigma(\beta\log(\pi_\theta/\pi_\text{ref}) - z) \\ \text{IPO:}&\quad L = \mathbb{E}[(\log(\pi_\theta(y_w)/\pi_\text{ref}(y_w)) - \log(\pi_\theta(y_l)/\pi_\text{ref}(y_l)) - 1/(2\beta))^2] \\ \text{ORPO:}&\quad L = L_\text{SFT}(y_w) - \lambda\log\sigma(\log(\text{odds}(y_w)/\text{odds}(y_l))) \\ \text{GAE:}&\quad \hat{A}_t = \textstyle\sum_{l=0}^{T-t}(\gamma\lambda)^l\delta_{t+l}, \quad \delta_t = r_t + \gamma V(s_{t+1}) - V(s_t) \\ \text{KL Penalty:}&\quad R_\text{total} = r_\phi(x,y) - \beta D_\text{KL}[\pi_\theta(y|x)|\pi_\text{ref}(y|x)] \\ \text{RM (Bradley-Terry):}&\quad L = -\mathbb{E}[\log\sigma(r_\phi(x,y_w)-r_\phi(x,y_l))] \\ \text{Best-of-N:}&\quad y^* = \arg\max_{y_i \sim \pi_\theta(\cdot|x),, i=1..N} r_\phi(x, y_i) \end{aligned} \]

Transformer & Architecture Formulas

\[ \begin{aligned} \text{Self-Attention:}&\quad \text{Attn}(Q,K,V) = \text{softmax}(QK^\top / \sqrt{d_k}) \cdot V \\ \text{Multi-Head:}&\quad \text{MHA}(X) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h)W^O,\quad \text{head}_i = \text{Attn}(XW_i^Q, XW_i^K, XW_i^V) \\ \text{RoPE:}&\quad f(x_m, m) = x_m e^{im\theta_j}, \quad \theta_j = 10000^{-2j/d} \\ \text{LoRA:}&\quad W' = W_0 + (\alpha/r) \cdot BA, \quad B \in \mathbb{R}^{d \times r},; A \in \mathbb{R}^{r \times k} \\ \text{KD (soft targets):}&\quad L_\text{KD} = (1{-}\alpha)L_\text{CE}(y, \hat{y}) + \alpha, T^2 \cdot \text{KL}(p_T^\text{teacher} | p_T^\text{student}) \\ \text{FFN (SwiGLU):}&\quad \text{FFN}(x) = (\text{Swish}(xW_1) \odot xW_3) W_2 \end{aligned} \]

Decoding Methods

MethodFormula / RuleKey Param
Greedy\(y_t = \arg\max_v P(v\vert y_{<t})\)
Beam searchKeep top-\(B\) partial sequences by joint probability\(B=4\)–\(8\)
Temperature\(P'(v) = \text{softmax}(\text{logit}_v / T)\)\(T \in [0.1, 1.5]\)
Top-\(k\)Zero out all but top-\(k\) logits, renormalize\(k=40\)–\(100\)
Top-\(p\) (nucleus)Keep smallest set \(V'\) s.t. \(\sum_{v \in V'} P(v) \geq p\)\(p=0.9\)–\(0.95\)
Min-\(p\)Keep tokens with \(P(v) \geq p_\text{min} \cdot P(v_\text{max})\)\(p_\text{min}=0.05\)–\(0.1\)
Repetition penalty\(\text{logit}_v \leftarrow \text{logit}_v / \theta\) if \(v\) appeared before\(\theta=1.1\)–\(1.3\)

Systems & Parallelism

FormulaValue (70B, BF16)Description
Model memory\(2P\) bytes\(140\) GB (weights only)
Adam optimizer\(2P \times 4\) bytes (m + v)\(280\) GB
Full training footprint\(\sim 8P\) bytes\(560\) GB (weights + opt + grad)
FSDP memory/GPU\(8P / N_\text{GPUs}\)\(70\) GB with 8 GPUs
Gen arithmetic intensity\(2P / 2P = 1\) FLOP/byteHeavily memory-bound
Token rate (gen)HBM_BW \(/ (2P)\)\(\sim\)14 tok/s (A100, batch=1)
TP AllReduce / layer\(2 \times 2 \cdot \frac{T-1}{T} \cdot bsd\) bytes\(\sim\)188 MB (70B, TP=8)
PP bubble fraction\((P-1)/(P+M-1)\)\(P\)=stages, \(M\)=micro-batches
MFUobserved_toks \(\times\) 6\(P\) / peak_FLOPSTarget: \(>40%\)

GPU Hardware Specs

GPUMemoryBW (HBM)BF16 TFLOPSNVLinkNotes
A100-80GB80 GB HBM2e2.0 TB/s312600 GB/sWorkhorse, widely available
H100-80GB80 GB HBM33.35 TB/s989900 GB/sCurrent gen, FP8 support
H200-141GB141 GB HBM3e4.8 TB/s989900 GB/sLarge context / fewer GPUs
B200192 GB HBM3e8.0 TB/s22501800 GB/sNext gen (2025)

Hyperparameter Ranges

ParameterTypical RangeDefaultNotes
\(\beta\) (DPO/KTO)0.05–0.50.1Higher = more conservative
\(\epsilon\) (PPO clip)0.1–0.30.2Higher = more aggressive updates
\(\gamma\) (GAE discount)0.99–1.01.0Use 1.0 for episodic tasks
\(\lambda\) (GAE)0.9–0.990.95Lower = more biased, less variance
KL coeff (\(\beta_\text{KL}\))0.01–0.20.05Auto-adapt to target KL \(\approx\) 5–8
LR (RLHF)1e-7 – 5e-65e-7Much lower than pre-training
LR (SFT)1e-5 – 5e-52e-5Standard fine-tuning range
LoRA rank \(r\)8–12816–64Higher \(r\) = more capacity, more memory
LoRA alpha \(\alpha\)\(r\) – \(2r\)\(2r\)Scaling factor; \(\alpha/r\) is the effective scale
Temperature (gen)0.6–1.00.7Lower = less diverse candidates
Num generations \(K\)4–644–16For GRPO/Online DPO/Best-of-N
Grad clip norm0.5–2.01.0Prevents gradient explosion

TRL API Quick Reference

TrainerMethodKey ConfigData Format
SFTTrainerSupervised FTpacking, max_seq_lengthprompt + completion
RewardTrainerReward modelcenter_rewards_coefficientprompt + chosen + rejected
PPOTrainerPPOinit_kl_coef, target_kl, cliprangeprompts (online gen)
DPOTrainerDPO/IPObeta, loss_type="sigmoid"/"ipo"prompt + chosen + rejected
GRPOTrainerGRPOnum_generations, beta, use_vllmprompts + reward_fn
OnlineDPOTrainerOnline DPOnum_generations, reward_model_pathprompts (online gen)
KTOTrainerKTOdesirable_weight, undesirable_weightprompt + completion + label
ORPOTrainerORPObetaprompt + chosen + rejected
Best-of-N (manual)Best-of-Nn_samplesprompts (inference)

RAG Pipeline Formulas

\[ \begin{aligned} \text{Cosine similarity:}&\quad \text{sim}(q, d) = \frac{q \cdot d}{|q| \cdot |d|} \\ \text{Retrieval:}&\quad \mathcal{D}_k = \text{top-}k_{d \in \mathcal{C}} ; \text{sim}(\text{embed}(q),; \text{embed}(d)) \\ \text{RAG generation:}&\quad P(y|q) = P_\text{LLM}(y ;|; q, \mathcal{D}_k) \\ \text{Chunking overlap:}&\quad \text{stride} = \text{chunk_size} - \text{overlap} \\ \text{Reranker (cross-enc):}&\quad \text{score}(q, d) = \text{MLP}(\text{BERT}([q; d])) \end{aligned} \]

Agentic Design Patterns

PatternStructureBest For
ReActThink \(\to\) Act \(\to\) Observe \(\to\) loopGeneral tool-use agents
Plan-and-ExecutePlan \(\to\) Execute steps \(\to\) ReviseLong-horizon, structured tasks
SupervisorRouter \(\to\) specialist agentsMulti-domain, clear subtask boundaries
Swarm (handoffs)Agent transfers control + contextCustomer service, escalation flows
HierarchicalTree of delegating agentsComplex decomposition
Human-in-the-loopAgent \(\to\) Approval gate \(\to\) ContinueHigh-stakes, irreversible actions

Agent Communication Protocols

ProtocolScopeTransportKey Concept
MCPTool integrationstdio / HTTP+SSEServer exposes tools; client discovers & calls
A2AAgent-to-agentHTTP + JSON-RPCTasks with lifecycle (submitted\(\to\)working\(\to\)done)
OpenAI Function CallingTool useAPI payloadJSON schema in tools[] array

Context Window Budget

\[ C \geq \underbrace{S}_{\text{system}} + \underbrace{M}_{\text{memory/RAG}} + \underbrace{T}_{\text{tool defs}} + \underbrace{H}_{\text{history}} + \underbrace{R}_{\text{reserved output}} \]

Rule of thumb for 128K context:

  • System prompt: 1–4K tokens (fixed)

  • Tool definitions: 2–8K (scales with # tools)

  • RAG context: 4–16K (top-\(k\) chunks)

  • History: grows unbounded \(\rightarrow\) summarize/truncate

  • Reserved output: 2–8K

Common Failure Modes & Fixes

Method Selection Decision Tree

  1. Have paired preferences (chosen + rejected)?

    • Noisy labels \(\rightarrow\) IPO

    • Memory-constrained, no SFT done yet \(\rightarrow\) ORPO

    • Clean data, limited compute \(\rightarrow\) DPO

    • DPO plateaus, want exploration \(\rightarrow\) Online DPO

  2. Have only binary feedback (thumbs up/down)? \(\rightarrow\) KTO

  3. Have verifiable rewards (math/code)? \(\rightarrow\) GRPO

  4. Need maximum quality, any cost? \(\rightarrow\) PPO

  5. Want training-free improvement? \(\rightarrow\) Best-of-N

Evaluation Metrics

MetricRangeWhat It Measures
Perplexity\([1, \infty)\)Model’s surprise; lower = better language modeling
Win Rate (vs. baseline)\([0, 1]\)Fraction of outputs preferred by judge/human
BLEU\([0, 1]\)\(n\)-gram overlap with reference (precision-focused)
ROUGE-L\([0, 1]\)Longest common subsequence with reference
Pass@\(k\)\([0, 1]\)Probability \(\geq\)1 of \(k\) code samples passes tests
MMLU / GPQA\([0, 1]\)Multi-choice accuracy on knowledge/reasoning benchmarks
HumanEval\([0, 1]\)Functional correctness of generated code
Faithfulness (RAG)\([0, 1]\)Fraction of claims supported by retrieved context
Context Relevancy\([0, 1]\)Fraction of retrieved content relevant to query
Answer Relevancy\([0, 1]\)Degree to which answer addresses the question

Reasoning & Test-Time Scaling

MethodCompute CostMechanism
Chain-of-Thought (CoT)1.5–3\(\times\) tokens“Think step by step” in prompt
Self-Consistency\(N \times\) generationSample \(N\) CoT paths, majority vote on final answer
Tree-of-Thought (ToT)\(B \times D \times\) generationBFS/DFS over reasoning tree; evaluate branches
Best-of-\(N\)\(N \times\) generationSample \(N\), score with RM, pick highest
Beam search (on reasoning)\(B \times\) generationMaintain top-\(B\) partial reasoning chains
Budget forcingVariableAllocate more tokens to harder problems dynamically
Verification (ORM/PRM)\(N \times\) gen + scoringGenerate \(N\) solutions, rank by outcome/process RM

Memory System Types

TypeStorageUse Case
Working memoryContext windowCurrent conversation, immediate tool results
Episodic memoryVector storePast interactions, user preferences, session history
Semantic memoryKnowledge graph / embeddingsFacts, concepts, domain knowledge
Procedural memorySkill library / codeHow-to procedures, learned workflows

MCP Quick Reference

PrimitiveDirectionSide Effects?Purpose
ToolsClient \(\to\) ServerYesExecute actions (create, modify, delete)
ResourcesClient \(\to\) ServerNo (read-only)Read data (files, DB records, configs)
PromptsClient \(\to\) ServerNoReusable templates for common tasks
SamplingServer \(\to\) ClientNoServer requests LLM generation from client

Transport: stdio (local subprocess) or HTTP+SSE (remote, streamable).
Discovery: Client calls tools/list, resources/list, prompts/list at connection init.
Tool annotations: readOnlyHint, destructiveHint, idempotentHint, openWorldHint.

A2A Protocol Quick Reference

ConceptDescription
Agent CardJSON at /.well-known/agent.json — name, skills, supported content types
TaskUnit of work: id, status, artifacts. Lifecycle: submitted \(\to\) working \(\to\) completed/failed
MessageCommunication unit within a task (role: user/agent, parts: text/file/data)
ArtifactOutput produced by the agent (structured data, files, generated content)
Push NotificationsWebhook-based updates for long-running tasks (via tasks/pushNotification/set)

Key endpoints: tasks/send (create/update), tasks/get (poll status), tasks/sendSubscribe (SSE stream).

Agent Framework Comparison

FrameworkOrchestrationMulti-AgentBest For
LangGraphExplicit state graphConditional routingProduction: persistence, HITL, fine control
OpenAI Agents SDKDeclarative handoffsHandoff-basedSimplicity: guardrails, tracing, fast start
AutoGen (AG2)Conversation-drivenGroupChatPrototyping: code execution, research
CrewAIRole-based teamsSequential/parallelLow-code: quick demos, simple pipelines
Google ADKSession + eventsA2A nativeEnterprise: artifact mgmt, multi-modal

Agentic RL Formulas

\[ \begin{aligned} \text{Trajectory GRPO:}&\quad \hat{A}_i = (R(\tau_i) - \mu_G)/\sigma_G, \quad R(\tau_i) = \sum_{t} r_t^{(\tau_i)} \\ \text{Agent reward:}&\quad R = w_1 R_\text{task} + w_2 R_\text{efficiency} + w_3 R_\text{safety}, \quad R_\text{eff} = \max(0, 1 - \text{steps}/N_\text{max}) \\ \text{Masking:}&\quad \mathcal{L} = \sum_{t \in \text{agent tokens}} \min(r_t \hat{A}_t,; \text{clip}(r_t) \hat{A}_t) \quad \text{(mask env outputs)} \\ \text{Pass@}k:&\quad 1 - \frac{\binom{n-c}{k}}{\binom{n}{k}}, \quad n = \text{total samples},; c = \text{correct} \end{aligned} \]

Agent Security Checklist

ThreatLayerMitigation
Prompt injection (direct)InputInput validation, instruction hierarchy, delimiters
Prompt injection (indirect)Tool outputTreat tool output as untrusted; don’t follow instructions in retrieved docs
Tool misuseExecutionLeast-privilege permissions; destructiveHint gates; sandboxing
Data exfiltrationOutputOutput filtering; restrict tool access to allowed domains
Excessive autonomyArchitectureMax iterations; cost budgets; human approval gates
Confused deputyMulti-agentVerify task origin; capability-based access control

Agent Evaluation Metrics

MetricFormula / DefinitionTarget
Task Success Rate (TSR)Correct completions / total tasks\(>85%\) (production)
Steps to completionAvg agent actions per successful taskLower = more efficient
Cost per taskTotal tokens \(\times\) price/tokenBudget-dependent
Latency (TTFC)Time from request to first useful output\(<5\)s for interactive
Tool call accuracyCorrect tool selections / total calls\(>90%\)
Recovery rateSuccessful retries / initial failures\(>60%\)
Human escalation rateTasks requiring human / total tasks\(<15%\)

Key Agentic Benchmarks

BenchmarkDomainMetricSOTA (2025)
SWE-bench VerifiedSoftware engineering% resolved issues\(\sim\)70%
WebArenaWeb browsingTask success rate\(\sim\)40%
OSWorldDesktop computer useTask success rate\(\sim\)25%
GAIAGeneral AI assistantExact match accuracy\(\sim\)75% (L1)
Tau-benchTool-use reliabilityPass rate (5 trials)\(\sim\)65%
HumanEval / MBPPCode generationPass@1\(>95%\)