Skip to main content

7.3 Agent Frameworks

AI-Generated Content

AI-generated content may contain errors. Always verify against official sources.

7.3 Agent Frameworks

Key Concepts: LangChain Agents · CrewAI · AutoGen · OpenAI Agents SDK · When to use what

Official Docs: LangGraph · CrewAI · AutoGen · OpenAI Agents SDK


Framework Landscape

FrameworkCreatorBest forControl level
OpenAI Agents SDKOpenAISimple single/multi-agent, productionHigh
LangGraphLangChainComplex stateful workflows, fine-grained controlHighest
CrewAICrewAIMulti-agent role-based collaborationMedium
AutoGenMicrosoftConversational multi-agent researchMedium

Option 1 — OpenAI Agents SDK

Lightweight, production-grade. Best starting point for most use cases.

pip install openai-agents
from agents import Agent, Runner, function_tool

@function_tool
def search_web(query: str) -> str:
"""Search the web for recent information."""
# In production: call a real search API (e.g., Tavily, Serper)
return f"Search results for '{query}': [mock result 1, mock result 2]"

@function_tool
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression."""
try:
return str(eval(expression, {"__builtins__": {}}))
except Exception as e:
return f"Error: {e}"

agent = Agent(
name="Research Assistant",
instructions="You help users research topics and do calculations. Use tools as needed.",
tools=[search_web, calculate],
model="gpt-4o-mini",
)

result = Runner.run_sync(agent, "What is 15% of 2847, and find information about RAG systems.")
print(result.final_output)

Option 2 — LangGraph

LangGraph models agents as directed graphs (nodes = actions, edges = transitions). It gives you full control over state and execution flow.

pip install langgraph langchain-openai
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool

@tool
def multiply(a: int, b: int) -> int:
"""Multiply two integers."""
return a * b

@tool
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = create_react_agent(llm, tools=[multiply, add])

result = agent.invoke({"messages": [("human", "What is (5 + 3) * 7?")]})
print(result["messages"][-1].content)

Option 3 — CrewAI (Multi-Agent Collaboration)

CrewAI models agents as a team where each agent has a role, goal, and backstory.

pip install crewai crewai-tools
from crewai import Agent, Task, Crew, Process

# Define agents with roles
researcher = Agent(
role="Senior Research Analyst",
goal="Find accurate, up-to-date information on the topic",
backstory="You are an expert at finding and synthesising information from multiple sources.",
verbose=True,
)

writer = Agent(
role="Technical Writer",
goal="Turn research findings into clear, concise reports",
backstory="You write technical documentation that is accurate and accessible to students.",
verbose=True,
)

# Define tasks
research_task = Task(
description="Research the key concepts of Retrieval-Augmented Generation (RAG).",
expected_output="A structured list of 5 key facts about RAG.",
agent=researcher,
)

writing_task = Task(
description="Write a 200-word student-friendly explanation of RAG based on the research.",
expected_output="A clear 200-word explanation suitable for undergraduate students.",
agent=writer,
context=[research_task],
)

crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
verbose=True,
)

result = crew.kickoff()
print(result)

When to Use What

┌──────────────────────────────────────────────┐
│ Start here: OpenAI Agents SDK │
│ → Simple tool-calling agent, fast to build │
└──────────────────────────────────────────────┘

Need fine-grained state control? → LangGraph
Need role-based collaboration? → CrewAI
Need multi-model conversations? → AutoGen

Common Mistakes

Common Mistakes
  1. Jumping to CrewAI/AutoGen for simple tasks — frameworks with high abstraction are harder to debug. Start with OpenAI Agents SDK.
  2. Not setting verbose=True while learning — enabling verbose output shows you exactly what each agent is thinking and calling.
  3. Treating agents as deterministic — agents make non-deterministic decisions. Test with many different inputs before deploying.

Quick Quiz

Test Your Understanding

Q1. Which framework models workflows as directed graphs for maximum control?
A1. LangGraph.

Q2. What concept makes CrewAI different from a single-agent system?
A2. Agents are assigned specific roles, goals, and backstories, and collaborate as a team — mimicking a human crew structure.

Q3. For a beginner building their first agent, which framework is recommended and why?
A3. OpenAI Agents SDK — it is lightweight, well-documented, and requires minimal boilerplate to get a working agent running.


Student Exercise

Exercise 7.3 — Compare frameworks
Build the same task ("Research AI ethics and write a 100-word summary") using both the OpenAI Agents SDK and CrewAI. Compare: lines of code, output quality, latency, and ease of debugging.


Further Reading

Next → 7.4 Building a Research Agent