1. Chains vs. Agents
The difference between chains and agents lies in control flow:- Static Chain: The developer hardcodes the workflow.
- Example: User Input Prompt Template LLM StrOutputParser. The model has no choice but to follow this exact path.
- Autonomous Agent: The model dynamically routes execution based on the user’s goal.
- Example: If a user asks “Check the price of Apple stock and email it to my manager,” the agent decides to:
- Call a stock price search API tool.
- Read the results.
- Call an email API tool.
- Formulate a final response.
- Example: If a user asks “Check the price of Apple stock and email it to my manager,” the agent decides to:
2. Core Components of an Agent
According to agent architectures, an autonomous agent consists of three central pillars:2.1 Planning
- Subgoal Decomposition: Breaking a large, complex task into smaller, manageable milestones.
- Reflection & Self-Correction: Analyzing tool execution outputs. If a tool returns an error (e.g. API authentication failure), the agent alters its plan and attempts an alternative route instead of failing.
2.2 Memory
- Short-Term Memory: In-context conversation history. Allows the model to remember preceding turns in a chat session.
- Long-Term Memory: Storing past outputs and vector embeddings in a database, allowing the agent to recall information across days or weeks.
2.3 Tools
- Interfaces that allow the LLM to interact with the physical world.
- Examples: Web search engines (Tavily), calculation modules, databases, shell execution tools, or API endpoints.
3. The ReAct Design Pattern
The most popular agent execution framework is the ReAct (Reason + Act) pattern. ReAct combines reasoning (thoughts) and acting (actions) in a recursive loop:- Thought: The model reasons about the current state of the problem (e.g., “I need to find the population of Paris. I should use the search tool.”).
- Action: The model triggers a specific tool with generated arguments (e.g., Calling
search_web("Paris population")). - Observation: The application executes the tool, gets the raw output, and feeds it back to the model (e.g., Output:
"2.1 million"). - Thought / Final Answer: The model evaluates the observation. If the goal is met, it outputs the final answer to the user.
4. Practice Exercises
Practice 1: Identifying Agentic Capabilities
Suppose you want to build an application that analyzes a company budget CSV, generates a chart, and uploads it to Slack. Explain why this requires an Agent rather than a Static Chain.Solution
Solution
A static chain has a linear flow. However, this task involves dynamic steps:
- Decision making: The code must read the CSV and decide which columns are relevant for the chart based on the user’s description.
- Error recovery: If the chart generation library throws a syntax error, a static chain crashes. An agent can read the traceback error (Observation), write corrected python code (Thought/Action), and execute it again until it succeeds.
- Execution Routing: Choosing the Slack upload API tool only after the chart file is successfully verified on disk.