Goodbye Python? Building Production-Ready Agents with Mastra & Vercel AI SDK
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
| Dimension | Python stack | TypeScript (Mastra + AI SDK) |
|---|---|---|
| Runtime | Python 3.11 + venv | Node 20+ (same as Next.js) |
| Types | Pydantic + manual sync | Shared Zod in tools + UI |
| Streaming UI | Custom SSE bridge | createAgentUIStreamResponse |
| Deploy | Container / GPU | Vercel serverless or Node |
| Best for | ML pipelines | Web 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@latest → src/mastra/ agents & tools → npm run dev → Studio on port 4111.
AI SDK path: install ai + @ai-sdk/openai → define ToolLoopAgent → createAgentUIStreamResponse 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)
- Install Node.js 20+ and obtain
OPENAI_API_KEY(or Anthropic/Google per wizard). npm create mastra@latest my-ts-agent cd my-ts-agent- Complete the wizard (provider, example agent, observability opt-in).
- Inspect
src/mastra/index.ts,agents/,tools/,workflows/. - Add
.envwith API keys—never commit secrets.
→ opennpm run devhttp://localhost:4111- Customize instructions and add a Zod-typed tool (follow generated weather example).
- Before prod: add scorers, set explicit loop limits, deploy per Mastra docs.
Vercel AI SDK in Next.js (6 steps)
- Next.js 15 App Router project with TypeScript.
npm install ai @ai-sdk/openai zod- Create
src/agents/support-agent.tswithToolLoopAgent, tools, andstopWhen: stepCountIs(12). - Add
app/api/chat/route.tsusingcreateAgentUIStreamResponseandexport const maxDuration = 60.
and wirenpm install @ai-sdk/reactuseChat({ api: '/api/chat' }).- Production: rate-limit the route, enable telemetry, use
needsApproval: trueon 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),
});
Which stack to pick
- New agent product + workflows/evals: start with Mastra (
npm create mastra@latest). - Agent as one feature in existing Next.js SaaS: add AI SDK
ToolLoopAgentonly. - 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.