Getting Started
AI Agents Introduction
Learn what AI agents are and how they differ from simple LLM completions.
What is an AI Agent?
An AI agent is an LLM-powered system that can perceive, reason, and act in a loop to accomplish a goal. Unlike a simple chatbot that responds once, an agent:
- Receives a task
- Decides what to do (plan)
- Takes an action (tool call, code execution, web search)
- Observes the result
- Decides next step
- Repeats until done
Agents vs Chains vs Completions
| Type | Description | When to Use |
|---|---|---|
| Completion | Single LLM call | Simple Q&A |
| Chain | Fixed sequence of steps | Predictable workflows |
| Agent | Dynamic, self-directed loop | Open-ended tasks |
Core Components
- LLM: The reasoning engine (GPT-4, Claude, Gemini)
- Tools: Functions the agent can call (search, calculator, code runner)
- Memory: Short-term (context window) and long-term (vector DB)
- Planner: How the agent decides next steps (ReAct, CoT, etc.)
ReAct Pattern
The most common agent pattern: Reasoning + Acting
- Thought: What should I do?
- Action: Call a tool
- Observation: See the result
- Repeat until final answer
Example
python
# Simple ReAct agent from scratch
from anthropic import Anthropic
import json
client = Anthropic()
# Define tools
tools = [
{
"name": "calculator",
"description": "Perform arithmetic calculations",
"input_schema": {
"type": "object",
"properties": {
"expression": {"type": "string", "description": "Math expression to evaluate"}
},
"required": ["expression"]
}
},
{
"name": "get_current_time",
"description": "Get the current date and time",
"input_schema": {
"type": "object",
"properties": {}
}
}
]
# Tool implementations
def calculator(expression: str) -> str:
try:
result = eval(expression, {"__builtins__": {}}, {})
return str(result)
except Exception as e:
return f"Error: {e}"
def get_current_time() -> str:
from datetime import datetime
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def run_tool(name: str, inputs: dict) -> str:
if name == "calculator":
return calculator(inputs["expression"])
elif name == "get_current_time":
return get_current_time()
return "Unknown tool"
# Agent loop
def run_agent(user_message: str) -> str:
messages = [{"role": "user", "content": user_message}]
while True:
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=1024,
tools=tools,
messages=messages
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason == "end_turn":
for block in response.content:
if hasattr(block, "text"):
return block.text
# Process tool calls
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = run_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
messages.append({"role": "user", "content": tool_results})
answer = run_agent("What is 15% of 847, and what time is it right now?")
print(answer)Try it yourself — PYTHON