Agentic AI Notebook
Agent Foundations
Phase 4Module 15 of 15

Build First AI Agent

Building reveals infinite loops, bad args, and context overflow that reading cannot teach.

Hello World for agents — small, ugly, foundation for everything after.

Visual Workflows

Start here — scroll inside each diagram frame to explore, then use + / to zoom up to 200% if needed.

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Minimal Agent Loop

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

The while-loop every framework implements under the hood.

File Layout

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Recommended project structure for your first no-framework agent.

Key Takeaways

  • 1.Minimal agent: one LLM two tools while-loop logs.
  • 2.Steps: goal prompt schemas loop max steps.
  • 3.Learn failure modes before using frameworks.
  • 4.Completable in one afternoon with OpenAI API.

Real Example

Scenario

Build an 80-line Python agent with `web_search` and `calculator` tools that answers: 'What is Japan's GDP per capita times 2?'

What you would do

Loop: user goal → LLM with tool schemas → if tool_calls, execute and append observation → repeat until text answer or max 10 steps. Log every iteration. Test on 5 questions mixing search and math. Monitor: steps used and tool selection accuracy.

Practice Task

Build the 2-tool agent with max 10 steps. Log every iteration to the console. Run it on 5 queries (mix search + math) and note where it fails.

Code Walkthrough

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

Build First AI Agent
1from openai import OpenAI  # import dependencies2
3client = OpenAI()  # create API client4tools = [5    {"type": "function", "function": {"name": "web_search", "parameters": {"type": "object", "properties": {"query": {"type": "string"}}}}},6    {"type": "function", "function": {"name": "calculator", "parameters": {"type": "object", "properties": {"expression": {"type": "string"}}}}},7]8
9def run_agent(goal: str, max_steps: int = 10) -> str:  # define a reusable function10    messages = [{"role": "user", "content": goal}]11    for step in range(max_steps):12        resp = client.chat.completions.create(model="gpt-4o-mini", messages=messages, tools=tools)  # core API call for Build First AI Agent13        msg = resp.choices[0].message14        print(f"Step {step + 1}:", msg.tool_calls or msg.content)  # show output for debugging15        if not msg.tool_calls:16            return msg.content or ""  # return the result17        messages.append(msg)18        # execute tool, append {"role": "tool", ...}, continue loop19    return "max steps reached"  # return the result20
21print(run_agent("Japan GDP per capita times 2 — show sources"))  # main loop: LLM → tools → append → repeat

Cheat Sheet

Quick recap

quick ref
  • 2-3 tools max at first
  • max_steps=10 always
  • Log every iteration
  • Validate tool JSON args
  • No framework until loop works
  • API key in .env never in code

Common Mistakes

  • Skipping evaluation for Build First AI Agent before production
  • No logging or tracing around build first ai agent steps
  • Ignoring cost and latency implications