Principles of Building AI Agents

The world's leading guide to getting started with AI agents.

  • Agents
  • Memory
  • Workflows
  • RAG
  • Tools
  • Traces
Principles of Building AI Agents displayed on an event.

119,500 Books Printed

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 (CEO of Mastra) goes over a few chapters of his new book “Principles of Building AI Agents”

Read and shared worldwide.

Get your free copy of “Principles”

What's inside?

Get a clear overview of how modern AI agents work. Explore the core concepts, tools, and patterns you'll use to build real agentic systems.

1. A Brief History of LLMs

  • Hosted vs open-source
  • Model size: accuracy vs cost/latency
  • Context window size
  • Reasoning models
  • Providers and models (May 2025)

  • Give the LLM more examples
  • A seed crystal approach
  • Use the system prompt
  • Weird formatting tricks
  • Example: a great prompt

  • Levels of Autonomy
  • Code Example

  • Structured output

  • Designing your tools: the most important step
  • Real-world example: Alana’s book recommendation agent

  • Working memory
  • Hierarchical memory
  • Memory processors
  • `TokenLimiter`
  • `ToolCallFilter`

  • What are Dynamic Agents?
  • Example: Creating a Dynamic Agent
  • Agent middleware

  • Guardrails
  • Agent authentication and authorization

  • Web scraping & computer use
  • Third-party integrations

  • What is MCP
  • MCP Primitives
  • The MCP Ecosystem
  • When to use MCP
  • Building an MCP Server and Client
  • What’s next for MCP
  • Conclusion

12. Workflows 101

  • Branching
  • Chaining
  • Merging
  • Conditions
  • Best Practices and Notes

14. Suspend and Resume

  • How to stream from within functions
  • Why streaming matters
  • How to Build This

  • Observability
  • Tracing
  • Evals
  • Final notes on observability and tracing

17. RAG 101

18. Choosing a Vector Database

  • Chunking
  • Embedding
  • Upsert
  • Indexing
  • Querying
  • Reranking
  • Code Example

  • Agentic RAG
  • Reasoning-Augmented Generation (ReAG)
  • Full Context Loading
  • Conclusion

21. Multi-Agent 101

22. Agent Supervisor

23. Control Flow

24. Workflows as Tools

25. Combining the Patterns

  • How A2A works
  • A2A vs. MCP

27. Evals 101

  • Accuracy and reliability
  • Understanding context
  • Output
  • Code Example

  • Classification or Labeling Evals
  • Agent Tool Usage Evals
  • Prompt Engineering Evals
  • A/B testing
  • Human data review

  • Building an agentic web frontend
  • Building an agent backend

  • Deployment challenges
  • Using a managed platform

  • Image Generation
  • Use Cases
  • Voice
  • Video

33. Code Generation

34. What’s Next