AI
AI SDK Patterns

Command Palette

Search for a command to run...

PatternsMastranewHarnessnewComposeGitHub
All Patterns

Core / SDK

4
  • Structured Output
  • Generative UI
  • Image Generation
  • Code ArtifactPopular

Chat

4
  • Streaming ChatPopular
  • Reasoning Display
  • Chat with Citations
  • Multi-Modal ChatNew

Agents

9
  • Tool CallingPopular
  • Multi-Step AgentPopular
  • Mastra AgentNew
  • Mastra MemoryNew
  • Mastra RAGNew
  • Mastra Multi-AgentNew
  • Mastra Observational MemoryNew
  • Mastra Structured OutputNew
  • Mastra GuardrailsNew

Tools

5
  • Web Search Agent
  • RAG PipelinePopular
  • MCP Client AgentNew
  • Text-to-SQLNew
  • Mastra ToolNew

Workflows

4
  • Human-in-the-Loop
  • Durable Multi-Turn Chat Agent
  • Mastra WorkflowNew
  • Mastra Human-in-the-LoopNew
PatternsMastraComposeAI SDK DocsGitHub

Built with AI SDK · shadcn/ui · Next.js

© 2026 AI SDK Patterns

Mastra Guardrails

Add input/output guardrails to agents for content filtering, PII detection, and response validation before they reach users.

Newagentsintermediatemastraguardrailssafetyvalidationfiltering

Loading interactive preview...

Installation

Option 1: Install via CLI

pnpm dlx shadcn@latest add https://ai-sdk-patterns.vercel.app/r/mastra-agent-guardrails

Automatically installs the pattern and its dependencies in your project.

Option 2: Copy or Download

Download the complete pattern as a standalone Next.js project.

Usage

1. Set up environment variables

# .env.local
ANTHROPIC_API_KEY=your_anthropic_key
OPENAI_API_KEY=your_openai_key
GOOGLE_GENERATIVE_AI_API_KEY=your_google_key

Add your AI provider API key to enable real functionality.

2. Run the development server

npm run dev
# or
pnpm dev
# or
yarn dev

Open http://localhost:3000 to see the pattern in action.

3. Customize for your needs

The pattern is ready to use. Modify the components, API routes, and styling to fit your application.

  • Update the UI components in app/page.tsx
  • Modify API logic in app/api/
  • Adjust styling with Tailwind CSS classes
  • Add your own business logic and data sources

Use Cases

Custom Applications

Adapt this pattern for your specific use case and requirements.

Learning & Prototyping

Use this pattern to learn AI SDK concepts and prototype new ideas.

Integration Projects

Integrate AI capabilities into existing applications and workflows.

MVP Development

Quickly build minimum viable products with AI-powered features.

Technical Details

Dependencies

• Next.js 16+ (App Router)
• AI SDK v6
• React 19+
• Tailwind CSS
• TypeScript

Files Included

• src/mastra/agents/guarded-agent.ts

Related patterns

Mastra Agent

Create a Mastra agent with tools, register it in a Mastra instance, and call it with generate() or stream().

Mastra Human-in-the-Loop

Suspend a workflow mid-execution to collect human approval, then resume with the decision. Essential for agent guardrails.

Interactive Demo Coming Soon

The mastra-agent-guardrails pattern demo is being prepared.

Download the pattern to try it with your API key