7.1 What Are AI Agents?
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
Common Mistakes
- Using an agent for fixed workflows — agents add latency and unpredictability. For a known 3-step task, use a chain.
- No iteration limit — without a
max_stepsormax_turnslimit, a buggy agent can loop indefinitely, burning tokens and money. - Broad tool descriptions — vague tool descriptions cause the model to call the wrong tool. Be precise about what each tool does and when to use it.
- No error handling in tools — if a tool function raises an exception, the agent loop crashes. Always catch exceptions and return an error message as the tool result.
Quick Quiz
Q1. What does ReAct stand for?
A1. Reason + Act — the agent reasons about what to do, acts (calls a tool), observes the result, and repeats.
Q2. What are the three core components of an LLM agent?
A2. LLM (brain), Tools (hands/actions), Memory (state/context).
Q3. When should you use an agent instead of a pipeline?
A3. When the number of steps or which tools to use is unknown in advance — i.e., the path to the answer must be decided dynamically at runtime.
Q4. What happens if you forget to set a max iteration limit on an agent?
A4. The agent can loop indefinitely if it can’t converge on an answer, consuming API tokens until you manually stop it or your budget is exhausted.
Student Exercise
Exercise 7.1 — Your first agent
Using the OpenAI Agents SDK or LangChain, build a simple agent with two tools: calculate(expression: str) (uses Python eval) and get_country_capital(country: str) (returns hardcoded capitals). Test it with: "What is the capital of Japan?" and "What is 123 * 456?".