AI Hints
Home›LangGraph Tutorial›Agents by Hand
LangGraph · Lesson 36 of 120

Your Graph vs create_agent

When to use which.

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

LangChain's create agent builds this exact loop for you. In fact, it returns a LangGraph graph. So when should you build the graph yourself?

Your Graph vs create_agent
From the video: Your Graph vs create_agent
  • create_agent builds the loop
  • It's a LangGraph graph
  • When to build your own?

Inside create_agent

We make an agent with create agent, and look inside. Its type is a compiled LangGraph graph. Its nodes are a model node, and a tools node, the same loop we built by hand. Run it. Same idea, ready made.

inside_agent.pypython
from langchain.agents import create_agent
from shop_tools import check_stock

agent = create_agent("google_genai:gemini-flash-lite-latest", tools=[check_stock])
print(type(agent).__name__)
print([n for n in agent.get_graph().nodes if not n.startswith("__")])
Output
CompiledStateGraph
['model', 'tools']
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")

Which to use?

Use create agent when the standard loop is enough. It's quick, and middleware covers most customisation. Build your own graph when you need extra steps, like a classifier first, a review step before answering, branches, or several agents.

create_agent

  • Standard loopMiddleware for tweaksFastest to build

Your own graph

  • Extra stepsBranches, reviewsSeveral agents

Summary

Create agent is a ready made LangGraph agent. Build your own graph when you need steps and branches it doesn't have. Next, practise building a bookshop agent.

Key takeaways
  • create_agent is a graph
  • Same loop
  • Own graph for control
Next up: Practice: Bookshop Agent — tools in a graph