7.1 What Are AI Agents?
AI-Generated Content
AI-generated content may contain errors. Always verify against official sources.
7.1 What Are AI Agents?
Key Concepts: ReAct pattern · Tool use · Planning loops
Official Docs: OpenAI Agents SDK · LangGraph
LLM Pipelines vs Agents
| Pipeline | Agent | |
|---|---|---|
| Control flow | Fixed, predetermined | Dynamic, decided by the LLM |
| Tool use | Optional, fixed order | Self-selects tools at runtime |
| Steps | Known in advance | Variable, iterative |
| Suitable for | Structured tasks | Open-ended tasks |
The ReAct Loop
The ReAct (Reason + Act) pattern is the foundational agent loop: the model reasons, calls a tool, observes the result, and repeats until it can answer.
Thought: I need the current Bitcoin price.
Action: get_price(symbol="BTC-USD")
Observation: {"price": 67432.10, "currency": "USD"}
Thought: I have the price. I can now answer.
Final Answer: Bitcoin is currently trading at $67,432.10.
Core Agent Components
┌─────────────────────────────────────────┐
│ AGENT │
│ ┌─────────┐ ┌──────────┐ ┌────────┐ │
│ │ LLM │ │ Tools │ │ Memory │ │
│ │ (Brain) │ │ (Hands) │ │(State) │ │
│ └─────────┘ └──────────┘ └────────┘ │
└─────────────────────────────────────────┘
- LLM: decides what to do next
- Tools: Python functions the LLM can call (search, calculator, API)
- Memory: conversation history + retrieved context
A Minimal Agent with the OpenAI Agents SDK
pip install openai-agents
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"The weather in {city} is 22°C and sunny."
agent = Agent(
name="Weather Assistant",
instructions="You are a helpful weather assistant. Use tools to get real weather data.",
tools=[get_weather],
)
result = Runner.run_sync(agent, "What's the weather in Tokyo and London?")
print(result.final_output)
When to Use Agents
✅ Tasks requiring multiple, dynamic steps ✅ Tasks that need external data (search, database, APIs) ✅ Tasks where the path to the answer is unknown upfront
❌ Tasks with a fixed, known number of steps → use a pipeline ❌ Tasks requiring sub-second response → agents add latency