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.

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

Try it yourself

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