Autonomous AI agents represent the biggest paradigm shift in modern computing since the cloud. Rather than simply responding to static prompts, modern agents can reason through multi-step problems, execute tools, analyze API outputs, and adjust their strategy dynamically until they achieve a goal.
What Exactly is an AI Agent?
An AI agent is composed of four core building blocks:
- The Core Model (LLM): Serves as the central reasoning engine (e.g. GPT-4o, Claude 3.5 Sonnet, or local open-weight models).
- Memory: Retains context, conversation history, and intermediate calculation states.
- Tool Declarations: JSON Schema definitions explaining what functions the agent can invoke (e.g., query a database, fetch a URL, or send an email).
- Execution Loop: The runtime harness that parses tool calls from the model, executes the corresponding Python logic, feeds results back into the conversation, and stops when the task is complete.
Step 1: Defining a Custom Tool
Tools in Python are defined with clear schemas so the LLM knows when and how to call them. Here is an example of a tool that checks product pricing in a local database:
# tools.py
tools_spec = [
{
"type": "function",
"function": {
"name": "lookup_market_price",
"description": "Fetch the current wholesale and retail price of goods in major Lagos markets.",
"parameters": {
"type": "object",
"properties": {
"item_name": {
"type": "string",
"description": "The item to search for, e.g. 'HP EliteBook 840 G6' or 'Yam tubers'"
},
"market_location": {
"type": "string",
"enum": ["Computer Village", "Alaba International", "Balogun", "Mile 12"],
"description": "The market to query"
}
},
"required": ["item_name", "market_location"]
}
}
}
]
Step 2: The ReAct Execution Loop
The agent loop runs in a loop until the model returns a final text message without requesting any further tool calls:
import json
from openai import OpenAI
client = OpenAI()
def run_agent(user_prompt):
messages = [
{"role": "system", "content": "You are a helpful market intelligence assistant equipped with live pricing tools."},
{"role": "user", "content": user_prompt}
]
while True:
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools_spec,
tool_choice="auto"
)
msg = response.choices[0].message
messages.append(msg)
# If model did not request any tools, we are finished!
if not msg.tool_calls:
return msg.content
# Dispatch tool calls
for tool_call in msg.tool_calls:
fn_name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
if fn_name == "lookup_market_price":
# Call local Python function
result = {"status": "success", "average_price_ngn": 245000, "in_stock": True}
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result)
})
Key Production Best Practices
- Implement Strict Max Iteration Guards: Always cap agent loops at 10 or 15 iterations to prevent runaway token expenditure.
- Validate Parameters: Use Pydantic or type hints to validate tool arguments before running database queries.
- Human-in-the-Loop for Destructive Actions: If an agent can delete records, transfer money, or send mass emails, require human confirmation before execution.
By mastering tool calling and agent loops, you can build real-time assistants that automate days of manual administrative work in seconds.

Discussion (0)
No comments yet. Be the first to share your thoughts!