Book a free call

How to Build AI Agents With LangChain: A Practical Guide

A LangChain agent plans its own steps: it reads a request, picks a tool, checks the result and repeats until done. Here is how to build one you can trust.

Hertiv engineering teamReviewed by a senior engineer
7 min read
How to Build AI Agents With LangChain: A Practical Guide
On this page

Short answer

A LangChain agent is an AI agent built with the LangChain framework that decides its own steps at run time: it reads a request, chooses a tool, runs it, reads the result, and repeats until the task is done or it reaches a stopping rule. That freedom is what makes agents useful, and what makes them hard to test.

What is a LangChain agent?

A LangChain agent is a language model wrapped in a loop that lets it use tools. Instead of following a fixed sequence of steps, the agent reasons about the request, picks a tool, reads what the tool returns, and decides what to do next. The loop ends when the agent has an answer or hits a limit on steps.

The key idea is the reasoning-and-acting loop, often called ReAct. The model writes its reasoning in plain words, then takes an action, then observes the result and reasons again. That written reasoning is visible in the logs, which makes it possible to see why an agent chose a tool, even when the choice was wrong.

LangChain provides the pieces: the agent executor that runs the loop, tool definitions, memory for chat history, and integrations with models and vector stores. Your job is to pick the task, define the tools, set the limits, and test the result. The framework does the plumbing, but the judgement about what the agent may do stays with you. See LangChain for language models for the building blocks behind the agent loop.

How does a LangChain agent decide what to do?

A LangChain agent decides what to do by reasoning in words, then acting on that reasoning. Each turn of the loop has the same shape: think about the request and what is known so far, choose a tool and its inputs, run the tool, and read the observation before the next turn.

The table below shows the four moves in one turn of the loop.

Move What happens Example
Reason The model plans the next step I need the refund policy
Act It calls a tool Search the help centre
Observe It reads the tool result Policy found, 30 days
Decide It answers or loops again Draft the reply

The model makes its choice from the tool descriptions you write, so those descriptions are the real interface. A tool named 'search_docs' with a description that says when to use it and what it returns will be called correctly; a vague description leads to wrong calls. The agent also needs a stopping rule, usually a maximum number of turns, so a confused agent cannot loop forever.

What tools can a LangChain agent use?

A LangChain agent can use any tool you define: a search over your documents, a database lookup, a calculator, a web search, or a call to your own API. The tool is a small function with a name, a description and a defined input, and the model decides when to call it.

The most useful tools for a first agent are the boring ones. A document search grounds answers in your own content, so the agent quotes sources instead of guessing. A database lookup gives it facts about a customer or an order. A calculator keeps arithmetic honest, because a model that adds numbers in its head will sometimes get them wrong.

Keep the tool list short and the tools safe. Read-only tools, such as search and lookup, are safe to give an agent early. Tools that write, send or delete need a human approval step before they run, because the model cannot judge the cost of a mistake.

A tool should also fail clearly: return a short message that says what went wrong, so the agent can try a different approach instead of repeating the same call.

How do you build a LangChain agent, step by step?

You build a LangChain agent by picking one narrow task, defining a few tools, wiring the agent loop, and testing it on real requests before it touches anything that matters.

  1. Pick one task. Choose a job with a clear result, such as answering questions over a help centre or summarising a set of documents. Write down what a good answer looks like.
  2. Define two or three tools. Start with a document search and one lookup tool. Write a plain description for each, saying when to use it and what it returns.
  3. Choose the model. Pick a model with reliable tool calling. The model is the agent's brain, and a weak one will pick the wrong tool or the wrong inputs.
  4. Wire the agent. Connect the model, the tools and a prompt that states the task, the tools and the stopping rule. Keep the prompt short and specific.
  5. Set limits. Cap the number of turns per run, and cap the cost per run if your provider supports it. An agent without limits is a risk, not a feature.
  6. Test on real requests. Run a fixed set of real questions with known good answers, read the traces, and fix the worst failure first.
  7. Add the approval step. Route any action that writes, sends or deletes through a human check before it runs.

Ship the agent behind a flag so you can turn it off fast. The first week of real use will teach you more about its failures than any test set.

How do you test an agent before you trust it?

You test an agent with a fixed set of real requests and known good answers, plus a log of every step the agent took. The test set is the only honest way to tell whether a change made the agent better or worse.

Build the test set from real work: twenty to thirty requests your team has already answered, with the correct answer written down. Run the agent on the set after each change and record how many answers were right, how many tool calls each run used, and how long each run took. A change that fixes one case but breaks two others is not progress.

Also test the failures on purpose. Give the agent a request with no good answer in the documents, a request that needs a tool that does not exist, and a request that asks it to do something it must not do.

Watch whether it says it cannot help, asks for more detail, or invents an answer. An agent that invents answers when it lacks information is not ready to face users. The top AI agent frameworks comparison can help if LangChain turns out to be the wrong fit.

Example (hypothetical): a research assistant agent

Example (hypothetical): a small research team reads 40 industry reports a month, and each report takes about 45 minutes to read and summarise. That is 40 times 45 minutes, or 1,800 minutes, which is 30 hours a month.

The team builds a LangChain agent with three tools: a document search over the report library, a calculator, and a draft writer. For each report, the agent searches for the key findings, checks the numbers with the calculator, and drafts a one-page summary. A researcher reviews each draft in about 10 minutes, which is 40 times 10 minutes, or about 7 hours a month.

The saving is about 23 hours a month, but the review step is the point: the agent drafts, and the researcher checks the numbers and the claims before anything is shared. After two months, the team adds a fourth tool that fetches the source page for any figure the agent quotes, because one early draft had cited a figure from the wrong report.

What are the common mistakes?

The most common LangChain agent mistakes are giving the agent too many tools, skipping the evaluation set, and letting it act without approval on work that matters.

  • Too many tools. An agent with fifteen tools will pick the wrong one. Start with two or three and add one only when a real request shows the gap.
  • Vague tool descriptions. The model chooses tools from their descriptions. A description that does not say when to use the tool is a bug.
  • No step limit. Without a maximum number of turns, a confused agent loops until it runs out of money or patience.
  • Trusting the first draft. Agent output needs checking, especially numbers and quotes. A review step is part of the system, not an optional extra.
  • Skipping the test set. Without fixed cases, you cannot tell whether a prompt change helped or hurt.
  • Write access without approval. An agent that can send, delete or change records without a human check will eventually do something expensive.

Key takeaways

  • A LangChain agent loops through reason, act, observe and decide
  • Tool descriptions are the real interface, so write them plainly
  • Start with two or three safe, read-only tools
  • A fixed test set is the only honest way to judge a change
  • Any action that writes, sends or deletes needs human approval

Frequently asked questions

What is the difference between a LangChain agent and a chain?

A chain runs a fixed sequence of steps in the same order every time, which makes it easy to test. An agent decides its own steps at run time, choosing tools based on the request. Use a chain when the steps are known in advance, and an agent when the task needs judgement about which tool to use.

Which LangChain agent type should I start with?

Start with the simplest agent type your version of LangChain offers, usually a ReAct-style agent with a small tool list. It is the easiest to understand from its traces, and it handles most first tasks. More complex agent types are worth trying only when the simple one fails on real requests.

How many turns should an agent be allowed?

Ten turns is a reasonable starting limit for most tasks, since a good agent usually finishes in three to six. If your agent often hits the limit, the task is probably too broad or a tool is missing, and raising the limit will not fix it.

Can a LangChain agent remember past conversations?

Yes, through memory. Short-term memory keeps the chat history inside the run, so the agent can refer back to earlier turns. Longer memory, such as facts about a user, lives in a store the agent reads through a tool. Keep memory small so the prompt stays focused.

What does a LangChain agent cost to run?

It costs more than a single prompt, because each turn of the loop is another model call, and a run can take five to ten calls. Track cost per run during testing, and set a step limit to cap it. Prices change often, so check the vendor site before budgeting.

When is LangChain the wrong choice for an agent?

LangChain is the wrong choice when the task is a single fixed workflow, when you need strict control over every branch, or when your team works in a stack the framework does not fit. In those cases a fixed chain, a graph-based framework or a lighter toolkit will be easier to maintain.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building an agent that picks its own tools? Talk to Hertiv.

A senior engineer replies within one business day. Or see our AI Agent Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call