The Agent Loop: Think, Act, Observe

The model picks a tool, the program runs it and adds the result to the history, and the model decides again, until it can answer.

AI · intermediate · 9 min · 38 steps · free

What you'll learn

agent_loop.py

def search(query):
    return {'population of chennai': '7 million'}.get(query, 'no result')

def calculator(expression):
    a, op, b = expression.split()
    return int(a) * int(b) if op == '*' else int(a) + int(b)

TOOLS = {'search': search, 'calculator': calculator}

# Stands in for the language model: it picks the next action from what it has seen so far.
def model(history):
    if len(history) == 1:
        return {'tool': 'search', 'input': 'population of chennai'}
    if len(history) == 3:
        return {'tool': 'calculator', 'input': '7 * 2'}
    return {'answer': 'About 14 million people live in two cities the size of Chennai.'}

history = [{'role': 'user', 'content': 'How many people live in two cities the size of Chennai?'}]
for turn in range(5):
    action = model(history)
    if 'answer' in action:
        print(action['answer'])
        break
    result = TOOLS[action['tool']](action['input'])
    history.append({'role': 'assistant', 'content': action})
    history.append({'role': 'tool', 'content': result})

Step by step

  1. Python defines the function search(). Its body does not run yet: it runs each time the function is called.
  2. Python defines the function calculator(). Its body does not run yet: it runs each time the function is called.
  3. TOOLS is created and points to a new dict: {search: function search(), calculator: function calculator()}.
  4. model decides the next action from the history so far.
  5. The history starts with the user's question.
  6. turn is created with the value 0.
  7. Python calls model(), with history = [a dict]. It gets its own names.
  8. len(history) == 1 is true, so the block under it runs.
  9. The function returns {'tool': 'search', 'input': 'population of chennai'}.
  10. Turn 1: the model wants to search first.
  11. 'answer' in action is false, so the block under it is skipped.
  12. Python calls search(), with query = 'population of chennai'. It gets its own names.
  13. The function returns {'population of chennai': '7 million'}.get(query, 'no result').
  14. The program runs search and gets "7 million".
  15. a dict is added to the end of the list that history points to.
  16. Both the action and its result go into the history.
  17. turn changes from 0 to 1.
  18. Python calls model(), with history = [a dict, a dict, a dict]. It gets its own names.
  19. len(history) == 1 is false, so the block under it is skipped.
  20. len(history) == 3 is true, so the block under it runs.
  21. The function returns {'tool': 'calculator', 'input': '7 * 2'}.
  22. Turn 2: knowing the population, it asks the calculator for 7 × 2.
  23. 'answer' in action is false, so the block under it is skipped.
  24. Python calls calculator(), with expression = '7 * 2'. It gets its own names.
  25. a is created with the value '7'. op is created with the value '*'. b is created with the value '2'.
  26. The function returns int(a) * int(b) if op == '*' else int(a) + int(b).
  27. calculator() has finished, so its names are gone. The value it returned, 14, is stored in result.
  28. a dict is added to the end of the list that history points to.
  29. a dict is added to the end of the list that history points to.
  30. turn changes from 1 to 2.
  31. Python calls model(), with history = [a dict, a dict, a dict, a dict, a dict]. It gets its own names.
  32. len(history) == 1 is false, so the block under it is skipped.
  33. len(history) == 3 is false, so the block under it is skipped.
  34. The function returns {'answer': 'About 14 million people live in two cities the size of Chennai.'}.
  35. model() has finished, so its names are gone. The value it returned, {answer: 'About 14 million people live in two cities the size of Chennai.'}, is stored in action.
  36. An answer instead of a tool: the loop stops.
  37. It prints About 14 million people live in two cities the size of Chennai..
  38. Line runs: break.

Common mistakes

Best practices

Key takeaways

Check yourself

Why append the tool result to history?

Answer: The model only knows what is sent to it

What ends the loop?

Answer: An answer, or the turn limit

·

More in Agents