{
  "video_id": "dSGS6-iGhyo",
  "channel_slug": "huggingface",
  "channel_handle": "huggingface",
  "title": "Building Agents with Smolagents",
  "duration_seconds": 1151.0,
  "url": "https://www.youtube.com/watch?v=dSGS6-iGhyo",
  "upload_date": "",
  "transcript": "Building agents can seem overwhelming, and the objective of the series is precisely to\nmake them approachable to you. In the previous video we covered how to create agents completely\nfrom scratch, that is to say without any frameworks like LangGraph or LangChain or OpenAI.\nAnd the idea behind that was to help you see what's actually going on under the hood.\nIn this video we're going to be creating what you see right here on the screen,\nwhich is a complete agent with a graphical user interface that is capable of calling multiple tools one after another to complete a task.\nSo in this example, I asked it, generate an image of the Chancellor of Germany in 2010 playing the flute.\nAnd as you can see, we have the first step where it figured out who was the Chancellor of Germany in 2010 using the Web Search tool.\nThen it used the generate image tool once it figured out that it was Angela Merkel to generate this image.\nand we're going to be building it using small agents, a library by Hug and Face that makes it\nvery minimalist and very simple to create agents by abstracting away the things that were a little\nbit too verbose that we had to code in the previous video while still showing you what's\ngoing on behind the scenes in the agentic loop so that you see really what is going on.\nSo by the end of this video you're going to have this nice little chatbot right here that you're\ngoing to be able to add to your portfolio. And in the coming videos, we're going to be exploring more\ndifficult and sophisticated ways of creating agents, including how to create them with other\nlibraries such as LangChain and OpenAI. So let's get right into it. Okay, so first let's go over the\nchapters that we're going to cover right here. First, we're going to go over the setup, which is\nnow. Then we're going to initialize our agent, and we're going to create a tool. Then we're going to\nuse open source tools from the hub. And then finally, we're going to create our nice little\ngraphical user interface for our chatbot. Okay. So for spotters, the first thing to do is you're\ngoing to have to install small agents like this. So pip install small agents. And then after that,\nwe're going to have to define our API keys. Since I'm going to be showing you how to do this with\nboth open models from HugginFace and also with proprietary models, I'm going to be adding my\nHugging Face token, then my OpenAI key and my Anthropic API key. Okay, just as a reminder,\nif you want your Hugging Face token, all you have to do is go right here to your profile,\nyou go right here to Access Tokens, and right here you're going to click on Create New Token,\nthen you're going to just give it a name, just going to call it Temp2, click on Make Calls to\nInference Providers, so that to make sure that this token is able to call our models and just\ncreate token and this is the one that we're going to be using. Same thing for OpenAI and Anthropic,\njust go to their developer console and get your API key and then we can continue. Now we can\nactually just start initializing our agent. Now let's initialize our agent and it's very\nstraightforward. Now that you have small agents installed, all you're going to have to do is to\nimport tool calling agent which is going to be the abstraction that is going to coordinate our\ntool calls and our prompts. After that, we're going to import Web Search Tool because this is a\nbuilt-in tool that already comes with small agents that allows you to search the web.\nAnd we're going to include Inference Client Model, which will allow us to call open source models\nfrom Hug and Face, which are, of course, much more affordable than closed models like OpenAI or\nAnthropic, and you have many more options. I'm going to show you how to select one in just a\nmoment. After that, we're just going to initialize our model. So model equals inference client model\nlike this one. And we're going to pass in our API key that we just got from Hug and Face. Okay.\nAfter that, you're going to have to take two different parameters. The first one is the model\nID that you want to call and then the provider that you want to call. Remember that inference\nproviders is just a router that allows you to call any model from any provider that has it available.\nSo for example, Grok, Novita, Together, Cerebras, etc.\nAnd what we're going to do is we're going to go right here back to Hug and Face,\nand we're going to click right here on Models.\nAnd right here, you're going to select the model that you want to use.\nI want to go for Text Generation.\nThat's going to filter for only text generation models, like large language models.\nAnd we're going to go right here and click on Inference Available,\nbecause we're going to be using inference providers.\nSo we only want to use models that are available through this API.\nAnd from here, you can select any model you want.\nIn my case, I would probably use Kimi K2 thinking, but I'm actually going to go for the instruct\nversion right here.\nSo I going to go for Kimi instructors to have this demo go faster and not have to spend time on waiting for reasoning tokens And once you have the model that you want to use you going to go right here to compare providers\nto make sure that your provider actually supports tool calling, because we're going to be using this\nas an agent and agents require function calling or tool calling. So just go right here and make sure\nto pick a provider that actually supports tools. And right here, pretty much every provider that we\nhave supports function calling. And right here you can also see the pricing. And as you can see,\nit is much more affordable than OpenAI. To give you an idea, this one right here, Kimike to\ninstruct costs right here, for example, 50 cents and $2 per million tokens on input and output.\nAnd compared to OpenAI, for example, it is $1.75 per million tokens input and $14 per million\ntokens and output. It's ridiculously more affordable and it will work great as you are\ngoing to see. Right here you can also take a look at the context window that it has available,\nthe latency, and of course that it supports tool calling. So this is the one that we're going to\nbe using. I'm just going to click right here to copy the model ID. It's going to come right here\nand I'm going to paste the model ID right here and the provider and there we go. So Moonshot AI,\nKimi K2 Instruct is the model ID and the provider is going to be Novida. Now the next thing to do\nis to actually initialize the agent. Now this right here only connects to the inference provider API\nto be able to call the LLM, but in order to actually initialize the agent, we're going to\nbe initializing it with the tool calling agent class. And then we're going to give it tools. So\nin this case, we're going to give it the web search tool that is a pre-built tool that comes\nwithin small agents, as I mentioned before. The model that we're going to be using is the one that\nwe defined above. And now we're going to be able to call the agent like this. So agent.run to call\nour agents. And I'm going to query it, who is the current president of France? And I'm just going to\ngive it some context as of the current date to make sure that it searches the web correctly.\nAnd as you can see, it chose to use a tool call using web search. And the query was current\npresident of France as of December 2025. It got a few observations, as you can see right here,\nThey are logged right here. And then after that, it just responded with the final answer saying\nEmmanuel Macron is the current president of France as of December 2025. So there you go.\nAs you can see, it is incredibly simple to create an agent with small agents like this. And it is\nalso very educational because it logs pretty much all of the thinking process that goes behind the\nscenes that we had previously coded from scratch ourselves. Okay. And now let me show you real quick\nhow you can run this with other model providers such as OpenAI and Anthropic. Okay, so for OpenAI\nwhat we're going to do is first we're going to have to import OpenAI model from small agents\nand you're going to just load it like this. So OpenAI model, you're going to pass in your API\nkey from OpenAI this time and you're going to pass in the model ID from OpenAI that you want\nto use. In this case I'm just going to show this to you using GPT-5 mini and let me just comment\nout the other model right here. I'm going to rerun the whole thing just to show you how it works.\nAnd as you can see, it started the same task now with OpenAI GPT-5 Mini. Now in order to run\nAnthropic, we're going to use also this OpenAI model class, but we're going to pass in an API\nbase. Okay. So what happens right here is that this class right here, you can basically modify\nit to take another OpenAI-like API. And since Anthropic is OpenAI-like, all you have to do is\nadd the base from Anthropic right here. So api.anthropic.comv1, then just add the model\nID that you want to use. In this case, let's use CloudSonnet 4.5 and just execute it. And it will\ndo exactly the same thing right here. Okay. Now I'm going to stick to open models for now,\njust to continue this demo, but feel free to use any model you want. You can of course\ndo your experiments with other OpenAI-like APIs. So let's go to the next part where we're going to\ncreate our own tools. All right, now let's create a tool and we're going to be creating a very fun\ntool. We're going to be using this open source model for image generation. In case you want to\nknow again just how to find out good models for image generation, you go back to models.\nI'm going to go right here to image to text to image.\nAnd in case you were not familiar with it, this is the hottest model of the moment for image generation.\nSuper good and super fast.\nSo I'm going to use this one.\nAnd what I going to do is I just going to use it right here So what you going to want to do is to import tool from small agents and this is going to be your decorator for your function that is going to become your tool right Remember\nthat in the previous tutorial we created our function and then also we had to create our own\nschema and all that. It was very verbose and a lot of code and a lot of opportunities to get things\nwrong. In this case just by adding a very nice little decorator like this one you have everything\non for yourself. And what we're going to do is we're going to create a function called generate\nimage. It is very important to add type hints right here with this kind of frameworks, be it\nsmall agents, LangChain, OpenAI, etc. All of the agent frameworks do recommend or require you to\nadd very descriptive type hints right here. It is very important to also add a description to your\ntool because this is the description that your agent is going to be able to see and depending on\nthis description it is going to decide whether or not this tool is useful for its task right and\nwhat you're going to want to do is you're going to import inference client for Hugginface Hub.\nNow Hugginface Hub comes with small agents but in case you're not using small agents you're going\nto have to do pip install. I mean of course this is going to go like that but we're not going to do\nit right now because it came installed with small agents and we're going to also just add our import\nOS right here. Since we're going to be pushing later this tool to the hub we have to add the\nimports inside the tool for it to work right otherwise it's going to give us some errors.\nBut if you're not going to be pushing your tool to the hub you don't have to import this inside\nthe function itself. And then after that you can either take a look at right here inside the model\ncard at code snippets to make sure that you're using the model correctly. Here you can see which\nproviders are available. So here we have file, replicate, wave speed. In my case I'm going to do\nthis with file and I'm going to be using this text to image method from the client. And that's\nexactly what I did right here. So I initialized my inference client with my provider file AI\nand passed in my API key as you see right here and my image is precisely this one right here.\nSo I have my client, I ran the method textToImage, passed in the prompt which is going to be the input\nto this function and I say which model I want which is this one right here. Okay now let me just show\nyou how it works. I'm just going to test it. Generate an image of the current president of\nfriends drinking coffee. So let's execute this and see what we get returned. And there we go.\nHere we have the image of a random guy that looks a little French and the model deems that he is\nmost likely the president of France. Now I'm going to show you how to get this a little bit more\naccurate with agents, but as you can see, it is a great image generation model and it is super\nfast. So this is the tool that we're going to be giving our agents so that it is capable of creating\nimages for us. So let's add this tool to our agent. I'm going to go right here, just going to\npaste this right here. For the tools that are imported from small agents, the ones that come\nby default, you're going to have to initialize them like this, so don't forget the parentheses.\nFor the ones that you define yourself, all you're going to have to do is pass it like that.\nAnd let's actually test it out. So I'm going to say the current date, just to give it a little bit\nof context, what is the current date today, and generate an image of the current president of\nFrance drinking coffee. Then just return only the image. And there we go. The current date is this\none right here. Generate an image with the current president of France. And of course, the first thing\nthat our model did was web search. So it was like, okay, so who is the current president of France in\n2025? It now knows that the current president is Emmanuel Macron. And now it can generate an image\nof Emmanuel Macron. And as you can see, we have the actual president right there, which is much better\nthan this image right here that we got from just calling the model. In this case, we called an agent\nwho was able to figure out who was the actual person that we were referring to and then generating\nthe image with the image generation tool. So that is the kind of multi-step thinking that agents are\ngreat for. And I would invite you to create your own tools for checking your email, maybe even\nwriting your own emails, or interacting with other services that you currently use that have APIs\nall you have to do is put them inside a function and then just add it and\ndecorate it as a tool and add it to your agent and now your agents going to be\nable to help you solve all of your all of these problems okay and just very\nquickly I just wanted to give you an idea of what you can do with this in the ecosystem of the hub is that once you have created a tool you can actually push it to the hub like this so if you have created a tool and\ndecorated it with the tool decorator you already have this method within it called push to hub\nand you can run it just give it your username slash the name that you want to give your tool\nand then give your token right here again now something important to keep in mind is that if\nIf you're going to be doing this, your token has to have this right access to constants and settings of repos in order for this to work.\nAnd once that is done, you're going to be able to get that tool directly from the hub without having to define it inside your code base.\nJust a very quick shortcut if you want to use that.\nAnd something really cool as well is that you're also capable of using spaces as tools.\nNow in case you're not familiar with spaces, spaces are these small apps that live within\nHug and Face that are usually for showcasing models, which means that there are already a lot\nof models that are exposed as spaces, and you can very easily take one existing space and use it as\na tool. Let me show you how you can do that. So let's suppose that for example you want to use\nanother image generation model. Let's suppose that you want to use Flux. So let's suppose that you\nwant to use this one right here which is flux1dev and this is a space where you can basically just\ntest the model out so you can just generate an image from flux. Something super cool about this\nis that you can run an existing space like this one right here as a tool. So let me show you how\nto do it. You're going to go right here and you're going to go from small agents import tool\nand then you're going to do tool.from space and then you're going to pass in the space id like\nI did right here. In this case I am using blackforestlabs flux.1 def like this so you can\njust copy it like this. You go back right here and you paste it right here. You give it a name\nand then a description for what this tool is supposed to do. So in this case generate an\nimage from a prompt and then just initialize it just like we did before and just run your agent\nlike we did just before so I can ask it generate an image of the current top artist on Spotify\nplaying the trumpet and just like before it used the web search tool to get the top artist on\nSpotify in 2024 and then it used this generate image tool from the space that we imported and\nasked for Taylor Swift playing a golden trumpet blah blah blah and there you go you have Taylor\nSwift playing the trumpet okay so there you go this is a pretty good way to test out models and\nhow they would work as tools so now as I promised before let's create our graphical user interface\nchatbot. All right now behind the scenes what I did is I reinitialized my agent with that\nZ-image turbo image generation tool because flux is a little bit slower but everything's the same\nSo small agents actually comes with a very nice built-in user interface with Gradio that you can\nspin up with just this very quick command right here. So what you're going to do is from small\nagents import Gradio UI. Then Gradio UI is going to take a first parameter of agent and then you're\ngoing to want to set reset agent memory to true so that this one right here does not build on top\nprevious runs that you were doing in your notebook. And then all you do is your Gradio UI.launch.\nAnd there we go. Here is my nice little interface that I showed you before. And actually this also\nspins up a live version of it that I can access just by clicking right here. And here you go.\nThis is exactly what I showed you before at the beginning of the video. And now we can test it\nagain just by sending a prompt right here so the current date again generate an image of the current\nPM of England this time so let's test it out it is going to stick to Kimi K2 just like before so\nas you can see right here we have a couple of tool calls it used web search to get the current\nprime minister of England and then it used the tool generate image and then by the end it gave\nme the answer right here. So there you go. That is how you can start debugging your agents,\nscaffolding them, and actually testing them out and showcasing them to other people in your team\nusing Gradio UI right here. I hope that you enjoyed this video. In the next part of the series,\nwe're going to be going into more advanced SDKs and agent frameworks such as OpenAIs and LimeChain.\nbut let me know what you want to continue learning about on agents and we'll be sure\nto add it to the series. Thanks a lot for watching and I'll see you in the next one.",
  "transcript_chars": 19457,
  "ingested_at": "2026-05-15T10:41:28.499307+00:00",
  "source": "channel",
  "yt_meta": {
    "view_count": 10990,
    "like_count": 439,
    "channel_id": "UCHlNU7kIZhRgSbhHvFoy72w",
    "categories": [
      "Education"
    ],
    "tags": [
      "agents",
      "ai agents",
      "smolagents",
      "smolagents tutorial",
      "smolagents huggingface",
      "ai agents open source"
    ]
  }
}