AI guide
# Principles of Building AI Agents — Reading Guide
## 【One-Line Pitch】
A practical, hype-free handbook for developers who want to move beyond chatbot demos and build production-grade AI agents—covering everything from prompt engineering and agent architecture to graph-based workflows and RAG. Written by the founder of the Mastra framework, it's ideal for JavaScript/TypeScript developers and technical teams shipping real AI features.
## 【Book Arc】
- **Opening (~0%–12%)**: Sets the stage with the author's Y Combinator experience building Mastra, then introduces the core building blocks—providers (OpenAI, Anthropic, Google, Meta), model selection trade-offs (size vs. cost/latency), and context window considerations.
- **Early (~12%–29%)**: Dives into prompt engineering fundamentals (zero-shot, single-shot, few-shot techniques, formatting tricks) and agent basics—levels of autonomy, structured output, tool design best practices, and memory systems including hierarchical memory patterns.
- **Middle (~29%–47%)**: Covers dynamic agents (runtime configuration), middleware and guardrails (prompt injection defense), popular third-party tools (web scraping, search APIs, integrations), and the Model Context Protocol (MCP) ecosystem for connecting agents to external tools.
- **Middle (~47%–59%)**: Introduces graph-based workflows as a more predictable alternative to free-form agents—branching, chaining, merging, conditions, suspend/resume, streaming updates, and observability/tracing with OpenTelemetry.
- **Late (~59%–end)**: Explores Retrieval-Augmented Generation (RAG)—vector databases, embeddings, indexing, querying with cosine similarity, reranking, and synthesis—plus multimodal capabilities, code generation, and future directions.
## 【Key Takeaways】
- **Model selection is a cost-accuracy-latency trade-off** (Early): Start with more expensive models when prototyping, then optimize down once things work. Context window size matters—Gemini Flash's 2M token window enables feeding entire codebases to a model.
- **Prompt engineering has a spectrum from zero-shot to few-shot** (Early): More examples give more precise control. Production prompts are surprisingly detailed—study real examples like bolt.new's code-generation prompt rather than writing minimal instructions.
- **Agents operate at different autonomy levels** (Early): Low-level agents make binary decisions in a tree; medium-level agents have memory, call tools, and retry failures; high-level agents plan and divide tasks. Most deployed agents today are low-to-medium autonomy.
- **Think like an analyst when designing tools** (Early): Break problems into clear, reusable operations and write each as a tool. Provide detailed descriptions, specific input/output schemas, and semantic naming (multiplyNumbers, not doStuff).
- **Memory systems should be hierarchical** (Early): Combine recent messages with relevant long-term memories retrieved on demand—not everything goes into the context window. Filter tool calls from memory to save tokens.
- **Guardrails are essential for production agents** (Middle): Input sanitization defends against prompt injection, jailbreaking, PII requests, and off-topic chats that rack up LLM bills. Security through obscurity no longer works when agents can retrieve hidden knowledge.
- **Graph-based workflows beat free-form agents for predictability** (Middle): When agents have too much freedom, define decision trees with branching, chaining, and merging. Keep steps meaningful for tracing, and limit each step to one LLM call.
- **RAG follows a pipeline: index, query, rerank, synthesize** (Late): Use vector embeddings and cosine similarity for retrieval. For vector DBs, prefer pgvector if you're on Postgres, Pinecone for new projects, or your cloud provider's managed service—avoid infrastructure sprawl.
## 【Reading Tips】
- **Skim Part I if you're already prompting LLMs daily** (~0%–12%): The provider comparison and prompt basics are useful refreshers, but the real value starts with agent architecture in Part II.
- **Deep-read the agent building blocks section** (~12%–29%): Tool design, memory patterns, and structured output are the foundation everything else builds on. The Mastra code examples are worth studying even if you use a different framework.
- **Pay special attention to the MCP chapter** (~41%–47%): This is where the ecosystem is standardizing. Understanding when to build an MCP client vs. server will save you months of integration work.
- **The workflow section rewards careful reading** (~47%–59%): The branching/chaining/merging patterns are the most transferable concepts—they apply regardless of which LLM framework you choose.
- **Skim the RAG section if you've done vector search before** (~59%+): The pipeline is standard, but the practical advice on vector DB selection and avoiding infrastructure sprawl is worth noting.
## 【Coverage Limits】
This guide covers the first ~59% of the book in detail (prompting, agents, workflows, and RAG fundamentals). The later sections on multimodal capabilities, code generation, and future directions are only briefly mentioned in the table of contents and not covered in the available excerpts.
##
Passage locations
Page 4
vs cost/latency 6 Context window size 6 Reasoning models 7 Providers and models (May 2025) 8 3. WRITING GREAT PROMPTS 9 Give the LLM more examples 9 A “seed...
View in text
Excerpt 2
le start with more expensive models when prototyping — once you get something working, you can tweak cost. Context window size One variable you may want to t...
View in text
Excerpt 3
ming out. Input sanitization tries broadly to guard against “prompt injection” attacks. These include model “jailbreaking” (“IGNORE PREVIOUS INSTRUCTIONS AND...
View in text
Page 14
s too much freedom. Graph-based workflows have emerged as a useful technique for building with LLMs when agents don’t deliver predictable enough output. Some...
View in text