6.1 LangGraph Agent Workflow
Source: LangChain LangGraph Documentation
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from typing import TypedDict, Annotated
# Define state
class AgentState(TypedDict):
messages: list
current_step: str
# Define tools
@tool
def search_web(query: str) -> str:
"""Search the web for information."""
# Implementation
return "search results"
@tool
def execute_code(code: str) -> str:
"""Execute Python code."""
# Implementation
return "execution results"
# Create agent node
def agent_node(state: AgentState):
llm = ChatOpenAI(model="gpt-4", temperature=0)
response = llm.invoke(state["messages"])
return {"messages": state["messages"] + [response]}
# Create tool node
def tool_node(state: AgentState):
# Execute tool based on last message
pass
# Build graph
workflow = StateGraph(AgentState)
workflow.add_node("agent", agent_node)
workflow.add_node("tools", tool_node)
workflow.add_edge("agent", "tools")
workflow.add_conditional_edges(
"tools",
lambda x: "agent" if x["current_step"] != "done" else END
)
app = workflow.compile()
Key Insight
Use LangGraph for complex workflows with loops, state persistence, and conditional logic. Use LangChain for simpler sequential chains.
6.2 CrewAI Agent Configuration
Source: CrewAI vs AutoGen Comparison
from crewai import Agent, Task, Crew, Process
# Define agents with roles
researcher = Agent(
role="Research Analyst",
goal="Find and synthesize relevant information",
backstory="Expert at finding patterns in data",
tools=[search_tool, web_scraper],
llm=llm,
memory=True, # Built-in memory management
verbose=True
)
writer = Agent(
role="Content Writer",
goal="Create compelling content from research",
backstory="Experienced technical writer",
tools=[text_editor],
llm=llm,
memory=True
)
# Define tasks
research_task = Task(
description="Research the topic: {topic}",
expected_output="Comprehensive research summary",
agent=researcher
)
writing_task = Task(
description="Write article based on research",
expected_output="Complete article",
agent=writer,
context=[research_task] # Depends on research
)
# Create crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
memory=True,
verbose=True
)
result = crew.kickoff(inputs={"topic": "AI trends 2024"})
When to Use
- CrewAI: You know how to solve a problem and want to automate it
- AutoGen: You need experts to collaboratively find a solution