Chapter 3: Agents and Tool Use
What are LLM Agents?
LLMs that can plan, use tools, and take actions autonomously.
ReAct Pattern
Reasoning + Acting:
Thought: I need to find the current weather
Action: search("weather in Paris")
Observation: It's 15°C and sunny
Thought: Now I can answer the user
Answer: The weather in Paris is 15°C and sunny
Tool Use Implementation
tools = [
{
"name": "web_search",
"description": "Search the web",
"parameters": {"query": "string"}
},
{
"name": "calculator",
"description": "Perform calculations",
"parameters": {"expression": "string"}
}
]
def agent_loop(query, tools, max_iterations=5):
messages = [{"role": "user", "content": query}]
for _ in range(max_iterations):
response = llm.generate(messages, tools=tools)
if response.tool_calls:
for call in response.tool_calls:
result = execute_tool(call)
messages.append({"role": "tool", "content": result})
else:
return response.content
Agent Frameworks
| Framework | Features |
|---|---|
| LangChain | Chains, agents, memory |
| AutoGPT | Autonomous goal pursuit |
| CrewAI | Multi-agent collaboration |
Safety Considerations
- Limit tool permissions
- Human approval for actions
- Sandboxed execution
- Clear stopping conditions