Skip to content

Exercise: The Agent Loop

Objective

Build the core agent loop that powers all agentic behavior: Reason → Act → Observe → Repeat.

Concepts Covered

  • The agent loop: Reason → Act → Observe → Repeat
  • finish_reason as loop termination condition
  • Multi-iteration tool calling
  • MAX_ITERATIONS safety valve
  • Accumulating context across loop iterations

How It Works

This script introduces the core agent loop that all later exercises build on. Instead of a single pass, it runs in a while loop: send messages → check for tool_calls → execute → append results → re-send — repeating until the model's finish_reason is "stop" (meaning it has all the information it needs).

flowchart TD
    A[Send messages + tools to LLM] --> B{tool_calls in response?}
    B -- Yes --> C[Execute tool functions locally]
    C --> D[Append assistant message +<br/>tool results to messages list]
    D --> A
    B -- No / finish_reason=stop --> E[Return final text response]

    style A fill:#4a90d9,color:#fff
    style E fill:#2ecc71,color:#fff

The loop is capped by MAX_ITERATIONS = 10 to prevent infinite cycles. The model might call multiple tools in a single turn (e.g., search_database + get_stock_price) before combining the results.

Context sharing: The messages list accumulates every iteration — the model sees its own prior tool calls and results, building up context until it can produce a final answer.

Structured output: Not used for inter-agent communication. Tool definitions use Pydantic schemas for input validation (strict mode), but responses are plain text.

Interactive Message Flow

You are a financial research assistant with access to database, stock price, and calculator tools.
What is the total market value of our top tech holdings?
search_database(query='top tech holdings')
{"results": [{"company": "NVDA", "shares": 1000}, {"company": "AAPL", "shares": 500}]}
get_stock_price(symbol='NVDA') + get_stock_price(symbol='AAPL')
{"symbol": "NVDA", "price": 875.50}
{"symbol": "AAPL", "price": 178.25}
calculate(expression='1000 * 875.50 + 500 * 178.25')
{"result": 964625.0}
Your top tech holdings have a total market value of $964,625. NVDA: 1,000 shares at $875.50 = $875,500. AAPL: 500 shares at $178.25 = $89,125.

Files

  1. 02_tool_loop.py — Full agent loop that runs until the model is done
  2. tools/ — Reusable mock tool implementations

How to Run

python exercises/02_tool_use/02_tool_loop.py

Expected Output

Structured logging showing each loop iteration, tool calls with arguments, tool return values, and the final model response.

Next

Exercise: Single Agent