liveBook · Manning

LangGraph

As explained in Hugging Face in Action

LangGraph is a framework for building stateful, multi-step applications whose components communicate through graph-based state transitions.

LangGraph is a Python framework for building stateful, multistep workflows with language models, tools, and external APIs. It represents application logic as a directed graph: nodes perform computational steps, while edges determine the next step from output or state. This suits multiturn chatbots, decision trees, tool-using agents, data-enrichment pipelines, and modular conversational flows. section 9.4.1

!pip install langgraph

Install the framework before creating a graph-based agent. section 9.4.1

A practical starting point is create_react_agent(), which wraps a ReAct-style workflow. With an LLM and an empty tools list, the graph has start, agent, and end stages. The agent receives messages through invoke(), and the returned state contains the complete message history. section 9.4.2

import os
from langgraph.graph.message import add_messages
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI

Use these imports to combine LangGraph's prebuilt ReAct agent with LangChain's OpenAI chat model. section 9.4.2

The basic execution path provides:

  • A start node that accepts the request. section 9.4.2
  • An agent node that reasons over the input and produces a response. section 9.4.2
  • An end node that terminates execution and returns the result. section 9.4.2
  • A state dictionary whose messages list records the interaction and can be inspected as an agent trace. section 9.4.2

Tools extend the agent beyond its trained knowledge. A LangChain Tool supplies a name, callable function, and description; the description helps the LLM decide whether and how to invoke it. LangGraph then adds a tools node and conditional transitions, allowing the agent to search, calculate, query a database, or call another API before answering. section 9.4.3

from langchain_core.tools import Tool
from langchain_community.utilities import SerpAPIWrapper

os.environ["SERPAPI_API_KEY"] = "<SERPAPI_KEY>"          #1
serpapi = SerpAPIWrapper()                               #2
search_tool = Tool(
    name = "SerpAPI",                                    #3
    func = serpapi.run,                                  #4
description = "A search engine tool to query real-time information
                   from the web."
)

Wrap SerpAPIWrapper as a search tool for real-time web information, then pass it to create_react_agent(). section 9.4.3

Adding search and weather tools

A search-enabled agent answers a question about the 2024 US presidential election by requesting SerpAPI results and then composing an answer. A custom get_weather_info(city) function calls OpenWeatherMap, formats condition, temperature, humidity, and wind speed, and becomes a LangChain Tool alongside search. section 9.4.3 section 9.4.4

Memory requires explicit message-passing state. A State TypedDict stores messages, while Annotated[List, add_messages] tells LangGraph how to append and merge message sequences. Passing the updated state into invoke() lets later turns use earlier exchanges instead of treating every query as isolated. section 9.4.5

class State(TypedDict):
    messages: Annotated[List, add_messages]

Define the message state used to preserve conversation history across agent invocations. section 9.4.5

How Hugging Face in Action covers it

How deep it goes
Explains nodes, edges, ReAct execution, tools, message state, and memory, while focusing only on core agent-workflow components.
Examples
Demonstrates a Bill Gates question, arithmetic, election search, weather lookup, custom tools, and follow-up conversation.
What it assumes
Assumes Python, an LLM provider, and API credentials while teaching graph structure, tool integration, ReAct flow, and memory incrementally.
What you can do after
Build agents with create_react_agent(), connect custom or external tools, inspect traces, and maintain conversations through explicit state.
How it connects
Connects LangChain, ReAct agents, tools, external APIs, directed graphs, message history, and stateful chains.

Also explained in

Compare how 7 books explain LangGraph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.