Hugging Face showed how to build a weather AI agent on smolagents in 15 minutes
Hugging Face simplified entry into the world of AI agents: with the smolagents library, you can build a weather assistant in about 40 lines of Python without…
AI-processed from KDnuggets; edited by Hamidun News
smolagents from Hugging Face demonstrates that the first useful AI agent can be built without heavy frameworks and hundreds of lines of code. In the walkthrough, the author constructs a weather assistant in Python that independently decides which tools to call, retrieves data from the internet, and returns a ready-made answer.
What
Makes smolagents Different The core idea here is not about weather, but about the approach. smolagents is a library for so-called code agents, where the model doesn't simply select a tool from a list, but writes small fragments of Python code to link steps together. Instead of JSON schemas and lengthy orchestrators, the agent is given a goal like "find out the weather in London," and then it decides which tool to call, in what order, and how to assemble the final answer.
This format makes agent behavior clearer and more flexible. Code better expresses loops, conditions, and data transformations than text prompts with rigid rules. For beginners, it's also a convenient entry point: the library is open source, lightweight, and doesn't require setting up complex infrastructure.
So smolagents looks not like yet another experimental layer on top of LLM, but as a practical way to quickly understand the mechanics of autonomous AI scenarios.
How to
Build an Agent The demonstration 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 suggested to be stored in the `HF_TOKEN` environment variable, and for those who don't want to set anything up locally, Google Colab works. Already at this stage, the main point of the material becomes clear: for your first agent, you don't need a large stack, just Python, access to a model, and one external function. * `@tool` turns an ordinary Python function into a tool that the agent can call on its own.
- The `get_weather(city: str)` function calls the `wttr.in` service and returns a short forecast for the specified city.
- As the model, `InferenceClientModel` is used with `Qwen/Qwen2.5-Coder-32B-Instruct` and a token from `HF_TOKEN`.
- The agent itself is created through `CodeAgent`, to which you pass a list of tools, the model, and the `add_base_tools=False` flag for minimal configuration. After that, you simply run the task with an ordinary phrase, for example, asking the agent to tell you the weather in Paris and Tokyo. Next comes the most interesting part: the model reads the prompt, understands that it has the `get_weather` tool, writes an internal Python script with two calls, executes it in an isolated environment, and returns an already assembled answer to the user. An important detail is the docstring of the function. It's through this that the agent understands what the tool does, what arguments it accepts, and when it should be used.
How to Extend the Scenario The example doesn't end with weather.
The article shows how to add a second tool `save_to_file` that 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 a local environment: write the result, for example to `london_weather.txt`.
This transition is important because it's the combination of small tools that turns the demonstration into the beginning of a real workflow. From this same template, more useful cases easily grow: connecting a search API, working with a database, assembling simple research, or even managing a browser. The author specifically emphasizes that the core logic here is very compact: despite all the autonomy effect, the main part of the example fits in less than twenty lines, and the entire script stays within about forty.
The entry barrier to agent scenarios is thereby significantly lowered: you think not about the framework, but about what specific actions you need to give the model.
What
This Means smolagents demonstrates an important shift: autonomous assistants can now be built not only in large teams with their own orchestration layer, but also solo in one evening. For developers, it's a quick way to prototype AI automation in familiar Python, and for a product, it's a chance to test a useful scenario before investing in complex architecture.
Want to stop reading about AI and start using it?
AI News is a curated feed of AI/tech news. Hamidun Academy teaches you to use AI systematically in your work.