LangChainLangChain 1.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
46 small wins to finish your pathNext lesson →

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

python
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.

Exampleagent.py
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.

python
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 turn

The agent loop end to end

One question that needs a lookup runs the whole loop and returns four messages.

Example
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.

Example
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 toolsYour codeThe agent
Who calls the model againYour codeThe agent
When it stopsYou decideWhen 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.
Watch out. The model, not the agent, decides how many times the loop runs. A model that keeps asking for tools never returns text, so the loop never ends; lesson 20 sets a limit to stop that.
Try it yourself
  • Ask about B22, an order the shop does not have, and read the final answer.
  • Remove lookup_order from 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.