How to create a weather tool for an AI agent?
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
requestsis the only outside library here, and it is not part of Python itself. It is listed in the project'spyproject.toml, souv syncinstalls it for you.- The first call goes to ipinfo.io. It answers with a small
JSONobject: plain text that stores data as labelled values, such as{"city": "Hong Kong"}, so a program can read one field at a time.locholds the latitude and longitude as one string,"22.3193,114.1694", which is why the code splits it on the comma. - That position comes from the public IP address, so it is accurate to a city, not to a street. For a weather question that is enough. The call also sends your public IP address to ipinfo.io, which is the only thing about you that leaves the machine.
- The second call goes to Open-Meteo. The
paramsblock makes the question precise: current conditions only, the temperature two metres above the ground, in Celsius, with the time zone taken from the coordinates. timeout=10appears twice. Without it, a slow network leaves the agent waiting with no answer. With it, each call gives up after ten seconds and raises an error instead of hanging.- The function returns a sentence:
Current: 24.6°C. The model gets text it can quote directly. - The last two lines call the function and print the result, so the tool can be checked from a terminal before any agent is involved.
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
- Read-only: it reads public data and changes nothing.
- Easy to verify: any weather app gives you a second opinion.
- Immediately useful: “shall I bring an umbrella?” becomes a question the agent can answer with a fact.
Further reading
- Open-Meteo — API documentation — the full list of current, hourly and daily variables you can request.
- ipinfo.io — developer documentation — the fields in the location response, and the rate limits without a token.
- requests — documentation — timeouts, parameters and error handling for HTTP calls.