9 Programming Agents
Modern AI applications often require more than a single specialized model; they need systems that can understand natural language, reason through multi‑step tasks, choose and use the right tools, and synthesize results. This chapter introduces such systems—agents—highlighting how they decompose problems, plan actions, call external tools or models, and return coherent answers. It frames agents as a practical response to ambiguous or dynamic workflows and sets the stage for building them with two complementary approaches: lightweight experimentation with smolagents and more feature‑rich, stateful designs using LangChain and LangGraph. By the end, you learn the essentials of agent design, tool integration, and managing state and memory for multi‑step reasoning.
With smolagents, you build code‑executing agents that pair an LLM with tools to act programmatically. The chapter walks through creating search and computation agents using built‑in tools like DuckDuckGoSearchTool and PythonInterpreterTool, explains the agent’s run loop, and shows how to swap LLM backends—from hosted inference (via Hugging Face) to local or third‑party providers (e.g., through LiteLLM with Ollama or OpenAI). It emphasizes practical concerns such as inference credits and latency, and demonstrates creating custom tools with a simple decorator and clear docstrings, illustrated by a weather retrieval function that calls an external API. A key takeaway is that tool choice and LLM selection significantly affect quality, speed, and reliability.
For enterprise‑grade use, the chapter turns to LangChain and LangGraph. LangChain standardizes tool use through the Tool interface and wraps popular utilities (e.g., web search) so agents can follow a ReAct‑style loop—reason, act with tools, observe, and answer—using zero‑shot prompting. It then extends the same ideas in LangGraph, where agent logic is represented as a directed graph with nodes for the agent, tools, and flow control, enabling robust, multi‑step, and conditional workflows. Examples show how to add real‑time search for up‑to‑date answers, define custom tools (such as weather), and incorporate memory via message state so agents can sustain context across turns. Together, LangChain and LangGraph provide a path from quick prototypes to stateful, production‑ready agents that coordinate reasoning, tools, and conversation history.
Roles of an agent
The agent will automatically use the default tools if you set the add_base_tools parameter to True
The agent working on the first step to solving the question
The agent displaying its execution log
The final answer returned by the agent
Using the agent to write the code to find the 10th Fibonacci number
Getting the agent to generate the Fibonacci numbers up to 100
The result from the agent when you asked for the weather of a city/country
The graph for the agent we are building
The steps taken by the LangGraph agent
The graph of the agent together with the external tool
The agent works with the tool to return the answer
Summary
- An agent is a specialized system designed to perform tasks autonomously by combining language understanding, reasoning, and tool usage.
- smolagents is a lightweight and flexible framework designed to make agent development simple and approachable.
- DuckDuckGoSearchTool is a lightweight and privacy-focused search tool that allows your agent to find information from the web quickly and efficiently.
- Using the HfApiModel() object, you can use a model hosted on Hugging Face server.
- The CodeAgent class creates a smolagents agent.
- To use a locally running LLM through Ollama, use the LiteLLMModel class, which connects to the Ollama server and allows your agent to send prompts and receive responses directly from models running on your machine.
- The PythonInterpreterTool class allows the agent to execute Python code dynamically.
- Use the @tool decorator above your function to design it as a custom tool.
- SerpAPI is a real-time search API that allows developers to programmatically access and extract search results from search engines like Google, Bing, Yahoo, YouTube, Amazon, and more.
- You can create a LangChain agent using the initialize_agent() function.
- LangGraph is a flexible and feature-rich framework designed specifically for building complex, stateful agents.
- You can use the create_react_agent() function to create a ReAct-style agent.
- ReACT is a method where the agent thinks step-by-step, calls tools if needed, observes results, and continues reasoning.
- In LangGraph agents, you create a Tool object and use it as a custom tool.
- You can maintain memory in your LangGraph agent by creating a State object and passing it to your agent.
Hugging Face in Action ebook for free