Lesson 10: What Is an Agentic Loop?
One prompt = one answer. An agentic loop = the model plans, acts, observes, and repeats until the job is done.
From Chatbot to Agent
A plain LLM call is a single shot: prompt in, answer out. An agent wraps the LLM in a loop that lets it take actions and use the results:
┌─────────────┐
│ THINK │ decide what to do next
└──────┬──────┘
▼
┌─────────────┐
│ ACT │ call a tool (search, code, API)
└──────┬──────┘
▼
┌─────────────┐
│ OBSERVE │ read the tool's result
└──────┬──────┘
│
┌──────▼──────┐
│ DONE? │── no ──▶ back to THINK
└─────────────┘
│ yes
▼
FINAL ANSWER
The Loop Phases
| Phase | What Happens | Example |
|---|---|---|
| Think | The model reasons about the goal and picks the next step | "I need the current price, so I'll call get_stock_price" |
| Act | The model calls a tool with arguments | get_stock_price("RELIANCE") |
| Observe | The tool's output is fed back into the context | "₹2,940.50 as of 10:04 AM" |
| Repeat | Loop until the goal is met or a stop condition fires | Re-check, compare, summarize |
| Answer | Produce the final response | "RELIANCE is up 1.2% today…" |
What Makes It "Agentic"
The key property: the model decides the next action based on what it just observed. It's not a fixed script — it adapts. That's what lets one agent write code, run it, read the error, fix the bug, and rerun — autonomously.
Agent vs. Chatbot vs. Workflow
| Definition | Example | |
|---|---|---|
| Chatbot | Single-turn Q&A, no tools | "What's the weather?" |
| Workflow | Fixed, predefined steps | Summarize → Translate → Email |
| Agent | Model decides steps dynamically | Research → plan → execute → verify |
Real-World Agent Examples
- Coding agents: read your repo, write code, run tests, iterate on failures.
- Research agents: search the web, read pages, cross-check sources, write a report.
- Data agents: query databases, clean data, build charts, explain findings.
- Customer-service agents: check orders, refund policies, draft replies.
Stop Conditions (Crucial!)
Agents need to know when to stop, or they loop forever (and burn tokens):
- Goal achieved (final answer ready).
- Max steps reached (e.g. 10 tool calls).
- Model decides it needs human input.
- Budget/token limit hit.
- Tool error that can't be recovered.
Key Takeaways
- An agentic loop = Think → Act → Observe → Repeat → Answer.
- The model decides the next action from observations — that's the "agentic" part.
- Agents shine at multi-step, adaptive tasks; workflows win for fixed pipelines.
- Always define stop conditions — unbounded loops are a real failure mode.
Next up: Tools and function calling — how agents actually do things.
# The agentic loop in its simplest form: plan -> act -> observe -> repeat
def agent_loop(task, max_steps=3):
print(f"Task: {task}\n")
for step in range(1, max_steps + 1):
print(f"Step {step}:")
print(" Thought: I need more information to complete this task.")
action = f"search('{task.split('about ')[-1]}')"
print(f" Action: {action}")
print(" Observation: 3 results found.")
if step == max_steps:
print(" Final Answer: Task complete based on gathered evidence.")
break
print()
agent_loop("Find facts about the Eiffel Tower", max_steps=3)Lesson Code (Python)
# The agentic loop in its simplest form: plan -> act -> observe -> repeat
def agent_loop(task, max_steps=3):
print(f"Task: {task}\n")
for step in range(1, max_steps + 1):
print(f"Step {step}:")
print(" Thought: I need more information to complete this task.")
action = f"search('{task.split('about ')[-1]}')"
print(f" Action: {action}")
print(" Observation: 3 results found.")
if step == max_steps:
print(" Final Answer: Task complete based on gathered evidence.")
break
print()
agent_loop("Find facts about the Eiffel Tower", max_steps=3)Console Output
Task: Find facts about the Eiffel Tower
Step 1:
Thought: I need more information to complete this task.
Action: search('the Eiffel Tower')
Observation: 3 results found.
Step 2:
Thought: I need more information to complete this task.
Action: search('the Eiffel Tower')
Observation: 3 results found.
Step 3:
Thought: I need more information to complete this task.
Action: search('the Eiffel Tower')
Observation: 3 results found.
Final Answer: Task complete based on gathered evidence.Code Visualization Tips
- Draw the loop as a circle with the five phases and an arrow back from OBSERVE to THINK.
- Trace a real task (e.g. 'book a flight') through the loop, writing each tool call.
- Annotate where stop conditions fire — circle the moment the agent should stop.
Professional Tips & Tricks
- Start with a workflow; upgrade to an agent only when the steps genuinely vary per task.
- Log every Think/Action/Observation — agent debugging is impossible without traces.
- Give agents a 'ask the user' tool: knowing when to stop and ask is a feature, not a failure.
Python Code Judge & Practice Arena
LeetCode StyleRun real Python 3.12 WebAssembly code directly in your browser against automated test suites.
Trace an Agentic Loop
Test Your Knowledge
Instant feedbackQuick Check: Agentic Loops & AI Agents
Up next · Continue learning
Tools & Function Calling
Tools are how agents touch the world. Learn function calling, tool schemas, and the ReAct pattern with a real example.