By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every tool-using agent is the same four-beat loop. The model reads the message state and answers with text or with tool calls. If it answers with text, the run is done. If it asks for tools, the runtime executes them, appends the results to the state and calls the model again. Repeat until the model stops asking.
The LangChain path described this loop as a runtime you do not write by hand. LangGraph shows you the graph it actually is: two nodes (model and tools), a conditional edge between them, and a message state carried through every turn. Once you can see the loop as a graph, every knob — a step limit, an approval interrupt, a second model, a retrieval node — becomes an edit to a diagram instead of a rewrite.
create_agent(model=..., tools=[...], system_prompt=...) returns a compiled LangGraph runtime with exactly that structure. The example below runs it with a deterministic fake model, so the graph and the trace are real while the model's two turns are scripted. This is the same fake-model pattern from Lesson 9, now feeding the factory.
from langchain.agents import create_agent
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage, ToolCall
from langchain_core.tools import tool
class FakeToolCallingModel(GenericFakeChatModel):
"""A fake model that create_agent can bind tools to."""
def bind_tools(self, tools, **kwargs):
return self
@tool
def lookup_order(order_id: str) -> str:
"""Read-only: look up an order's status by id."""
orders = {"4521": "delivered (ceramic mug)"}
if order_id not in orders:
raise ValueError(f"Unknown order: {order_id}")
return orders[order_id]
model = FakeToolCallingModel(
messages=iter(
[
AIMessage(
content="",
tool_calls=[
ToolCall(
name="lookup_order",
args={"order_id": "4521"},
id="call_1",
)
],
),
AIMessage(
content="Order #4521 was delivered and contains one ceramic mug."
),
]
)
)
agent = create_agent(model, tools=[lookup_order])
print(sorted(agent.get_graph().nodes))
result = agent.invoke(
{"messages": [HumanMessage("Where is order #4521?")]}
)
for message in result["messages"]:
detail = getattr(message, "tool_calls", []) or message.content
print(type(message).__name__, "|", detail)AIMessage with tool calls and one ToolMessage per result, in order, with matching ids.create_agent accepts a checkpointer= and middleware=[...].Everything that determines whether the agent is any good is still yours: the prompt, the tool contracts, argument and ownership validation, the step budget and the approval policy for writes. create_agent is a loop, not a decision-maker — a badly designed tool will be called correctly and still do the wrong thing. The graph is inspectable with agent.get_graph(), and a checkpointer plus a per-ticket thread_id turns it into a stateful agent.
In production the only line that changes from the example above is the model: init_chat_model("openai:gpt-4.1-mini", temperature=0) (or your provider's chat model) replaces the fake. That call needs an API key and a network — everything else in the graph is unchanged.
Key takeaways
The agent loop is model → tool call → tool result → model, repeated until no tool calls are returned.
create_agent(model, tools, system_prompt=...) returns a compiled LangGraph with model and tools nodes.
It hides bookkeeping and dispatch, not prompt quality, tool contracts, budgets or approval policy.
Leave the factory when state is more than messages or routing depends on business fields — build the StateGraph explicitly.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.