By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
create_agent is the shortest path from a model and a list of tools to a working agent. You give it the pieces; it returns a compiled LangGraph runtime that handles the loop from Lesson 6. You invoke it with a message list and get back the final state, including the full message trace.
The important options are few: system_prompt for durable rules, checkpointer for thread memory, middleware for cross-cutting policy (trimming, human approval), response_format for validated structured output, and state_schema when your tools need custom state. Everything else is your tool design.
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.tools import tool
@tool
def lookup_order(order_id: str) -> dict:
"""Look up an order by id.
Args:
order_id: Numeric order id, for example "4521".
"""
orders = {"4521": {"status": "delivered", "items": ["ceramic mug"]}}
if order_id not in orders:
raise ValueError(f"Unknown order: {order_id}")
return {"order_id": order_id, **orders[order_id]}
agent = create_agent(
model=init_chat_model("openai:gpt-4.1-mini", temperature=0),
tools=[lookup_order],
system_prompt=(
"You are Harbor Supply's support agent. Use lookup_order for order "
"questions and never invent order details."
),
)
result = agent.invoke(
{"messages": [{"role": "user", "content": "Where is order #4521?"}]}
)
for message in result["messages"]:
label = type(message).__name__
detail = getattr(message, "tool_calls", []) or message.content
print(f"{label:14} |", detail)AIMessage with tool calls and one ToolMessage per result in the right order, with matching ids.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.
result['messages'] is the trace, in execution order. Read it the way you would read a customer conversation, and assert on it in tests: which tool was called, with which arguments, what came back, and what the model did with it. agent.get_graph() shows the nodes and edges if you want the structural view.
ToolMessage must immediately follow the AI message that requested it, with a matching tool_call_id.usage_metadata on the final AIMessage is your first cost signal.Key takeaways
create_agent(model, tools, system_prompt=...) returns a compiled runtime that owns the loop.
Invoke it with a message list and read the trace from the returned state's messages.
It hides bookkeeping and dispatch, not prompt quality, tool contracts, budgets or approval policy.
Assert on the trace — tool names, arguments, results and order — instead of on the model's exact wording.
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.