English 繁體中文 简体中文

How to create a weather tool for an AI agent?

Howard Pong Yuen Lam · AI Notes · November 8, 2026 · 4 min read

An AI agent cannot step outside, and it does not know where you are. A weather tool answers both questions in one call. Every device on the internet has a number, called an Internet Protocol (IP) address, and that number is registered to a region, so a lookup service can turn it into an approximate location — a city, never a street. The tool then asks a free weather service for the current temperature in that city. Neither service needs a sign-up or a key.

The tool

This is tool02_weather.py as it stands:

# https://open-meteo.com/
import requests
def get_weather_from_ip():
    """
    Get the current temperature in Celsius for the user's location
    using the IP address and return it as a string.
    """
    location = requests.get("https://ipinfo.io/json", timeout=10).json()
    latitude, longitude = location["loc"].split(",")

    params = {
        "latitude": latitude,
        "longitude": longitude,
        "current": "temperature_2m",
        "temperature_unit": "celsius",
        "timezone": "auto",
    }

    weather_data = requests.get(
        "https://api.open-meteo.com/v1/forecast",
        params=params,
        timeout=10,
    ).json()

    return f"Current: {weather_data['current']['temperature_2m']}°C"

result = get_weather_from_ip()
print(result)

What each part does

Running it on your own computer

Download the zip file and unzip it to a folder. The zip holds tool02_weather.py, pyproject.toml, and README.md. Open the folder in Visual Studio Code, then run these two commands in the Terminal:

uv sync
uv run tool02_weather.py

uv sync only needs to run the first time. It creates a local environment with the one package this project needs, so nothing is installed into the rest of your computer.

You should see a single line, for instance Current: 24.6°C. Compare it with the weather app on your phone.

If the line never appears, the cause is almost always one of three things: no internet connection, the IP address is being rate-limited by ipinfo.io, or Open-Meteo is briefly unavailable. Both services are free for light use, which is what makes them suitable for a classroom, but free also means no guarantee of uptime.

Why this is a good tool to start with

Further reading