AI Hints
Home›LangGraph Tutorial›Agent Practice and Routing
LangGraph · Lesson 37 of 120

Practice: Bookshop Agent

Tools in a graph.

⏱ 2 min read▶ Video at 38:50🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Let's practise. We'll build the bookshop agent again, from memory, with all three tools and a system prompt. Then ask it a question that needs every tool.

Practice: Bookshop Agent
From the video: Practice: Bookshop Agent
  • Build it again
  • Three tools + a prompt
  • One question, every tool

The bookshop agent

The agent node adds the system prompt, then calls the model with tools. The rest is the loop you know. Tool node, tools condition, and the edge back. We ask about Circe's author, stock, and price. Run it. The agent calls all three tools, and answers in one friendly reply.

bookshop_agent.pypython
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.prebuilt import ToolNode, tools_condition
from shop_tools import check_stock, get_price, get_author

tools = [check_stock, get_price, get_author]
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0).bind_tools(tools)
SYSTEM = ("system", "You are the Riverside Books assistant. Use the tools. Be brief and friendly.")

def agent(s: MessagesState):
    return {"messages": [model.invoke([SYSTEM] + s["messages"])]}

b = StateGraph(MessagesState)
b.add_node(agent); b.add_node("tools", ToolNode(tools))
b.add_edge(START, "agent"); b.add_conditional_edges("agent", tools_condition); b.add_edge("tools", "agent")
q = "Who wrote Circe, is it in stock, and how much is it?"
out = b.compile().invoke({"messages": [{"role": "user", "content": q}]})
print("tool calls:", sum(len(m.tool_calls) for m in out["messages"] if m.type == "ai"))
print(out["messages"][-1].text)
Output
tool calls: 3
*Circe* was written by Madeline Miller. It costs £10.99, but unfortunately, we are
currently out of stock!
Also uses shop_tools.py
from langchain.tools import tool

BOOKS = {
    "The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
    "Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
    "Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}

@tool
def check_stock(title: str) -> int:
    """Copies of a book in stock at Riverside Books."""
    return BOOKS.get(title, {}).get("stock", 0)

@tool
def get_price(title: str) -> float:
    """Price of a book in pounds."""
    return BOOKS.get(title, {}).get("price", 0.0)

@tool
def get_author(title: str) -> str:
    """Author of a book."""
    return BOOKS.get(title, {}).get("author", "unknown")

What you practised

You built a complete agent from scratch. A system prompt in the node, a model with tools, a tool node, and the loop. Try adding a fourth tool, like a delivery estimate, and asking a question that needs it.

keyvalue
Promptin the node
Modelbind_tools
LoopToolNode + tools_condition
Trya fourth tool

Summary

You built a complete ReAct agent by hand, with a prompt and three tools. Next, letting a model choose the route through a graph.

Key takeaways
  • Prompt + tools
  • The loop
  • Three tools, one answer
Next up: Structured Routing — a model decides