Home › Agentic AI with Bunty › Lesson 4
The agent loop: think, act, check
Agentic AI with Bunty, lesson 4 of 6. simple English notes, free.
Open the interactive lessonWatch on YouTube
The idea
Raj said: "Bunty, find the cheapest flight to Delhi for tomorrow." Bunty failed the first time, and still finished the job. How?
Bunty works in a loop. Think: "I need to open a flight site." Act: search the site. Check: oops, this flight is for the day after tomorrow!
Bunty fixes the date and goes round again: think, act, check. Until the goal is done.
- Try 1: search, check, wrong date. "Sir, the date was wrong. Trying again."
- Try 2: search, check, correct: "Sir, tomorrow's 6 am flight. The cheapest!"
The code: The agent loop in Python
Save this as bunty_loop.py and run it with python bunty_loop.py:
# The agent loop: think -> act -> check, again and again until the goal is done.
want = "tomorrow"
tries = [{"flight": "6:00 am", "date": "day after tomorrow", "price": 3200}, # first search: wrong date
{"flight": "6:00 am", "date": "tomorrow", "price": 3500}]
done, step = False, 0
while not done and step < 5:
step += 1
print(f"Try {step} THINK: search for a flight for {want}")
result = tries[min(step - 1, len(tries) - 1)] # ACT: use the flight-search tool
print(f"Try {step} ACT: found {result['flight']} on {result['date']}")
if result["date"] == want: # CHECK the result
done = True
print(f"Try {step} CHECK: correct! Sir, {result['flight']} tomorrow, Rs {result['price']}.")
else:
print(f"Try {step} CHECK: wrong date. Trying again.")
Output:
Try 1 THINK: search for a flight for tomorrow
Try 1 ACT: found 6:00 am on day after tomorrow
Try 1 CHECK: wrong date. Trying again.
Try 2 THINK: search for a flight for tomorrow
Try 2 ACT: found 6:00 am on tomorrow
Try 2 CHECK: correct! Sir, 6:00 am tomorrow, Rs 3500.
The while loop is the agent loop: think, act (search), check. The first result has the wrong date, so the check fails and Bunty goes round again.
Try it: change the code
- Make both tries return the wrong date. What stops the loop from running forever?
- Add a price check: only accept a flight under Rs 4000.
Try it yourself
- Pick a task you do (booking a ticket, paying a bill). Write its think, act and check steps.
- Write one thing the check step should catch, and what the agent should do then.
Remember
A chatbot answers once and stops. An agent stops only when the work is done. Next: Part 5, Planning.
← Tools: Bunty's hands · Planning: big goal, small steps →
All lessons: Agentic AI with Bunty