Introduction

An AI agent is not a single chatbot that magically knows everything. Under the hood, it runs a repeating loop: it observes something, thinks about what to do, picks a tool, and takes action. Then it observes the result of that action and loops again. Understanding this loop is the foundation for everything that follows in this course — connecting databases, calling APIs, and handling authentication are all "action" steps inside this loop.

The Perception–Planning–Action Loop

The agent loop has four phases that repeat continuously until the agent's task is done.

Perception is everything the agent can read or sense: the user's prompt, conversation history, the output of a previous tool call, or data fetched from a database. Planning is the LLM's reasoning step where it decides what should happen next. Tool selection is the moment the model picks a specific function (like query_database or send_email) and produces the arguments for it. Action is when that tool actually runs and returns a result.

flowchart of4 steps in a circle: Perception -> Planning -> Tool Selection -> Action -> back to Perception

After the action returns, the result becomes new perception input. The agent then re-plans. This is why agents can chain calls — one tool's output informs the next decision.

How the Model Decides What Tool to Use

Modern agents are given a structured list of available tools, each with a name, description, and parameter schema. The LLM reads these descriptions and decides which one fits the current need. It does not "know" how to call an API by itself — it picks the tool that you, the developer, registered.

python
tools = [
{
"name": "query_database",
"description": "Run a read-only SQL query against the users database.",
"parameters": {
"type": "object",
"properties": {
"sql": {"type": "string", "description": "SELECT statement only"}
},
"required": ["sql"]
}
},
{
"name": "get_weather",
"description": "Fetch current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
]

When the user asks "How many users signed up last week?", the model matches the request to query_database, generates the SQL argument, and the runtime executes it. Notice the agent did not invent its own ability — it selected from a menu you defined.

Where External Integrations Fit

Every database query, API call, or auth check is just another tool in that menu. From the agent's perspective there is no difference between "look up an order in Postgres" and "call the Stripe refund endpoint" — both are tools with a schema and a handler function. The course will build these handlers one by one: first read-only DB queries, then write operations, then external APIs, then authenticated flows.

python
def handle_tool_call(tool_name, arguments):
if tool_name == "query_database":
return db.execute(arguments["sql"])
if tool_name == "get_weather":
return requests.get("https://api.weather.example/current",
params={"city": arguments["city"]}).json()
raise ValueError(f"Unknown tool: {tool_name}")

This handler is where the real work happens. The LLM only proposes what to do — your code decides what is safe to do.

Common Pitfalls

Treating the LLM as omniscient — it cannot call tools you have not registered, and it cannot reach data you have not given it a tool for. Confusing planning with execution — the model only proposes; your runtime must validate and run. Letting the agent loop forever — always set a max-iterations limit so a misbehaving tool cannot trap the agent. Finally, never trust raw model output as a direct database or API call without validation; SQL injection and bad API payloads come from skipping this step.

Summary

An AI agent is a loop: perceive, plan, pick a tool, act, observe the result, repeat. Tools are the bridge between the LLM's reasoning and the outside world. Databases, APIs, and authentication layers are all implemented as tools with schemas and handlers. Mastering this mental model is the prerequisite for everything else in this course.

Lesson Checkpoint

1. What are the four phases of the AI agent loop introduced in this lesson?

2. Where does the real execution of a database query happen in an agent system?

3. Why does a developer register tools with descriptions and parameter schemas?

4. After a tool call returns a result, what does the agent do next?

5. From the agent's perspective, what is the difference between querying a database and calling an external API like Stripe?

6. Which of the following is a recommended safeguard when running an agent loop?

7. What is a common misconception about AI agents mentioned in the lesson?

8. What happens at the "Action" phase of the agent loop?