AI Hints
LangChain · Lesson 43 of 120

The Agent Loop

Think, act, observe.

⏱ 2 min read▶ Video at 47:06🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

An agent is a model that runs in a loop, using tools until the job is done. It decides for itself which tools to call, and in what order. That's what makes it more flexible than a fixed chain.

The Agent Loop
From the video: The Agent Loop
  • A model in a loop
  • Uses tools until done
  • Decides the order itself

Think, act, observe

The loop has three steps. Think, where the model decides what to do next. Act, where a tool is called. And observe, where the model reads the result. Then it thinks again. When it has enough information, it stops calling tools, and answers.

Goal
  1. Thinkwhat next?
  2. Actcall a tool
  3. Observeread the result
  4. Answerwhen it's done

Chain vs agent

So when do you use which? A chain follows the same fixed steps every time. It's predictable, cheap, and easy to test. An agent chooses its own steps. It handles open ended requests, but it's less predictable, and can use more calls. Use a chain when the steps are known, and an agent when they're not.

Chain

  • Fixed stepsPredictable, cheapSteps known in advance

Agent

  • Chooses its stepsFlexibleSteps not known in advance

Summary

An agent loops through think, act, and observe, until it can answer. Use agents when the steps aren't known in advance. Next, building one the easy way.

Key takeaways
  • Think
  • Act
  • Observe
Next up: create_agent — the easy way