Lesson · ⏱ 8 min read

🤖 AI agents: plan, act, observe, repeat

EnglishHinglish

🏠 Real-life example

Planning a Goa trip, you check flight prices, then hotels near the beach, notice the hotel is far from the airport, re-check flights to a different airport, and then book. You plan, act, look at the result and adjust. An AI agent works in the same loop, using tools at each step until the task is done.

💡 In simple words

An agent is an LLM in a loop: it decides the next action, calls a tool, reads the result, and repeats until it can give a final answer or hits a limit. Agents need memory (what has happened so far), clear tools, and stopping rules such as maximum steps and a budget. Frameworks like LangGraph or agent SDKs manage the loop and state. Agents fail in new ways: they loop forever, pick the wrong tool, or act on bad data. Start with a fixed workflow and add autonomy only where it clearly helps, and keep a human approving high-impact steps.

🎬 See how it works

🧠 Think🛠️ Act (tool)👀 ObserveloopGoal: 3 cafés open after 10 pm1 · search_places('cafés')2 · 12 results3 · get_hours() for each4 · filter rating ≥ 4.2 → 35 · ✓ final answerguardrail: max_steps = 8

An agent loops: think about the next step, act with a tool, observe the result, and repeat, until the goal is met or a step limit stops it.

🪜 Step by step

  1. Give the agent a clear goal and a small set of tools.
  2. Loop: the model plans the next step, calls a tool, reads the result, and decides again.
  3. Set limits: maximum steps, maximum cost and a timeout, so it can't loop forever.
  4. Require human approval before risky actions like payments, emails to customers or deleting data.
  5. Log every step (a trace) so you can see why it did what it did, and evaluate on realistic tasks.

🔑 Key terms

Agent loop
Think → act (tool) → observe → repeat.
State / memory
What the agent has done and learned so far in the task.
Guardrails
Limits on steps, cost and actions that keep the agent safe.

👀 See it in action

text
Goal: "Find 3 well-rated cafés near Koramangala open after 10 pm"
Step 1 think → search_places("cafés Koramangala")
Step 2 observe → 12 results
Step 3 think → get_hours() for each, filter rating ≥ 4.2
Step 4 observe → 3 match
Step 5 → final answer with names, ratings, closing times ✅  (max_steps = 8)

Each step uses the previous result. A step limit stops endless loops.

⚠️ Common mistakes to avoid

  • Using an agent where a fixed workflow works. Fixed steps are cheaper, faster and easier to test.
  • No stopping rules, leading to runaway loops and big bills.
  • Letting web pages or documents give the agent instructions (prompt injection). Treat them as data.

✅ Quick check

An agent keeps calling the same search tool 50 times. What should have prevented it?

  1. Higher temperature
  2. A bigger model
  3. Guardrails like a max-steps limit and loop detection
Show answer

C. Guardrails like a max-steps limit and loop detection: Agents need explicit stopping rules. Don't rely on the model alone.

🙋 Questions learners ask

What's the difference between an agent and a chatbot?

A chatbot mostly answers. An agent takes actions with tools over several steps to complete a task.

Do I need a framework like LangGraph?

Not to start. A simple loop in your own code teaches you the most. Frameworks help later with state, retries and multi step flows.

📌 Remember

Agent = LLM + tools + loop + memory + guardrails.

📖 Words from this lesson (AI dictionary)

Learn this as part of a full course

Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:

LLM / Agent Engineer

Continue in the course