Patterns
for Building
AI Agents

Patterns of Building AI Agents is a practical guide to taking AI agents from prototype to production. Written by Sam Bhagwat, CEO of Mastra, it covers design patterns, context engineering, eval workflows and security emerging from teams pushing agents into production at prominent AI companies.

  • Design Patterns
  • Context Engineering
  • Eval Workflows
  • Security

The Practitioner's Guide to Taking AI Agents to Production

Patterns for Building AI Agents stacked on a shelf.

32,500 Books Printed

Combined across both books, 32,500 copies have been distributed to people across the globe.

Principles of Building AI Agents displayed on an event.

Principles

Start with the first book in the agentic series to learn the fundamentals

Sam Bhagwat introducing Patterns for Building AI Agents.

Sam Bhagwat

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

Read and shared worldwide.

Get your free copy of “Patterns”

What's inside?

Patterns of Building AI Agents covers practical patterns for taking AI agents from prototype to production. Learn strategies emerging from teams pushing agents into production at prominent AI companies. Chapter topics include human-in-the-loop patterns and dynamic agent architecture, context engineering techniques for parallelizing and compressing context, eval workflows for moving from MVP to production, and security patterns including guardrails, sandboxing and granular access controls.

  • Whiteboard Agent Capabilities
  • Evolve Your Agent Architecture
  • Dynamic Agents
  • Human-in-the-Loop
  • Parallelize Carefully
  • Share Context Between Subagents
  • Avoid Context Failure Modes
  • Compress Context
  • Feed Errors Into Context
  • List Failure Modes
  • List Critical Business Metrics
  • Cross-Reference Failure Modes and Success Metrics
  • Iterate Against Your Evals
  • Create an Eval Test Suite
  • Have SMEs Label Data
  • Create Datasets from Production Data
  • Evaluate Production Data
  • Prevent the Lethal Trifecta
  • Sandbox Code Execution
  • Granular Agent Access Control
  • Agent Guardrails

5. What's Next(ish)

Frequently asked questions

Do I need to read Principles before Patterns?

Patterns of Building AI Agents builds on Principles of Building AI Agents. Mastra recommends starting with Principles to learn the fundamentals before reading Patterns.

Is Patterns of Building AI Agents really free?

Patterns of Building AI Agents is free. Enter your email to get your copy. Combined across both books in the series, 32,500 copies have been distributed to people across the globe with no payment required.

What does Patterns of Building AI Agents cover?

Patterns of Building AI Agents covers five chapters: From Wishlist to Working Agent, Intro to Context Engineering, From MVP to Production, Autonomy Is a Two-Edged Sword and What's Next(ish). Each chapter covers practical strategies and patterns emerging from teams pushing agents into production at prominent AI companies.

What does the context engineering chapter cover?

The context engineering chapter in Patterns of Building AI Agents covers techniques including parallelizing carefully, sharing context between subagents, avoiding context failure modes, compressing context and feeding errors into context.

How does Patterns approach taking agents to production?

Patterns of Building AI Agents covers the practical steps for moving agents from MVP to production, including listing failure modes, defining critical business metrics, creating eval test suites, labeling data with subject matter experts and evaluating production data. It also covers security patterns for preventing the lethal trifecta, sandboxing code execution and granular agent access controls.