AI / Automation May 29, 2026

Goodbye Python? Building Production-Ready Agents with Mastra & Vercel AI SDK

VpsGona Engineering Team May 29, 2026 ~15 min read
Mastra Vercel AI SDK TypeScript agent development 2026

Python still dominates LLM notebooks, but frontend and full-stack teams already ship in TypeScript. This guide shows how to build production-ready agents with Mastra and the Vercel AI SDK—without a Python sidecar beside your Next.js app. Official docs: Mastra quickstart, AI SDK agents overview. Related: OpenClaw + Ollama (local models), iOS without a Mac, Mac mini M4 LLM memory, Hermes Agent + Ollama.

Disclosure: VpsGona offers optional Mac cloud rental for native builds. Mastra and Vercel AI SDK are independent projects.

Why TypeScript-native agents matter

Shipping an agent next to your React UI should not require a second runtime, duplicate schemas, or a fragile FastAPI bridge. Mastra gives you agents, tools, workflows, RAG, and evals in one TS framework. Vercel AI SDK 6 adds ToolLoopAgent—model calls, tool execution, and loop control in a few dozen lines inside app/api/chat/route.ts.

Quotable fact: Mastra Studio runs on localhost:4111 after npm run dev; AI SDK’s ToolLoopAgent defaults to 20 tool-loop steps unless you set stopWhen: stepCountIs(N) explicitly.

Python glue vs TypeScript-first stack

DimensionPython stackTypeScript (Mastra + AI SDK)
RuntimePython 3.11 + venvNode 20+ (same as Next.js)
TypesPydantic + manual syncShared Zod in tools + UI
Streaming UICustom SSE bridgecreateAgentUIStreamResponse
DeployContainer / GPUVercel serverless or Node
Best forML pipelinesWeb product engineers

Recommendation: If you are Next.js-first, default to TypeScript. Keep Python only when you depend on PyTorch-only pipelines or libraries with no TS port.

How Mastra and Vercel AI SDK fit together

Mastra path: npm create mastra@latestsrc/mastra/ agents & tools → npm run dev → Studio on port 4111.

AI SDK path: install ai + @ai-sdk/openai → define ToolLoopAgentcreateAgentUIStreamResponse in a Route Handler → useChat on the client.

You can use Mastra alone, AI SDK alone, or Mastra as a backend service called from Next.js—share Zod schemas in a monorepo package to avoid duplicating tool definitions.

Mastra quickstart (8 steps)

  1. Install Node.js 20+ and obtain OPENAI_API_KEY (or Anthropic/Google per wizard).
  2. npm create mastra@latest my-ts-agent
    cd my-ts-agent
  3. Complete the wizard (provider, example agent, observability opt-in).
  4. Inspect src/mastra/index.ts, agents/, tools/, workflows/.
  5. Add .env with API keys—never commit secrets.
  6. npm run dev
    → open http://localhost:4111
  7. Customize instructions and add a Zod-typed tool (follow generated weather example).
  8. Before prod: add scorers, set explicit loop limits, deploy per Mastra docs.

Vercel AI SDK in Next.js (6 steps)

  1. Next.js 15 App Router project with TypeScript.
  2. npm install ai @ai-sdk/openai zod
  3. Create src/agents/support-agent.ts with ToolLoopAgent, tools, and stopWhen: stepCountIs(12).
  4. Add app/api/chat/route.ts using createAgentUIStreamResponse and export const maxDuration = 60.
  5. npm install @ai-sdk/react
    and wire useChat({ api: '/api/chat' }).
  6. Production: rate-limit the route, enable telemetry, use needsApproval: true on destructive tools.
import { ToolLoopAgent, stepCountIs, tool } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

export const supportAgent = new ToolLoopAgent({
  model: openai('gpt-4o-mini'),
  instructions: 'Concise SaaS support agent. Use tools before guessing.',
  tools: {
    lookupOrder: tool({
      description: 'Fetch order status',
      inputSchema: z.object({ orderId: z.string() }),
      execute: async ({ orderId }) => ({ orderId, status: 'shipped' }),
    }),
  },
  stopWhen: stepCountIs(12),
});
  • New agent product + workflows/evals: start with Mastra (npm create mastra@latest).
  • Agent as one feature in existing Next.js SaaS: add AI SDK ToolLoopAgent only.
  • Also ship iOS: TypeScript agents do not replace Xcode—see iOS without a Mac.
  • Local models only: see OpenClaw + Ollama or Hermes + Ollama.

Troubleshooting

Tool loop never stops

Symptom: 15+ tool rounds, token bill spikes.
Fix: Set stopWhen: stepCountIs(8); tighten tool descriptions; return structured errors from execute.

Mastra Studio not on :4111

Symptom: npm run dev fails or port closed.
Fix: Node 20+, lsof -i :4111, verify .env API key, run from project root.

Frequently asked questions

Do I need Python for Mastra or Vercel AI SDK?

No for the app layer—both run on Node.js 20+. Python may remain for offline ML only.

What Node version is required?

Mastra targets Node 20+. Match dev and CI.

What is the default ToolLoopAgent step limit?

Up to 20 steps unless you set stopWhen: stepCountIs(N).

Where does Mastra Studio run?

Typically http://localhost:4111 after npm run dev.

Can I use local Ollama?

Yes via OpenAI-compatible endpoints; see our OpenClaw + Ollama and Hermes guides for local stacks.

TypeScript-native AI

Build agents beside your existing React stack without a Python sidecar.