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_reasonas loop termination condition- Multi-iteration tool calling
MAX_ITERATIONSsafety 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¶
Files¶
02_tool_loop.py— Full agent loop that runs until the model is donetools/— Reusable mock tool implementations
How to Run¶
Expected Output¶
Structured logging showing each loop iteration, tool calls with arguments, tool return values, and the final model response.