Zero to AI Engineer

Module 19 of 54

Module 19: Building LangChain Agents

4 min read793 words
What you'll learn
Explain what an agent isDescribe the reason–act loopSee how agents use tools autonomouslyKnow when agents help (and when they don't)

"A chain follows steps you defined. An agent decides the steps itself — reasoning about which tools to use to reach a goal."

Level: Intermediate · Time: ~13 min · Prerequisites: Module 18

Learning Objectives

By the end of this module, you will be able to:

  • Explain what an agent is
  • Describe the reason–act loop
  • See how agents use tools autonomously
  • Know when agents help (and when they don't)

1. From Chains to Agents

A chain runs a fixed sequence you wrote. An agent is given a goal and a set of tools, and it decides for itself which tools to use and in what order — looping until the task is done.

Explain like I'm new: A chain is a recipe you follow step by step. An agent is a chef given ingredients and a goal ("make dinner") who decides the steps. More flexible — and more unpredictable — which is the trade-off.

2. The Reason–Act Loop

Agents run a loop (often called ReAct = Reason + Act):

  1. Think — "To answer this, I need today's weather."
  2. Act — call the weather tool.
  3. Observe — read the result.
  4. Repeat — think about the next step, until it can answer.

The LLM is the "brain" deciding each step; the tools are its hands. Crucially, the loop is adaptive: the agent doesn't plan all steps upfront. It takes one action, looks at what came back, and only then decides the next move. If a search returns nothing useful, it can rephrase and try again — behavior you'd have to hand-code in a fixed chain.

Key idea: The agent loop is what separates an agent from a single LLM call. It can chain several tool uses, adapt based on results, and handle multi-step tasks no single prompt could — because it reasons after each observation.

Concept: Every trip through the loop re-sends the growing history — original goal, past thoughts, actions taken, and results observed — back to the model. So the agent's "state" is just an ever-lengthening conversation, which is also why long agent runs get slower and more expensive: each step carries all the previous ones.

3. Agents in LangChain / LangGraph

LangChain (and its stateful sibling LangGraph) provide ready-made agent loops. You supply a model, tools, and instructions; the framework runs the reason–act cycle, handles tool calls, and returns the final answer.

python
[object Object],
agent = create_agent(model, tools=[search, calculator])
agent.invoke(,[object Object],)
,[object Object],

Real-world use case: A research agent given web_search and summarize tools: it searches, reads results, decides it needs more detail, searches again, then writes a cited summary — all autonomously. That's real work done, not just text produced.

4. When to Use Agents

Agents shine for open-ended, multi-step tasks where you can't script the steps ahead of time. But they're slower, costlier, and less predictable than fixed chains.

Real-world use case: A customer-support agent given tools for look_up_order, check_shipping, and issue_refund can handle "Where's my order and can I get a refund?" end to end — it looks up the order, checks the status, and decides whether a refund applies, adapting to whatever it finds. Scripting every branch of that conversation as a fixed chain would be a tangle.

Common mistake: Reaching for a full autonomous agent when a simple chain would do. If you know the steps, hard-code them as a chain — it's faster, cheaper, and easier to trust. Save agents for genuinely open-ended problems, and always cap their steps and tool permissions.

Hands-On: Try This

Try this: Take a task like "find three laptops under $800 and compare them." Write out the reason–act loop an agent might follow (search → read → search → compare → answer). Notice how each step depends on the last — that's why it needs an agent, not a fixed chain.

✅ Checkpoint

  1. How does an agent differ from a chain?
  2. What are the steps of the reason–act loop?
  3. When should you prefer a chain over an agent?

Answers: 1) A chain follows fixed steps; an agent decides its own steps. 2) Think → Act → Observe → repeat. 3) When you already know the steps — it's faster, cheaper, more predictable.

Key Takeaway: An agent is an LLM given tools and a goal that decides its own steps via a reason–act loop (think → act → observe → repeat), handling multi-step tasks a single prompt can't. LangChain/LangGraph provide the loop. Agents are powerful but slower and less predictable — use them only for open-ended tasks, with capped steps and least-privilege tools.

Further Learning

Adapted from the LangChain for Beginners curriculum (MIT License).