Agentic AI Notebook
Phase 9

Mastra

~2 min read

Concept & How It Works

  • Key points are in the visual diagram above.

Why Does It Exist?

Python dominates agent tooling, but many product teams are TypeScript-native. Mastra brings agent primitives, tracing, and deployment patterns to the JS ecosystem without duct-taping Python microservices.

Real-World Analogy

Mastra is Next.js for agents — opinionated, TypeScript-native, with batteries included for the stack modern web teams already use.
Loading diagram...

Visual Workflows

What is Mastra?

Loading diagram...

Example

Scenario

Next.js app with a Mastra support agent: workflow retrieves from pgvector, calls GPT-4o, runs eval scorer on response quality before returning.

Solution

In Agent Framework Landscape, apply Mastra to this scenario: Next. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Do this before moving to the next module — reading alone is not enough.

Open the Code Walkthrough below and run it locally. Change one parameter related to Mastra (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Mastra happens in the code.

Mastra
1import { Agent } from "@mastra/core/agent";  # import dependencies2import { openai } from "@ai-sdk/openai";  # import dependencies3
4const supportAgent = new Agent({5  name: "support",6  instructions: "Help customers with billing.",7  model: openai("gpt-4o"),8  tools: { lookupInvoice },9});

Commands to Remember

Commands to Remember

  • pip install llama-index # LlamaIndex workflows
  • pip install semantic-kernel # Microsoft Semantic Kernel
  • pip install smolagents # Hugging Face agents

Common Mistakes

  • Treating Mastra as a black box without evaluation
  • Ignoring cost and latency in production
  • Skipping error handling for mastra

Cheat Sheet

Quick recap — the most important points from this module.

Cheat Sheet

quick ref
  • Mastra
  • Workflow
  • Zod
  • MastraClient