9 Programming Agents

 

This chapter covers

  • Introduction to agents
  • Creating simple agents using smolagents
  • Creating enterprise-grade agents using LangChain
  • Creating enterprise-grade agents using LangGraph

Up to this point, you've worked with Hugging Face Transformers to tackle a variety of tasks, ranging from natural language processing to image analysis and computer vision. Each of these tasks typically involves a specific, specialized model—for instance, a translation model for converting text between languages, or an image captioning model to generate textual descriptions of images.

While using specialized models works well for clearly defined tasks, it becomes increasingly difficult to manage workflows when the tasks are ambiguous, multi-step, or require dynamic decision-making. This is where agents come into play. Agents use large language models (LLMs) not just to perform tasks, but to reason, plan, and delegate—breaking down complex problems into smaller subtasks and calling appropriate tools or models to complete them.

Now, you will explore the concept of agents and learn how to build one yourself. In particular, we will focus on constructing agents using two practical and widely applicable frameworks:

  • smolagent – A lightweight, minimalistic agent framework for quick experimentation.
  • LangGraph – A powerful framework for building stateful, multi-step workflows involving language models and tools, ideal for handling conversations and decision trees.

9.1 What are Agents?

9.2 Developing agents using smolagents

9.2.1 Using built-in tools - DuckDuckGoSearchTool

9.2.2 Using built-in tools — PythonInterpreterTool

9.2.3 Writing your own custom tools

9.3 LangChain Agents

9.3.1 Using the Built-in Tool class

9.3.2 Using custom tools

9.4 Developing Agents using LangGraph

9.4.1 What is LangGraph?

9.4.2 LangGraph agent basics

9.4.3 Using LangGraph with tools

9.4.4 Using LangGraph with custom tool

9.4.5 Using LangGraph with memory

9.5 Summary