0
Phase 4

Build First AI Agent

~3 min read

Concept & How It Works

    Why Does It Exist?

    Single LLM calls can't handle multi-step tasks. The loop enables sequential decision-making: each iteration sees the results of previous actions and adapts. Without a well-designed loop, agents either stop too early or run indefinitely.

    Real-World Analogy

    The agent loop is like a GPS navigation system — it shows your current location (observe), plans the next turn (reason), you drive (act), then it recalculates based on where you actually ended up (update state).
    Loading diagram...

    Visual Workflows

    What is Build First AI Agent?

    Loading diagram...

    Example

    Scenario

    Task: 'Book a flight to Tokyo next Friday.' Step 1: search_flights('Tokyo', 'next Friday') → 3 options. Step 2: LLM picks cheapest → book_flight(id='JL408') → confirmation. Step 3: LLM returns booking confirmation to user. 3 iterations, done.

    Solution

    In Agent Foundations, apply Build First AI Agent to this scenario: Task: 'Book a flight to Tokyo next Friday. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to Build First AI Agent (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where Build First AI Agent happens in the code.

    Build First AI Agent
    1def agent_loop(client, messages: list, tools: list, max_steps: int = 10) -> str:  # define a reusable function2    for step in range(max_steps):3        response = client.chat.completions.create(  # call the API4            model="gpt-4o", messages=messages, tools=tools,5        )6        msg = response.choices[0].message7        if not msg.tool_calls:8            return msg.content or ""  # return the result9        messages.append(msg)10        for tool_call in msg.tool_calls:11            fn_name = tool_call.function.name12            args = json.loads(tool_call.function.arguments)13            try:14                result = TOOL_REGISTRY[fn_name](**args)15            except Exception as e:16                result = f"Error: {e}"17            messages.append({18                "role": "tool",19                "tool_call_id": tool_call.id,20                "content": str(result),21            })22    return "Max iterations reached"  # return the result

    Commands to Remember

    Commands to Remember

    • pip install openai # minimal agent = LLM API + Python loop
    • python agent.py # run your agent script
    • pip install python-dotenv # load API keys from .env

    Common Mistakes

    • No max iteration limit
    • Crashing on tool errors instead of feeding back to LLM
    • Not streaming progress — user sees nothing for 30 seconds
    • Sequential execution of independent tools
    • No cost tracking — surprise API bills

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • while not done: observe→reason→act
    • max_steps=10-25
    • Feed errors back to LLM
    • Parallel independent tools
    • Stream steps to user
    • Log every iteration