AI Hints
Home›LangChain Tutorial›Agent Practice and Approval
LangChain · Lesson 55 of 120

Practice: Bookshop Agent

Tools and memory.

⏱ 2 min read▶ Video at 1:01:21🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Let's put the agent pieces together. Build an agent for Riverside Books with the three shop tools, a short system prompt, and memory, then hold a two turn conversation. Pause, and try it first.

Practice: Bookshop Agent
From the video: Practice: Bookshop Agent
  • Three shop tools
  • A system prompt
  • Memory across turns

One possible solution

Here's one solution. The agent gets the three tools, a system prompt, and an in memory checkpointer. A small helper sends a message on one thread. First, the customer asks about The Hobbit. Then they ask, who wrote it, without saying the title again. Run it. The agent remembers which book they mean, and calls the author tool.

shop_agent.pypython
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
from shop_tools import check_stock, get_price, get_author

agent = create_agent(
    "google_genai:gemini-flash-lite-latest",
    tools=[check_stock, get_price, get_author],
    system_prompt="You are Riverside Books' assistant. One short sentence per answer.",
    checkpointer=InMemorySaver(),
)
cfg = {"configurable": {"thread_id": "shop-1"}}
def say(text):
    out = agent.invoke({"messages": [{"role": "user", "content": text}]}, cfg)
    print("You:", text, "\nBot:", out["messages"][-1].text)

say("How much is The Hobbit?")
say("And who wrote it?")
Output
You: How much is The Hobbit?
Bot: The Hobbit costs £8.99.
You: And who wrote it?
Bot: The Hobbit was written by J.R.R. Tolkien.
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 this shows

This short program has the core of a real assistant. Tools for live data. A system prompt for tone and rules. And memory, so follow up questions like, who wrote it, just work. In the project section, we'll add documents, guardrails, and tests.

keyvalue
Toolslive shop data
System prompttone and rules
Memoryfollow-ups work
Laterdocs, guardrails, tests

Summary

Tools, a system prompt, and a checkpointer make a capable assistant in a few lines. Next, pausing an agent to ask a human for approval.

Key takeaways
  • Three tools
  • Short prompt
  • Memory per thread
Next up: Human Approval — pausing for a yes