Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 32 ยท Agents and Tools

What Is an Agent?

Learn what a LangChain agent is: a model that chooses tools in a loop until it can answer. Compare chains and agents, and run a stand-in agent loop in Python.

A chain runs the steps you wrote, in the order you wrote them. An agent is different: the language model itself decides which step comes next. You give it a goal and a set of tools (functions it may call), and it loops: think, call a tool, read the result, think again, until it can answer. That loop is the whole idea behind LangChain agents, and it is why agents can solve questions you did not plan for, and also why they can go wrong in ways a fixed chain cannot.

LangChain itself cannot run on this site (it needs the langchain packages and a model provider with an API key). Real LangChain code appears in read-only blocks. The runnable examples use a small hand-written stand-in in plain Python (real Pyodide) with a scripted "model", so you can watch the loop without any network.

Chain versus agent

In a chain, control flow lives in your code: prompt | model | parser. The model only fills in text. In an agent, control flow lives in the model's output: each reply is either a final answer or a request to call a tool.

flowchart TD
    Q["User question"] --> M["Model"]
    M -->|"tool call"| T["Run the tool"]
    T -->|"observation"| M
    M -->|"final answer"| A["Answer"]

The agent loop: the model keeps asking for tools until it answers.

Chain Agent
Steps Fixed by you Chosen by the model at run time
Number of model calls Known in advance Unknown, can loop
Predictability High Lower
Best for Known pipelines (summarize, extract) Open-ended tasks needing tools

A good rule: if you can draw the steps before the program runs, write a chain. If the steps depend on what the tools return, use an agent.

The three parts

Every agent has three parts.

  1. A model that supports tool calling: given messages and tool descriptions, it can reply with a structured request such as "call get_weather with city="Oslo"".
  2. Tools: ordinary functions with a name, a description and typed arguments. The model sees the description, never the code.
  3. A loop (the runtime): it sends messages to the model, executes any requested tool, appends the result as a new message, and repeats until the model replies with plain text.

In current LangChain you rarely write the loop yourself. The create_agent helper builds it, on top of LangGraph (see the LangGraph chapter).

Python
# Real LangChain (read-only: needs a provider and an API key)
from langchain.agents import create_agent
from langchain.tools import tool

@tool
def get_weather(city: str) -> str:
    """Return the current weather for a city."""
    return f"It is sunny in {city}."

agent = create_agent(
    model="anthropic:claude-sonnet-4-5",
    tools=[get_weather],
    system_prompt="You are a concise weather assistant.",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "What is the weather in Oslo?"}]}
)
print(result["messages"][-1].content)

The exact model identifier depends on your provider; any chat model that supports tool calling works. The agent returns a state dictionary whose messages list holds the whole conversation, including the tool call and its result.

A stand-in agent loop

The next example builds the loop from scratch. The "model" is a function with fixed rules, standing in for a real LLM: it asks for a tool when it has no observation yet, and answers once it has one. Everything else (the tool registry, the messages list, the loop, the step limit) is exactly what a real runtime does.

Python Stand-in: the agent loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note the max_steps guard. A real model can keep asking for tools forever (or until your bill arrives), so every production agent has a limit.

Agents are not magic: where they fail

The loop is simple; the difficulty is that the model's choices are probabilistic. Typical failure modes:

  • Wrong tool: two tools with vague, overlapping descriptions.
  • Bad arguments: the model invents a city or a date format your function rejects.
  • Endless loops: the model repeats the same call because the result never satisfies it.
  • Over-trust: the model believes whatever a tool returns, including text an attacker planted in a web page.

You fight these with clear tool descriptions (next lesson), step limits, validation and error handling (agent errors and guardrails), and tests (evaluating LLM apps).

Seeing the difference

The second stand-in runs the same job twice: once as a fixed chain, once as an agent whose "model" picks which tools to use based on the question.

Python Stand-in: fixed chain versus agent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The chain searches for "what is 12 * 7" (useless). The agent noticed digits and used the calculator. In a real agent, the model makes that decision from the tool descriptions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What mainly distinguishes an agent from a chain?

    In a chain you fix the steps; in an agent the model's output selects the next action, usually a tool call.
  2. 2. What are the three parts of a typical agent?

    A tool-calling model, the tool functions, and a runtime loop that executes tool calls and feeds results back.
  3. 3. Why should every agent have a step limit?

    The number of loop iterations is not known in advance, so a cap prevents runaway cost and time.
  4. 4. When is a plain chain the better choice?

    Fixed pipelines are cheaper, faster and more predictable than agents.

Summary

  • An agent is a model plus tools plus a loop; the model chooses the next action.
  • Chains fix the steps in code; agents decide them at run time.
  • create_agent builds the loop for you; the stand-in shows what happens inside.
  • Agents fail through wrong tools, bad arguments, endless loops and over-trust, so limit and validate.

Next: Defining Tools