AutoGen & Mastra
Microsoft AutoGen
Core Pattern
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient
model = OpenAIChatCompletionClient(model="gpt-4o")
analyst = AssistantAgent("analyst", model_client=model,
system_message="Analyze code for bugs and performance issues.")
fixer = AssistantAgent("fixer", model_client=model,
system_message="Fix identified issues with minimal changes.")
reviewer = AssistantAgent("reviewer", model_client=model,
system_message="Review fixes for correctness. Say APPROVE to finish.")
team = RoundRobinGroupChat(
[analyst, fixer, reviewer],
max_turns=10,
)
result = await team.run(task="Review and fix: def add(a,b): return a-b")
Group Chat Types
| Type |
Description |
RoundRobinGroupChat |
Agents take turns in order |
SelectorGroupChat |
LLM selects next speaker |
Swarm |
Agents hand off to each other |
SelectorGroupChat
from autogen_agentchat.teams import SelectorGroupChat
team = SelectorGroupChat(
[analyst, fixer, reviewer],
model_client=model,
selector_prompt="Select the next agent based on conversation state.",
)
Tools
from autogen_core import FunctionCall
async def search_code(query: str) -> str:
"""Search codebase for relevant files."""
return grep_results(query)
agent = AssistantAgent("dev", model_client=model, tools=[search_code])
Streaming
async for message in team.run_stream(task="Fix the authentication bug"):
print(message)
Mastra (TypeScript)
Agent Definition
import { Agent, createTool } from "@mastra/core";
import { z } from "zod";
const searchTool = createTool({
id: "search",
description: "Search documentation",
inputSchema: z.object({ query: z.string() }),
execute: async ({ context }) => {
return { results: await search(context.query) };
},
});
const agent = new Agent({
name: "dev-assistant",
instructions: "You are a helpful development assistant.",
model: openai("gpt-4o"),
tools: { search: searchTool },
});
const response = await agent.generate("How do I set up MCP?");
Workflows
import { createWorkflow, createStep } from "@mastra/core";
const analyzeStep = createStep({
id: "analyze",
inputSchema: z.object({ code: z.string() }),
execute: async ({ context }) => ({ issues: analyze(context.code) }),
});
const fixStep = createStep({
id: "fix",
execute: async ({ context }) => ({ fixed: fix(context.issues) }),
});
const workflow = createWorkflow({ name: "code-review" })
.then(analyzeStep)
.then(fixStep);
// Branching
const workflow2 = createWorkflow({ name: "triage" })
.then(analyzeStep)
.branch([
[ctx => ctx.severity === "high", urgentFixStep],
[ctx => ctx.severity === "low", queueStep],
]);
// Parallel
const workflow3 = createWorkflow({ name: "multi-check" })
.parallel([lintStep, testStep, securityStep]);
RAG Support
import { createVectorStore } from "@mastra/core";
const store = createVectorStore({
provider: "pinecone",
index: "codebase",
});
const agent = new Agent({
name: "rag-agent",
tools: { search: store.asTool() },
});
When to Use
| Framework |
Best For |
| AutoGen |
Multi-agent conversations, research teams |
| Mastra |
TypeScript projects, workflow automation, RAG |
Sources