create_agent: the agent loop
create_agent is a function that puts a model and its tools in a loop: it calls the model, runs the tools it asks for, and calls it again until the model answers with text.
Last updated: 27 Sep, 2026 · LangChain 1.4
Lesson 6 ran one round trip by hand. Doing that for every question gets repetitive, so create_agent runs the whole loop for you.
The create_agent call
from langchain.agents import create_agent
agent = create_agent(model, tools=[my_tool], system_prompt="...") # model + tools in a loop
result = agent.invoke({"messages": [{"role": "user", "content": "..."}]})Building the agent
Build the agent from a model, a list of tools, and an optional system prompt. Nothing runs yet.
from langchain.agents import create_agent
from shop_model import ShopModel
from tools import lookup_order
agent = create_agent(
ShopModel(),
tools=[lookup_order],
system_prompt="You help customers of a small online shop.",
)Sending a conversation
tools.py is the file from lesson 5 and shop_model.py the one from lesson 6. Send a conversation as a dictionary with a messages list, then read the messages back.
result = agent.invoke({"messages": [{"role": "user", "content": "Where is my order A17?"}]})
for message in result["messages"]:
print(f"{message.type:<6} {message.text or message.tool_calls}") # one line per turnThe agent loop end to end
One question that needs a lookup runs the whole loop and returns four messages.
result = agent.invoke({"messages": [{"role": "user", "content": "Where is my order A17?"}]})
for message in result["messages"]:
print(f"{message.type:<6} {message.text or message.tool_calls}")A question with no order to look up ends on the first model call.
result = agent.invoke({"messages": [{"role": "user", "content": "Hello there"}]})
for message in result["messages"]:
print(f"{message.type:<6} {message.text or message.tool_calls}")Reading the loop's messages
- The human message is what you sent.
- The first AI message has no text, only a tool call; the agent saw that and ran the tool.
- The tool message is the result, and the last AI message is the answer, written after the model read it. Text with no tool call is what ended the loop.
- The system prompt is not in the message list: the agent adds it to every model call without storing it with the conversation.
- With nothing to look up the model answers on the first call, so no tool runs and the loop ends at once. The model decides how many rounds, not the agent.
By hand vs create_agent
| By hand (lesson 6) | create_agent | |
|---|---|---|
| Who runs the tools | Your code | The agent |
| Who calls the model again | Your code | The agent |
| When it stops | You decide | When the model returns text with no tool call |
When to use create_agent
- Any assistant that may need one or more tool calls before it can answer.
- A task where the number of steps is not known ahead of time and the model decides.
Related
- Previous: bind_tools: asking for a tool
- Next: Streaming steps with stream_mode
- Reference: Agents
- Ask about B22, an order the shop does not have, and read the final answer.
- Remove
lookup_orderfrom the tools and ask about A17 again. - Print
result.keys()to see what else the agent returns.
Slow is fine. Stopping is the only problem.