Workik.

Mastra Tutorial for Beginners: Build AI Agents in TypeScript

Mastra Overview: What Is Mastra?

Learn what Mastra is, how AI agents differ from workflows, and when to use each. Preview the support assistant you will build in this free Mastra course.

Mastra is an open source TypeScript framework designed to build AI agents and deterministic workflows. This lesson introduces core agent concepts, compares workflows against agent loops, and outlines the customer support assistant you will build throughout this course.

Next lesson: Setting Up Your First Mastra Project


What Is an AI Agent?

An AI agent is a software program powered by a large language model that can perceive inputs, make decisions, and execute actions to reach a specific goal.

Traditional software requires developers to hardcode every branch and condition. In contrast, an agent uses a language model to decide which steps to take next. You give the agent instructions, access to external functions called tools, and an objective. The model inspects the user message, decides whether it needs extra information, calls the right tool, and interprets the tool result before generating a response.

Think of an agent like a chef who invents a dish on the fly based on whatever ingredients sit in the kitchen, while standard software follows a laminated recipe card with zero room for deviation.

Quick Insight: An agent is not just a chat model. An agent runs in an execution loop where it inspects tool outputs and decides whether to stop or take another action.


What Is a Workflow?

A workflow is a deterministic, step-by-step pipeline where operations run in a predefined order.

Workflows are structured as directed graphs. Each step takes typed inputs, performs an operation, and produces typed outputs for the next step. Workflows excel at repeatable business logic where you cannot afford unpredictable model behavior. Examples include processing a refund, sending an order confirmation email, or transforming database records.

Common Mistake: Trying to solve predictable business logic with an open-ended agent loop. If your task always requires step A, followed by step B, followed by step C, use a workflow instead of an agent.


Agents Versus Workflows: When to Use Each

Building reliable software with language models requires choosing the right pattern for each task. You can use agents for conversational adaptability and workflows for strict control.

DimensionAI AgentDeterministic Workflow
Execution pathDynamic and chosen by the modelFixed and defined by code
PredictabilityLower, depends on model reasoningHigh, reproducible every run
Best use caseCustomer support chat, research, open triageData transformation, transaction processing, approval pipelines
Failure modeHalts on bad reasoning or wrong tool callsFails on explicit exceptions or validation errors
Control flowModel-driven loopSequential, parallel, and branching steps

Mastra allows you to combine both patterns. You can place an agent inside a workflow step to classify a message, and then use standard code to route the ticket to the correct department.


Where Mastra Fits in the TypeScript Ecosystem

Most AI frameworks were originally written in Python. Mastra is built specifically for TypeScript and Node.js developers.

Mastra builds directly on top of the Vercel AI SDK to provide model interoperability. It adds the higher-level infrastructure needed to run agents in production:

  1. A central registry for agents, tools, and workflows.
  2. Built-in storage and memory layers for conversational history.
  3. Native support for structured output via Zod schemas.
  4. An integrated local development environment called Mastra Studio.

Here is a complete example showing how an agent is defined using Mastra:

ts
Copy

Pro Tip: Mastra standardizes model provider names using a provider slash model pattern, such as openai/gpt-5-mini or anthropic/claude-sonnet-4-6.


The Project You Will Build: Online Store Support Assistant

Throughout this course, you will build a production-ready customer support assistant for an online retail store.

The project starts small and grows in complexity across each lesson:

  • First, you will initialize a TypeScript project and set up Mastra Studio.
  • Next, you will build an agent that answers basic inquiries.
  • You will give the agent tools to fetch real-time shipping data for customer orders.
  • You will add long-term memory so the agent remembers customer context across sessions.
  • You will build a deterministic ticket routing workflow with parallel steps.
  • You will integrate Retrieval-Augmented Generation (RAG) to query a help center knowledge base.
  • Finally, you will connect external tools with the Model Context Protocol (MCP), evaluate agent responses, and deploy the application.

Try It Yourself

Write down two features of an online store. For each feature, decide whether it should be built as an agent or as a workflow. For example, sending an order confirmation email after every purchase always follows the same steps, so it suits a workflow. Talking with a customer to understand a complaint needs flexible reasoning, so it suits an agent.


In the next lesson, Setting Up Your First Mastra Project, you will install the Mastra command-line interface, configure your environment variables, and launch Mastra Studio locally.

Was this lesson helpful?