Here are some practical exercises to apply what you've learned about AI agents, structured to follow the learning path:
Practical Exercises to Master AI Agents
These exercises are designed to help you get hands-on experience with the concepts covered in each part of the learning series. They build upon each other, so it's recommended to work through them sequentially.
Exercise 1: Your First Basic Agent (Connecting Parts 1 & 2)
Goal: Set up a simple environment, choose an LLM, and define a basic tool.
Steps:
Environment Setup: Choose a Python environment. Install necessary libraries (e.g.,
openai
for LLM interaction, or a framework likeLangChain
if you prefer).LLM Integration: Select a free or low-cost LLM API (e.g., a basic OpenAI model, or a local open-source model if you have the setup) and make your first API call to ensure connectivity.
Define a Simple Tool: Create a Python function that simulates an external action. Examples:
get_current_time()
: Returns the current date and time.check_weather(city)
: Returns a hardcoded or simulated weather update for a given city.search_dummy_database(query)
: Returns a fixed response as if querying a database.
Agent Initialization: Write basic code to initialize your agent with the chosen LLM and the single tool. Test that the agent can "see" and "describe" the tool (e.g., using a LangChain
AgentExecutor
withtools
).
Challenge: Get your agent to successfully call the tool once based on a simple prompt (e.g., "What time is it?").
Exercise 2: Implementing the ReAct Workflow (Connecting Part 3)
Goal: Build an agent that uses the ReAct pattern to reason and act iteratively.
Steps:
Expand Tools: Add a second, more complex tool. Example:
calculate_expression(expression)
: Takes a mathematical expression as a string and returns the result (you can use Python'seval()
for simplicity, with caution!).
ReAct Prompting: Design your LLM prompt to encourage ReAct behavior. Ensure it guides the LLM to output
Thought
,Action
,Action Input
, andObservation
steps.Orchestration Loop: Implement a loop that:
Sends the prompt and current observation to the LLM.
Parses the LLM's response to identify
Thought
,Action
, andAction Input
.Executes the chosen tool with the provided input.
Updates the
Observation
for the next LLM call.
Challenge: Get your agent to solve a multi-step problem, like "What is 150 divided by 3, plus 10?" where it needs to calculate first and then perhaps state the result, demonstrating iterative thinking.
Exercise 3: Adding Grounded Intelligence with RAG (Connecting Part 3)
Goal: Enhance your ReAct agent with Retrieval Augmented Generation (RAG) to answer questions based on external, specific knowledge.
Steps:
Create a Simple Knowledge Base: Prepare a small set of text documents (e.g., 3-5 paragraphs about a specific topic, company info, or simple product descriptions). Store them as plain text files or in a list.
Build a Retrieval Tool: Create a tool,
retrieve_information(query)
, that simulates searching your knowledge base. When called, it should return relevant text snippets from your prepared documents based on thequery
(a simple string matching or embedding search can be implemented for more advanced users).Integrate RAG into ReAct: Modify your agent's prompt to encourage it to use the
retrieve_information
tool when it needs specific external data. The agent should retrieve the information and then use its ReAct loop to answer a question that requires that knowledge.
Challenge: Ask your agent a question that can only be answered by retrieving information from your knowledge base. Ensure it uses the retrieval tool before formulating its final answer.
Exercise 4: Agent Memory and Simple Self-Reflection (Connecting Part 4)
Goal: Give your agent conversational memory and a basic ability to reflect on its actions.
Steps:
Implement Conversational Memory: Maintain a list or queue of past user prompts and agent responses. Include this "chat history" in subsequent LLM calls to give the agent context.
Basic Self-Reflection: After an agent completes a task, add a step where you give it a prompt like "Review your last action and determine if it was successful. If not, what went wrong and what should you do next?" (This output might be for your logging, not necessarily shown to the user).
Test Memory: Engage in a multi-turn conversation where the agent needs to recall something from a previous turn to answer correctly (e.g., "My name is John. What is my name?").
Challenge: Design a scenario where the agent tries a tool, fails (e.g., deliberately give it bad input), and then your reflection prompt helps it identify the error and adjust its strategy for the next attempt.
Exercise 5: Basic Guardrails and Error Handling (Connecting Part 5)
Goal: Implement simple input filtering and robust error handling for your agent.
Steps:
Input Filtering: Add a simple check before the agent processes a user prompt. If the prompt contains certain keywords (e.g., "delete all," "harm," "sensitive_info"), reject the input with a polite message.
Tool Call Error Handling: Modify your tool execution logic (from Exercise 2/3) to include
try-except
blocks. If a tool call fails, capture the error and provide a meaningfulObservation
back to the agent (e.g., "Tool 'calculate_expression' failed with error: division by zero").Agent's Response to Errors: Design your LLM prompt to guide the agent on how to respond when a tool call fails. It should acknowledge the error, explain what happened (if appropriate), and suggest alternative actions or ask clarifying questions.
Challenge: Intentionally provide input that will trigger a tool error (e.g., "calculate 5/0"). Observe how your guardrails and error handling prevent undesirable behavior and guide the agent's response.
Exercise 6: Simple Agent Evaluation (Connecting Part 6)
Goal: Set up a basic framework to evaluate your agent's task completion and efficiency.
Steps:
Define Test Cases: Create a small set (e.g., 5-10) of distinct test questions/prompts that your agent should be able to handle, including some multi-step tasks.
Automate Runs: Write a script that iterates through your test cases, sends each to your agent, and captures the final output and the sequence of steps/tool calls.
Manual Review (Initial Quality Check): For each test case, manually assess if the agent successfully completed the task and if the answer was correct/relevant.
Calculate Basic Metrics: Based on your manual review, calculate:
Task Completion Rate: (Number of successful completions / Total test cases) * 100
Average Steps per Task: Sum of steps taken / Total successful tasks.
Challenge: Identify one test case where your agent performs poorly. Based on the steps logged, hypothesize why it failed (e.g., wrong tool chosen, hallucination) and suggest a change to your agent's prompt or tools to improve it.
Exercise 7: Framework Exploration & Iteration Mindset (Connecting Part 7)
Goal: Explore a popular agent framework and consider how to iterate on an agent's design.
Steps:
Choose a Framework: Pick one of the popular frameworks mentioned (LangGraph, AutoGen, CrewAI).
Run a Basic Example: Find a "getting started" example from the framework's documentation and run it successfully on your machine. Understand its structure.
Refactor an Existing Exercise: Try to refactor one of your earlier exercises (e.g., the ReAct agent from Exercise 2) using the chosen framework. Observe how the framework simplifies tool definition, agent orchestration, and state management.
Challenge: Think about a real-world problem you face that an AI agent could solve. Outline an iterative deployment plan: What's the smallest first step (MVA)? What would be the next few iterations of improvement? How would you monitor its performance in production? (No coding required for this challenge, just planning!)
Good luck with your practical exercises! This hands-on experience is invaluable for truly mastering AI agents.