An AI agent framework is a library or toolkit that streamlines the design and implementation of agents. Orchestration logic, tool management, and memory management that would require hundreds of lines to implement from scratch can be written concisely using the APIs that frameworks provide.
Why Use a Framework?
Section titled “Why Use a Framework?”Building from Scratch vs. Using a Framework
Section titled “Building from Scratch vs. Using a Framework”Building an agent from scratch requires you to implement everything yourself:
- The ReAct loop (managing the Reason → Act → Observe cycle)
- Tool definitions, invocations, and error handling
- Context management (length control, summarization)
- Information passing between agents
- Human-in-the-loop approval flows
- Execution logs and debugging
Frameworks provide all of this out of the box, so developers can focus on task-specific logic.
| Aspect | From Scratch | Using a Framework |
|---|---|---|
| Flexibility | Maximum | Within the framework’s scope |
| Development speed | Slow | Fast |
| Learning cost | No framework to learn | Must learn the framework |
| Maintenance | Self-managed | Follow framework updates |
| Recommended when | Strong custom requirements | Building standard agents |
Representative Frameworks
Section titled “Representative Frameworks”1. LangGraph
Section titled “1. LangGraph”LangGraph is a graph-based agent framework developed as part of the LangChain ecosystem. Its official documentation describes it as a foundation for long-running, stateful agents.[1]
# Python - Minimal LangGraph example (conceptual)
from langgraph.graph import StateGraph, END
from typing import TypedDict
class AgentState(TypedDict):
messages: list
next_step: str
# Define the graph
workflow = StateGraph(AgentState)
# Add nodes (processing steps)
workflow.add_node("reason", reason_node) # Thinking step
workflow.add_node("tool_use", tool_node) # Tool execution
workflow.add_node("observe", observe_node) # Result observation
# Define edges (transitions)
workflow.add_edge("reason", "tool_use")
workflow.add_conditional_edges(
"observe",
should_continue, # condition function
{"continue": "reason", "finish": END}
)
# Compile and run the graph
app = workflow.compile()
result = app.invoke({"messages": [("user", "Please execute the research task")]})StateGraph is a mechanism that manages workflow by type-defining the agent’s state and having each node update that state.[1] It’s well-suited for agents with complex conditional branching and state management.
Best suited for
- Agents that require complex state management
- Workflows with many conditional branches and loops
- Cases where you’re already using the LangChain ecosystem
2. CrewAI
Section titled “2. CrewAI”CrewAI is a framework that defines multiple agents as a “crew” and assigns each agent a role and goal. Its official documentation describes agents, crews, and flows as core concepts.[2]
# Python - Minimal CrewAI example (conceptual)
from crewai import Agent, Task, Crew
# Define agents (specify role, goal, and backstory)
researcher = Agent(
role="Research Analyst",
goal="Comprehensively research the latest EV market trends",
backstory="An analyst with 10 years of market research experience who values data accuracy.",
tools=[web_search_tool, data_analysis_tool],
verbose=True
)
writer = Agent(
role="Technical Writer",
goal="Compile research findings into a clear report",
backstory="A technical documentation expert skilled at communicating complex information clearly.",
tools=[file_write_tool]
)
# Define tasks
research_task = Task(
description="Collect and analyze the latest EV market data",
agent=researcher,
expected_output="Analysis report including market share, growth rate, and key players"
)
writing_task = Task(
description="Create a readable report based on the research findings",
agent=writer,
expected_output="A market trends report of approximately 1,500 words",
context=[research_task] # Use results from research_task as input
)
# Assemble and run the crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
verbose=True
)
result = crew.kickoff()Best suited for
- Multi-agent systems with clear role divisions (research → writing → review, etc.)
- Automation of business processes
- Content generation pipelines
3. AutoGen (Microsoft)
Section titled “3. AutoGen (Microsoft)”AutoGen is Microsoft’s framework for building AI agents and applications. Its current documentation describes AgentChat as a programming framework for conversational single-agent and multi-agent applications.[3]
# Python - Minimal AutoGen example (conceptual)
import autogen
# LLM configuration
llm_config = {"model": "MODEL_NAME", "api_key": "YOUR_API_KEY"}
# Define agents
assistant = autogen.AssistantAgent(
name="CodingAssistant",
llm_config=llm_config,
system_message="An agent that writes Python code to solve problems."
)
# Human proxy agent (implements Human-in-the-loop)
user_proxy = autogen.UserProxyAgent(
name="UserProxy",
human_input_mode="TERMINATE", # Ask the human when termination condition is met
code_execution_config={"work_dir": "workspace"}
)
# Start the conversation (agents solve the task through dialogue)
user_proxy.initiate_chat(
assistant,
message="Write a Python script that calculates the Fibonacci sequence and test it"
)This code uses an older AutoGen API as a conceptual example. Current AutoGen is organized into AgentChat, Core, and Extensions, so check the documentation for the version you are using before implementing it.[3]
Best suited for
- Interactive task solving
- Automation of code generation, execution, and debugging
- Multi-agent experiments for research purposes
4. Mastra
Section titled “4. Mastra”Mastra is a TypeScript framework for building AI applications and agents. Its documentation covers agents, workflows, tools, memory, and evals for a modern TypeScript stack.[4]
// TypeScript - Minimal Mastra example (conceptual)
import { Mastra, Agent } from "@mastra/core";
// Define the agent
const researchAgent = new Agent({
name: "ResearchAgent",
instructions: "A specialized agent that uses web search to gather the latest information.",
model: {
provider: "ANTHROPIC",
name: "claude-sonnet-4-5",
},
tools: {
webSearch: webSearchTool,
fileWrite: fileWriteTool,
},
});
// Create the Mastra instance
const mastra = new Mastra({
agents: { researchAgent },
});
// Run the agent
const agent = mastra.getAgent("researchAgent");
const result = await agent.generate(
"Please research the latest AI agent market size"
);
console.log(result.text);Best suited for
- Integration into web frameworks like Next.js or Nuxt
- Agent development in TypeScript projects
- Cases where you want tight coupling between the frontend and agents
5. OpenAI Agents SDK (formerly Swarm)
Section titled “5. OpenAI Agents SDK (formerly Swarm)”The OpenAI Agents SDK is OpenAI’s agent SDK. Its documentation highlights Agents, Tools, Handoffs, Guardrails, and Tracing as core concepts.[5]
# Python - Minimal OpenAI Agents SDK example (conceptual)
from openai import OpenAI
from agents import Agent, Runner, handoff
client = OpenAI()
# Define each specialist agent
triage_agent = Agent(
name="TriageAgent",
instructions="Analyze the user's question and route it to the appropriate specialist agent.",
model="MODEL_NAME"
)
coding_agent = Agent(
name="CodingAgent",
instructions="Handle questions about code.",
model="MODEL_NAME",
handoffs=[handoff(triage_agent)] # Return to triage after completion
)
research_agent = Agent(
name="ResearchAgent",
instructions="Handle information gathering and research tasks.",
model="MODEL_NAME",
handoffs=[handoff(triage_agent)]
)
# Set handoff targets on the triage agent
triage_agent.handoffs = [
handoff(coding_agent),
handoff(research_agent)
]
# Run
result = Runner.run_sync(triage_agent, "Please implement a sorting algorithm in Python")A handoff is a mechanism for delegating a conversation or control from one agent to another.[6] Think of it like transferring a customer call to the right department — the appropriate agent takes over based on its specialty.
Best suited for
- Projects centered on the OpenAI API
- Simple multi-agent handoff implementations
- Customer support and triage systems
6. Claude Code (Anthropic)
Section titled “6. Claude Code (Anthropic)”Claude Code is Anthropic’s coding-oriented AI agent. The official documentation presents it as an agentic coding tool used from the terminal.[7]
Claude Code supports MCP integration and custom subagents for specialized work units.[8][9]
# Claude Code basic usage (command line)
# Execute a single task
claude -p "Investigate and fix the bug in src/auth.ts"
# Parallel tasks using sub-agents (conceptual example)
claude -p "Please execute the following in parallel:
1. Write unit tests for the frontend
2. Generate API documentation for the backend
3. Optimize the CI configuration file"
# Operations integrated with an MCP server
# (when MCP servers are configured)
claude -p "Reference GitHub issue #123 and fix the corresponding code"Using the Claude Code SDK, you can also call Claude Code’s agentic capabilities from your own application.[7]
Best suited for
- Software development and codebase operations
- Large-scale refactoring and automated test generation
- Integration with external services via MCP
Framework Comparison Table
Section titled “Framework Comparison Table”| Framework | Language | Key Feature | Learning Cost | Best For |
|---|---|---|---|---|
| LangGraph | Python | Graph-based, state management | Medium | Complex workflows |
| CrewAI | Python | Role/goal-based design | Low–Medium | Role-divided multi-agent |
| AutoGen | Python | Conversational agents | Medium | Research, code execution automation |
| Mastra | TypeScript | Web integration, type safety | Medium | Web app integration |
| OpenAI Agents SDK | Python | Handoffs, simplicity | Low | OpenAI-centered multi-agent |
| Claude Code | CLI/Python/TS | Coding-specialized, MCP integration | Low | Software development |
Framework Selection Flowchart
Section titled “Framework Selection Flowchart”graph TD
Start["Framework Selection"] --> Lang{"Primary language?"}
Lang -->|"TypeScript"| TS["Mastra\n(TypeScript-first)"]
Lang -->|"Python"| Python{"Primary use case?"}
Python -->|"Coding / development assistance"| Claude["Claude Code\n(coding-specialized)"]
Python -->|"Want to use OpenAI API"| OpenAI["OpenAI Agents SDK\n(simple multi-agent)"]
Python -->|"Complex workflow / state management"| LangGraph["LangGraph\n(graph-based)"]
Python -->|"Clear role-divided team structure"| CrewAI["CrewAI\n(role/goal-based)"]
Python -->|"Research / conversational"| AutoGen["AutoGen\n(conversational agents)"]Summary
Section titled “Summary”- Frameworks let you avoid implementing the ReAct loop, tool management, and inter-agent communication yourself
- Each of the six frameworks has its own design philosophy and strengths
- For TypeScript projects, use Mastra; for coding assistance, use Claude Code; for complex workflows, use LangGraph; for role-divided agents, use CrewAI
- I recommend trying a small prototype before committing to production use
Frequently Asked Questions
Section titled “Frequently Asked Questions”Q: Which framework is easiest for beginners?
A: CrewAI or the OpenAI Agents SDK are most beginner-friendly. CrewAI is especially intuitive — you can build a multi-agent system just by defining roles and goals in natural language. LangGraph is feature-rich but takes time to get comfortable with the graph concept.
Q: What’s the difference between LangChain and LangGraph?
A: LangChain is a general-purpose framework for LLM applications. LangGraph is an agent-specialized framework built on top of LangChain, centered on flow control through graph structures. For building agents, LangGraph is recommended.
Q: Can multiple frameworks be used together?
A: Technically possible, but it gets complex and I don’t recommend it. First check whether a single framework can meet your requirements; only consider combining them if there’s truly no other way.
Q: Is there a way to call the Anthropic API directly without a framework?
A: Yes. Using the Anthropic Python SDK (anthropic package), you can build agents without a framework. This approach is appropriate for simple agents or cases with strong custom requirements. See AI Agents and MCP for details.
Related Links
Section titled “Related Links”- Orchestration Patterns — Multi-agent coordination design
References
Section titled “References”- LangChain, LangGraph overview
- CrewAI, Agents
- Microsoft, AutoGen
- Mastra, Get started with Mastra
- OpenAI, OpenAI Agents SDK
- OpenAI, Handoffs
- Anthropic, Claude Code overview
- Anthropic, Connect Claude Code to tools via MCP
- Anthropic, Create custom subagents