Lesson 11 / 29
Prebuilt Agents With a Real Model
Know the high-level constructors and when to use the low-level graph.
Start high-level, drop down when needed
For a standard tool-calling agent you can use a prebuilt constructor that takes a chat model and a list of tools and returns a compiled graph with the loop, tool execution and message handling already wired. The exact function names and import paths have changed between releases, so check the documentation for your installed version. Use the prebuilt agent for simple cases; build your own graph when you need custom routing, approval steps, extra nodes (guardrails, retrieval, planning) or specific state. Both produce the same kind of compiled app, so streaming, checkpointing and interrupts work the same way.
A prebuilt agent (illustrative)
Needs a provider package and an API key; constructor names vary by version, so treat this as a sketch. Not run here.
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI # pip install langchain-openai
from langgraph.prebuilt import create_react_agent # name/location may differ in your version
@tool
def get_order_status(order_id: str) -> str:
"""Look up the status of an order by its 6-digit id."""
return "shipped" # real code: query your system
model = ChatOpenAI(model="<model-name>", temperature=0)
agent = create_react_agent(model, [get_order_status])
result = agent.invoke({"messages": [("user", "Where is order 481516?")]},
{"recursion_limit": 10})
print(result["messages"][-1].content)Quick check: When should you build your own graph instead of using a prebuilt agent?
- When you have no tools
- Never
- Only for one-line scripts
- When you need custom routing, approvals or extra nodes
Answer
When you need custom routing, approvals or extra nodes — Custom graphs give full control over the flow.