Hugging Face shows how to build a weather AI agent with smolagents in 15 minutes

Hugging Face has lowered the barrier to AI agents: with the smolagents library, you can build a weather assistant in roughly 40 lines of Python, without a heavy framework. In the example, the agent fetches forecasts through wttr.in, uses a custom tool, connects to a Qwen model via an HF token and decides for itself which calls to make for cities such as Paris and Tokyo. The approach rests on code agents: instead of picking from a fixed list, the model writes small Python snippets to chain steps together.

AI-processed from KDnuggets; edited by Hamidun News
Hugging Face shows how to build a weather AI agent with smolagents in 15 minutes
Source: KDnuggets. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceKDnuggets ↗
Editor-in-Chief
Key metric #1`@tool` turns an ordinary Python function into a tool the agent can call by itself.
The `get_weather(citystr)` function queries the `wttr.in` service and returns a short forecast for the requested city.
Key metric #3The model is `InferenceClientModel` with `Qwen/Qwen2.5-Coder-32B-Instruct` and the token from `HF_TOKEN`.

smolagents from Hugging Face shows that you can build a first useful AI agent without heavy frameworks or hundreds of lines of code. In the walkthrough, the author builds a weather assistant in Python that decides on its own which tools to call, pulls data from the internet and returns a finished answer.

What sets smolagents apart

The key idea here is not the weather but the approach. smolagents is a library for so-called code agents, where the model does not simply pick a tool from a list but writes small fragments of Python code to link steps together. Instead of JSON schemas and long orchestrators, the agent gets a goal such as "find out the weather in London" and then decides for itself which tool to call, in what order, and how to assemble the final answer.

This format makes the agent's behavior easier to follow and more flexible. Code expresses loops, conditions and data transformation better than text prompts with rigid rules. It is also a convenient entry point for beginners: the library is open, lightweight and does not force you to set up complex infrastructure. That makes smolagents look less like yet another experimental layer on top of LLMs and more like a practical way to quickly grasp how autonomous AI workflows work.

How to build the agent

In the demo, everything starts with very basic setup: create a project folder, set up a virtual environment and install just three packages: `smolagents`, `requests` and `python-dotenv`. The Hugging Face token is meant to be stored in the `HF_TOKEN` environment variable, and anyone who does not want to install anything locally can use Google Colab. Already at this stage the main thesis of the piece is clear: a first agent does not need a big stack, just Python, access to a model and one external function.

  • `@tool` turns an ordinary Python function into a tool the agent can call by itself.
  • The `get_weather(city: str)` function queries the `wttr.in` service and returns a short forecast for the requested city.
  • The model is `InferenceClientModel` with `Qwen/Qwen2.5-Coder-32B-Instruct` and the token from `HF_TOKEN`.
  • The agent itself is created via `CodeAgent`, which receives a list of tools, the model and the `add_base_tools=False` flag for a minimal configuration.

After that, all that remains is to run a task with a plain sentence, for example asking the agent for the weather in Paris and Tokyo. Then the interesting part happens: the model reads the prompt, realizes it has a `get_weather` tool, writes an internal Python script with two calls, executes it in an isolated environment and returns a ready-made answer to the user.

One important detail is the function's docstring. It is what tells the agent what the tool does, which arguments it accepts and when it should be used.

How to extend the scenario

The example does not end with weather. The article shows how to add a second tool, `save_to_file`, which saves a text report to a file. After that, the agent can not only call an external API but also take the next step in the local environment: write the result, for example, to `london_weather.txt`. This step matters because it is the combination of small tools that turns a demo into the seed of a real workflow.

More useful cases grow easily from the same template: connecting a search API, working with a database, assembling simple research or even controlling a browser. The author separately stresses that the core logic is very compact: despite the impression of autonomy, the main part of the example fits in fewer than two dozen lines, and the whole script stays at roughly forty. This sharply lowers the barrier to agent scenarios: you think not about the framework but about which actions to give the model.

What this means

smolagents points to an important shift: autonomous assistants can now be built not only by large teams with their own orchestration layer but also by one person in a single evening. For developers, it is a fast way to prototype AI automation in familiar Python; for a product, it is a chance to test a useful scenario before investing in a complex architecture.

Story

Key Questions Answered

What sets smolagents apart?

The key idea here is not the weather but the approach. smolagents is a library for so-called code agents, where the model does not simply pick a tool from a list but writes small fragments of Python code to link steps together. Instead of JSON schemas and long orchestrators, the agent gets a goal such as "find out the weather in London" and then decides for itself which tool to call, in what order, and how to assemble the final answer. This format makes the agent's behavior easier to follow and more flexible. Code expresses loops, conditions and data transformation better t

How to build the agent?

In the demo, everything starts with very basic setup: create a project folder, set up a virtual environment and install just three packages: `smolagents`, `requests` and `python-dotenv`. The Hugging Face token is meant to be stored in the `HF_TOKEN` environment variable, and anyone who does not want to install anything locally can use Google Colab. Already at this stage the main thesis of the piece is clear: a first agent does not need a big stack, just Python, access to a model and one external function. `@tool` turns an ordinary Python function into a tool the agent can c

How to extend the scenario?

The example does not end with weather. The article shows how to add a second tool, `save_to_file`, which saves a text report to a file. After that, the agent can not only call an external API but also take the next step in the local environment: write the result, for example, to `london_weather.txt`. This step matters because it is the combination of small tools that turns a demo into the seed of a real workflow. More useful cases grow easily from the same template: connecting a search API, working with a database, assembling simple research or even controlling a browser. T

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…