Most "build an AI agent" tutorials skip straight to higher-level frameworks like LangGraph, CrewAI, or AutoGen. As a result, developers call .invoke() without seeing the mechanics underneath. Beneath every agent framework lies a single, surprisingly straightforward control loop.

To make this walkthrough accessible, the examples in this guide use a Gemini model. Because anyone with a Google Account can obtain a free-tier API key, you can run the code and follow along directly without running into paid platform barriers or complex setups.

The Loop at a Glance

Every AI agent, regardless of framework, executes the same four-step cycle:

Send Context: Pass the conversation history and tool definitions to the LLM.