Hey everyone, Emma here from agent101.net!
It’s May 6th, 2026, and if you’re anything like me, your inbox and social feeds are probably overflowing with talk about AI agents. It feels like every other day there’s a new framework, a new announcement, or a new “expert” telling you how they’re going to change everything. And honestly? A lot of it can feel a bit overwhelming, especially when you’re just trying to get your head around what these things actually ARE and how you might even start playing with them.
When I first started dipping my toes into this world, I remember feeling a bit lost. There were so many high-level explanations, but very few practical “here’s how you actually do a thing” guides for someone who isn’t a seasoned AI researcher. My background is more in general software development and, let’s be honest, trying to build a new habit of understanding complex technical jargon. So, I decided to tackle one of the most common stumbling blocks I encountered: getting a simple AI agent to actually DO something useful, specifically automating a very basic web task.
Today, I want to talk about how we can teach a simple AI agent to monitor a specific part of a website for changes and then alert us. Think of it as your personal, super-focused web assistant. This isn’t about building the next multi-modal, self-improving super-agent (yet!). It’s about taking those first, concrete steps and seeing an agent perform a task that’s genuinely helpful, even if it’s small. We’re going to use a combination of Python, a dash of LangChain (because it simplifies so much!), and a sprinkle of a large language model (LLM) to make this happen. And don’t worry, we’re keeping it absolutely beginner-friendly.
My Frustration with “Just Use It” Tutorials
My journey into AI agents started, like many things, with a personal itch. I was trying to buy a specific, somewhat obscure, limited-edition board game. It would randomly come back in stock on a particular online retailer’s website, often for only a few minutes before selling out again. I tried browser extensions, but they were too generic or didn’t quite hit the mark. I needed something that could intelligently “read” the page, understand if the “Add to Cart” button was active, and then tell me. This felt like a perfect, contained problem for a simple agent.
I dove into tutorials, and so many of them started with “just spin up this complex cloud environment,” or “assume you have a deep understanding of neural networks.” I just wanted to get something running on my laptop! I wanted to see the agent work, understand the pieces, and then iterate. This frustration fueled my desire to write this article – to bridge that gap between the high-level theory and the practical, “let’s get our hands dirty” application.
Setting the Stage: What We’re Building
Our goal today is to build a basic AI agent that:
- Visits a specified webpage.
- “Reads” the content of a specific element on that page (e.g., a stock status message).
- Compares the current content to what it saw last time.
- If there’s a change, it sends an alert (we’ll keep it simple with a print statement for now, but you could easily extend this to email or a messaging app).
Why this specific task? Because it involves core agent concepts:
- Tool Use: The agent needs a way to “browse” the internet.
- Observation: It needs to process the information it gets from the web.
- Decision Making (Simple): It needs to decide if a change has occurred.
- Action: It needs to “alert” us.
The Core Ingredients: What You’ll Need
Before we jump into the code, let’s make sure you have a few things ready:
- Python (3.9+ recommended): If you don’t have it, head over to python.org and download it.
- An LLM API Key: We’ll be using OpenAI’s models for this example because they’re widely accessible and have good documentation. You’ll need an API key from OpenAI. Make sure you have some credits!
- A Virtual Environment (Highly Recommended): This keeps your project’s dependencies separate.
- A Target Website: Pick a simple website where you want to monitor a piece of text. For demonstration, I often use a simple “status” page I’ve created myself or a product page on a major retailer (be mindful of their terms of service and don’t bombard them with requests!). For this example, let’s pretend we’re monitoring a fictional product page for an “Out of Stock” message.
Let’s get our environment set up. Open your terminal or command prompt:
# Create a new directory for your project
mkdir web_monitor_agent
cd web_monitor_agent
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
.\venv\Scripts\activate
# Install the necessary libraries
pip install langchain langchain-openai beautifulsoup4 requests
Quick breakdown of what we just installed:
langchain: This is our framework for building agents. It provides the structure and tools.langchain-openai: The specific integration for OpenAI’s models within LangChain.beautifulsoup4: A fantastic library for parsing HTML and extracting data from web pages.requests: For making HTTP requests to fetch web page content.
Now, create a file named .env in your project directory and add your OpenAI API key:
OPENAI_API_KEY="your_openai_api_key_here"
And then create a Python file, let’s call it monitor_agent.py.
Step 1: Building Our Web Scraper Tool
The core of our agent’s ability to interact with the web comes from its “tools.” A tool is essentially a function that the agent can call to perform a specific action. In our case, we need a tool that can fetch a webpage and extract specific text.
Let’s start by defining a function that can do just that. We’ll use requests to get the page and BeautifulSoup to parse it.
# monitor_agent.py
import os
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain import hub
from langchain_core.tools import tool
# Load environment variables from .env file
load_dotenv()
# Our tool for scraping web content
@tool
def get_website_text(url: str, css_selector: str) -> str:
"""
Fetches the content of a specific HTML element from a given URL using a CSS selector.
Useful for monitoring changes in specific parts of a webpage.
"""
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # Raise an exception for HTTP errors
soup = BeautifulSoup(response.text, 'html.parser')
element = soup.select_one(css_selector)
if element:
return element.get_text(strip=True)
else:
return f"Error: No element found with selector '{css_selector}' on {url}"
except requests.exceptions.RequestException as e:
return f"Error fetching URL {url}: {e}"
except Exception as e:
return f"An unexpected error occurred: {e}"
# We'll add the rest of the agent code here later
A few things to note here:
- The
@tooldecorator from LangChain is what turns our regular Python function into something an LLM can understand and call. - The docstring for the function is crucial! This is how the LLM knows what the tool does and when to use it. Be clear and descriptive.
css_selector: This is how we target a specific part of the page. If you’re unsure how to find one, right-click on the element you want to monitor in your browser, select “Inspect,” and then you can often right-click the HTML element in the inspector and choose “Copy” -> “Copy selector.”
Let’s test this tool out quickly. Add this to the bottom of your monitor_agent.py file, outside any functions:
# ... (previous code) ...
if __name__ == "__main__":
test_url = "https://www.example.com" # Replace with a simple URL you want to test
test_selector = "h1" # Or any other selector on your chosen page
print(f"Testing scraper for {test_url} with selector '{test_selector}'...")
text_content = get_website_text(test_url, test_selector)
print(f"Extracted content: {text_content}")
# Example for a more complex scenario, maybe a product stock status
# product_url = "https://some-online-store.com/product/awesome-widget"
# stock_selector = "#product-status span.stock-label" # Example selector for a stock status
# stock_text = get_website_text(product_url, stock_selector)
# print(f"Product stock status: {stock_text}")
Run your script: python monitor_agent.py. You should see the extracted text from your chosen URL and selector.
Step 2: Crafting Our Agent
Now that we have a tool, we need to give it to an agent. An agent in LangChain typically consists of:
- An LLM: The “brain” that decides what to do.
- Tools: The “hands” that perform actions.
- A Prompt: The instructions that tell the LLM its role and goal.
We’ll use LangChain’s create_react_agent which implements the ReAct pattern (Reasoning and Acting). This means the agent will “think” (reason) and then “do” (act) in a loop until it reaches its goal.
Let’s extend our monitor_agent.py:
# ... (previous code) ...
# Initialize our LLM
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0) # Using gpt-3.5-turbo for cost-effectiveness, temperature 0 for deterministic output
# Define the tools our agent can use
tools = [get_website_text]
# Get the prompt for the ReAct agent
# This prompt structure is provided by LangChain and guides the LLM to think in a ReAct manner
prompt = hub.pull("hwchase17/react")
# Create the agent
agent = create_react_agent(llm, tools, prompt)
# Create an agent executor to run the agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Our main monitoring function
def start_monitoring(target_url: str, target_selector: str, interval_seconds: int = 60):
print(f"Starting monitoring for URL: {target_url} with selector: {target_selector}")
print(f"Checking every {interval_seconds} seconds. Press Ctrl+C to stop.")
last_known_content = None
while True:
try:
print("\n--- Checking website for changes ---")
# The agent decides to use the `get_website_text` tool based on the prompt
# We're passing the instructions as the 'input' to the agent
result = agent_executor.invoke({
"input": f"Check the content of the HTML element specified by CSS selector '{target_selector}' on the website '{target_url}'. Report the exact text content found. If an error occurs, report the error."
})
current_content = result["output"] # The agent's final answer will be in "output"
if "Error:" in current_content:
print(f"Agent reported an error: {current_content}")
elif last_known_content is None:
print(f"Initial content found: '{current_content}'")
last_known_content = current_content
elif current_content != last_known_content:
print(f"!!! CHANGE DETECTED !!!")
print(f"Old content: '{last_known_content}'")
print(f"New content: '{current_content}'")
last_known_content = current_content
# Here you could add more advanced alerting (email, SMS, push notification)
else:
print(f"No change detected. Content: '{current_content}'")
except Exception as e:
print(f"An error occurred during monitoring loop: {e}")
import time
time.sleep(interval_seconds)
# Replace the __main__ block with our actual agent execution
if __name__ == "__main__":
# Example: Monitor a fictitious product's stock status
# Make sure to replace with a real URL and a real CSS selector you want to monitor!
# For testing, you can use a simple page you control or a public page with predictable elements.
# For instance, a news site's main headline (h1 or specific div) might change.
# IMPORTANT: Use a webpage you have permission to scrape and do not overload servers.
# For a real-world test, consider a simple "test" page you host or a dev environment.
MONITOR_URL = "https://httpbin.org/html" # This is a simple page with a title
MONITOR_SELECTOR = "h1" # We'll monitor the main heading
# For a more dynamic example, you might look for a specific div's text
# MONITOR_URL = "https://www.some-product-page.com/product/amazing-thing"
# MONITOR_SELECTOR = "#stock-status-message" # e.g., "Out of Stock" or "In Stock"
# You could also use a weather site for temperature updates on a specific element
# MONITOR_URL = "https://www.weather.com/weather/today/l/USNY0996:1:US"
# MONITOR_SELECTOR = "span[data-testid='TemperatureValue']"
monitoring_interval_seconds = 15 # Check every 15 seconds for testing
start_monitoring(MONITOR_URL, MONITOR_SELECTOR, monitoring_interval_seconds)
Let’s unpack the new additions:
ChatOpenAI: This is our connection to the LLM.gpt-3.5-turbois a good balance of cost and performance for this kind of task.temperature=0means the LLM will be less creative and more deterministic in its responses, which is generally what you want for agent tasks.hub.pull("hwchase17/react"): LangChain provides pre-built prompts for common agent patterns. This pulls the standard ReAct prompt, which instructs the LLM on how to reason and use tools. It’s a great shortcut!create_react_agent: This function wires up our LLM, tools, and prompt into a runnable agent.AgentExecutor: This is what actually runs the agent.verbose=Trueis super helpful because it shows you the agent’s “thought” process, including when it uses a tool and what the tool returns.start_monitoring: This function encapsulates our continuous monitoring loop. It keeps track of thelast_known_contentand compares it to thecurrent_contentthe agent retrieves.agent_executor.invoke({"input": ...}): This is how we give the agent its task. The agent then uses its brain (LLM) and its hands (tools) to figure out how to fulfill that task.
Running Our Agent!
Now, save your monitor_agent.py file and run it from your terminal:
python monitor_agent.py
You should start seeing output similar to this (the exact wording will vary based on the LLM’s thought process):
Starting monitoring for URL: https://httpbin.org/html with selector: h1
Checking every 15 seconds. Press Ctrl+C to stop.
--- Checking website for changes ---
> Entering new AgentExecutor chain...
I need to check the content of the HTML element 'h1' on 'https://httpbin.org/html'. I should use the 'get_website_text' tool for this.
Action: get_website_text
Action Input: {'url': 'https://httpbin.org/html', 'css_selector': 'h1'}
Observation: H1 Test Page
I have successfully retrieved the text content of the h1 element, which is "H1 Test Page".
Final Answer: H1 Test Page
> Finished chain.
Initial content found: 'H1 Test Page'
--- Checking website for changes ---
> Entering new AgentExecutor chain...
I need to check the content of the HTML element 'h1' on 'https://httpbin.org/html'. I should use the 'get_website_text' tool for this.
Action: get_website_text
Action Input: {'url': 'https://httpbin.org/html', 'css_selector': 'h1'}
Observation: H1 Test Page
I have successfully retrieved the text content of the h1 element, which is "H1 Test Page".
Final Answer: H1 Test Page
> Finished chain.
No change detected. Content: 'H1 Test Page'
... (continues every 15 seconds) ...
If you were to change the content of the h1 on the httpbin.org/html page (which you can’t, but imagine it was a page you controlled!), or if you point it at a page that actually updates, you would see the “!!! CHANGE DETECTED !!!” message.
This “verbose” output is incredibly useful for debugging and understanding what the agent is “thinking” and how it’s using its tools. It demystifies the process quite a bit!
My Takeaways from This Little Project
Building this simple web monitoring agent, even with its basic functionality, was a huge learning moment for me. It really solidified a few key concepts:
- Tools are Everything (for Agents): An LLM without well-defined tools is like a brilliant strategist without an army. The tools give it the ability to interact with the real world.
- Prompts Guide the Brain: The prompt isn’t just a suggestion; it’s the core instruction set for the LLM. A good prompt makes the agent effective. LangChain’s pre-built prompts are a lifesaver.
- Start Small, Iterate: Don’t try to build a general AI from day one. Solve a specific, tangible problem. This helps you understand the components before tackling more complexity.
- Debugging with Verbosity: The
verbose=Trueflag in LangChain is your best friend. It shows the agent’s internal monologue, which is invaluable for understanding why it made certain decisions or failed.
Practical Next Steps for YOU
Now that you’ve got a working (albeit simple) agent, here are some actionable ways you can expand on this:
- Try a Different Selector/URL: Point the agent at a different website and a different CSS selector. Try monitoring a price on an e-commerce site (again, be mindful of their terms!).
- Add More Tools:
- Email Tool: Create a new tool that sends an email using Python’s
smtplibwhen a change is detected. - SMS Tool: Integrate with Twilio or a similar service to send an SMS.
- Data Storage Tool: Instead of just printing, have the agent save the historical data to a simple CSV file or a database.
- Email Tool: Create a new tool that sends an email using Python’s
- Enhance Decision Making: Right now, our agent’s decision-making is pretty basic (is there a change?). You could modify the prompt to ask the LLM to analyze the *type* of change. For example, “If the price drops below $50, alert me.” This would require the LLM to parse the text it gets back and make a more complex decision.
- Error Handling: Our
get_website_texttool has some basic error handling. You could make the agent more resilient by giving it tools to retry requests or report different types of errors. - Scheduled Execution: Instead of a simple
while Trueloop, consider using a scheduler like Python’sAPScheduleror a cron job to run your script at specific times.
The world of AI agents might seem daunting, but by breaking it down into manageable chunks and focusing on practical applications, you can start building really useful things. This simple web monitor is just the beginning. The core principles you’ve learned here—tools, prompts, LLMs, and iterative development—are the foundation for much more complex agents.
Keep experimenting, keep building, and don’t be afraid to get your hands dirty! If you build something cool with this, or run into snags, let me know in the comments or shoot me a message on social. I’d love to hear about it!
Happy coding,
Emma
🕒 Published: