Principles of Building AI Agents

Principles of Building AI Agents is the world's leading guide to getting started with AI agents. Written by Sam Bhagwat, CEO of Mastra, it covers the core concepts, tools and patterns developers need to build agents that reason, use tools, manage memory and orchestrate workflows in production.

  • Agents
  • Memory
  • Workflows
  • RAG
  • Tools
  • Traces

The Complete Handbook for Building Production AI Agents

Principles of Building AI Agents displayed on an event.

189,500 Books Printed

Principles of Building AI Agents has been distributed to people across the globe.

Patterns of Building AI Agents stacked on a shelf.

Patterns

Explore the second book in agentic series that builds on Principles

Sam Bhagwat introducing Principles of Building AI Agents.

Sam Bhagwat

Sam Bhagwat is the CEO of Mastra and the author of Principles of Building AI Agents. Watch him go over a few chapters of his book.

Read and shared worldwide.

What's inside?

Principles of Building AI Agents gives you a clear overview of how modern AI agents work and the core concepts, tools and patterns you will use to build real agentic systems. The book opens with LLM fundamentals and prompt engineering before moving into agents, tool calling, memory, workflows and RAG. Later chapters cover multi-agent systems, observability and evals, deployment, coding agents and multimodal capabilities. Each chapter includes code examples and real-world patterns.

1. A Brief History of LLMs

  • Hosted vs open-source
  • Model size: accuracy vs cost/latency
  • Context window size
  • Reasoning models
  • Providers and models (March 2026)
  • Use the system prompt
  • Give the LLM more examples
  • A “seed crystal” approach
  • Weird formatting tricks
  • Example: a great prompt
  • Levels of Autonomy
  • Creating a basic agent
  • Structured output
  • Designing your tools: the most important step
  • Real-world example: Alana’s book recommendation agent
  • Observational memory
  • Memory processors
  • Prompt caching
  • What are Dynamic Agents?
  • Example: Creating a Dynamic Agent
  • Guardrails
  • Agent authentication and authorization
  • What is MCP?
  • MCP Primitives
  • The MCP Ecosystem
  • When to use MCP
  • Building an MCP Server and Client
  • Web scraping & computer use
  • Third-party integrations

12. Workflows 101

  • Branching
  • Chaining
  • Merging
  • Conditions
  • Best practices

14. Suspend and Resume

  • Streaming step completion
  • Streaming within steps
  • Streaming tool calls
  • Speed matters

16. RAG 101

17. Choosing a Vector Database

  • Chunking
  • Embedding
  • Upsert
  • Indexing
  • Querying
  • Reranking
  • Code Example
  • Give your agent search tools
  • Let your agent run code
  • Feed the model full context
  • Extract entities and relationships
  • Conclusion

20. Multi-Agent 101

21. Agent Supervisor & Subagents

22. Control Flow

23. Workflows as Tools

24. Parallelized Tool Calls

  • A note on A2A
  • Why accuracy and token cost matter
  • Observability
  • Visualizing traces
  • Setting this up on a project
  • Seeing evals over time
  • Building your eval dataset
  • LLM-as-judge for textual evals
  • Classification or labeling evals
  • Tool calling evals
  • Multi-turn evals
  • Task completion
  • Prompt engineering evals
  • A/B testing
  • Human data review
  • Observability & evals are even more important than you think
  • Building an agentic web frontend
  • Building an agent backend
  • The core agent loop
  • Agentic workflows
  • Using a managed platform
  • On web-based platforms
  • Ephemeral vs. stateful sandboxes
  • Filesystems
  • Managing latency
  • Image Generation
  • Use Cases
  • Voice
  • Video

33. What’s Next

Frequently asked questions