
This is the cutting-edge of AI, far beyond a simple pipeline. An Agent is an LLM (like ChatGPT) that has been given tools and can figure out which tool to use to answer your prompt.
You can ask it a complex question, and it will write and run Python code on its own, using other AI models, to get the answer.
Step 1: Installation
You need the transformers library and a special agent-related library.
pip install transformers accelerate
Step 2: The Code
We will use the HfAgent. This agent has access to dozens of models on the Hugging Face hub.
from transformers import HfAgent
# 1. Initialize the agent (it connects to the Hugging Face inference API)
# You may need to provide your HF read-token for this
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
print("Agent initialized. You can ask me to do things!")
# 2. Ask it a complex, multi-modal question
prompt = "Generate an image of a cat. Then, tell me what is in that image."
# 3. Run the agent!
result = agent.run(prompt)Step 3: The Result
When you run this, the Agent will:
- Think: “The user wants an image. I need to find an image generation tool.”
- Act: It will call a text-to-image model (like
stable-diffusion) to generate the picture. - Think: “Now the user wants me to describe the image. I need an image-to-text tool.”
- Act: It will feed the new image into an image classifier or captioning model.
- Answer: It will give you the final text result, e.g., “I have generated an image. This image contains a tabby cat.”
You just commanded an AI that used other AIs to solve your problem. This is the future of programming.
Key Takeaways
- Hugging Face Agents are advanced LLMs that can choose tools to answer complex questions.
- To use them, install the transformers library and a special agent-related library.
- The HfAgent can access numerous models on the Hugging Face hub for different tasks.
- The agent processes information by thinking about what tool to use, acting on it, and providing a final answer.
- This approach allows AI to leverage other AIs for more effective problem solving.





