Agents are LLMs that can perceive, plan, and act autonomously using tools. This lesson covers the ReAct pattern, memory architectures, planning strategies, and failure modes.
An agent is an LLM-powered system that autonomously decides which actions to take to accomplish a goal. The core loop:
This distinguishes agents from simple chatbots: a chatbot responds to one message. An agent pursues a goal across multiple steps, making autonomous decisions about which tools to use and when to stop.
Key insight: The LLM is the "brain" — it does the planning and reasoning. Tools are the "hands" — they let the agent interact with the world. The loop connecting them is the agent framework.
ReAct (Reasoning + Acting) interleaves chain-of-thought reasoning with tool actions. Each step has three parts:
Thought: I need to find information about Python pathlib.
Let me search for it.
Action: search("Python pathlib module")
Observation: "The pathlib module provides object-oriented
filesystem path handling..."
Thought: Now I have the information. Let me save a summary.
Action: save_note("pathlib", "pathlib provides OOP file paths...")
Observation: Note saved successfully.
Thought: I have the information and saved it. I can answer now.
Answer: Python's pathlib module provides...
Agents need memory to maintain state across steps and sessions.
| Type | Where | Lifetime | Use Case |
|---|---|---|---|
| In-context | Conversation messages | Current session | Tool results, intermediate reasoning |
| External KV | Dictionary / database | Persistent | Notes, facts, user preferences |
| Episodic | Vector store | Persistent | Past conversations, learned patterns |
The simplest form: the full conversation history (messages list) serves as working memory. The agent can "remember" tool results from earlier steps because they are still in the context window.
Limitation: Context windows are finite. Long agent runs can exceed the limit, requiring summarization or truncation strategies.
Complex tasks require the agent to decompose the problem before acting.
Ask the LLM to break a complex task into sub-tasks before executing any of them. This reduces errors by making each step simpler and more focused.
Task: "Compare Python and Rust for web development"
Plan:
1. Search for Python web frameworks
2. Search for Rust web frameworks
3. Compare performance characteristics
4. Compare developer experience
5. Write comparison summary
When a step fails or produces unexpected results, the agent should be able to revise its plan. This requires the LLM to assess whether the current approach is working and adapt if needed.
Practical tip: Include a system prompt that says "After each tool result, briefly assess whether you are making progress toward the goal. If not, reconsider your approach."
Agents fail in predictable ways. Knowing these patterns lets you build defenses.
Live demonstration: a research agent with search, read, and note-taking tools.
search, read_url, save_note, get_notesrun_agent(task, tools, max_steps=10)