How to Add Tool-Calling Agents to a LangChain App

October 12, 2026

Once a basic LangChain chain works, the next step is letting the model choose its own actions. That is what an agent does: it reads your question, picks a tool, reads the result, and repeats until it has an answer.

Quick answer: Define tools with the @tool decorator, pass them to create_react_agent or AgentExecutor, and cap the loop with max_iterations. Setup is about 20 lines.

Fair warning up front: agents are slower and cost more than a fixed chain. Every tool call is another round trip to the model. I only reach for one when the path genuinely depends on the input.

When should you use an agent instead of a chain?

Use an agent when the number of steps is unknown at build time. A support bot that might check an order, then refund it, then email the customer fits. A summarizer that always loads a doc and summarizes fits a chain, and running it as an agent just adds latency and failure points.

How do you define a tool in LangChain?

The decorator route is cleaner than the old dictionary style because the docstring becomes the description the model reads when deciding whether to call it.

from langchain_core.tools import tool

@tool
def get_order_status(order_id: str) -> str:
    """Look up the shipping status for an order ID."""
    return db.fetch(order_id)

The docstring is not decoration. Vague descriptions are the single biggest cause of an agent calling the wrong tool.

How do you wire the agent together?

LangGraph is the current recommended path; the older AgentExecutor still ships but the team pushes people toward graphs for anything with branching.

from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o", temperature=0)
agent = create_react_agent(llm, [get_order_status, search_docs], prompt)
executor = AgentExecutor(agent=agent, tools=[get_order_status, search_docs],
                         max_iterations=6, verbose=True)
print(executor.invoke({"input": "Is order A19 shipped?"}))

Set max_iterations. Without it a confused agent will keep calling tools until you hit a rate limit or your budget does.

Chain vs agent: which fits your case?

Dimension Fixed chain Tool-calling agent
Latency One model call One call per step
Cost Predictable Scales with tool use
Debugging Linear, easy Branching, needs tracing
Best for Known workflow Unknown number of steps

How do you debug a misbehaving agent?

Turn on verbose=True first. Nine times out of ten you see the model picking a tool that does not match the intent, and the fix is a sharper docstring or a tighter parameter type. LangSmith, the vendor’s tracing tool, has a free tier and shows every step; it is worth the extra setup once your agent has more than three tools.

What does it cost to run?

An agent with a couple of steps on GPT-4o runs roughly $0.03-$0.10 per conversation; the same task on a fixed chain is closer to a cent. If you are on gpt-4o-mini, multiply your expectations down by 10-15x. There is no LangChain fee — you pay your provider.

What are the limits people hit?

  • Looping: the agent retries the same failing tool. Cap iterations and add a fallback message.
  • Tool sprawl: past 10-15 tools, selection accuracy drops. Split into sub-agents.
  • Silent errors: a tool returning an empty string looks like success. Return explicit error text.
  • Version churn: package names moved between 0.1 and 0.3; pin your versions.

Who this suits: teams building assistants that need to act, not just answer. Who should wait: if your task is a fixed pipeline, an agent will be slower and harder to test. Build the chain first.

Quick Summary

  • Agents let the model choose tools; chains run a fixed path.
  • Define tools with @tool and a clear docstring the model can read.
  • Always set max_iterations to stop runaway loops.
  • Expect 3-10x the cost and latency of an equivalent chain.
  • LangGraph is the recommended structure for branching agents in 2026.