Why Most AI Agents Are Just Fancy Loops
Build a minimal AI agent loop in Python and see how tools, memory, and control flow turn an API call into an agent.
The agent that wasn't
You asked your AI assistant to book a flight, update a spreadsheet, and send a Slack message. It did the first, ignored the second, and hallucinated the third. Sound familiar? Most so-called AI agents are just a single LLM call with a system prompt that says 'act as an agent.'
The reality is that an agent is a loop: call the model, parse the output, execute a tool, feed the result back, repeat until the task is done. That loop is simple to write, but most tutorials skip it. This article shows you the minimal loop, the tool schema, and the control flow that separates a demo from a working agent.
- You will build a Python agent that can query a database and send an HTTP request.
- You will see how tool schemas constrain the model's output.
- You will learn why the loop is the core of agentic behavior.
Before you start
You need Python 3.10 or newer and an OpenAI-compatible API key. The example uses the openai Python package, but the same pattern works with any chat completions endpoint.
Create a virtual environment and install the dependencies.
python -m venv venv
source venv/bin/activate # on Windows: venv\Scripts\activate
pip install openai python-dotenvStep 1: Define the tools
Before the agent can do anything, you must tell the model what tools exist. In the OpenAI API, tools are defined as JSON schemas. The model will output a structured request to call one of them.
Define two tools: one to look up a user's email in a fake database, and one to send a message via a webhook.
tools = [
{
"type": "function",
"function": {
"name": "get_user_email",
"description": "Get the email address of a user by their ID.",
"parameters": {
"type": "object",
"properties": {
"user_id": {"type": "string"}
},
"required": ["user_id"]
}
}
},
{
"type": "function",
"function": {
"name": "send_message",
"description": "Send a message to a webhook URL.",
"parameters": {
"type": "object",
"properties": {
"url": {"type": "string"},
"message": {"type": "string"}
},
"required": ["url", "message"]
}
}
}
]Step 2: Implement the tool functions
The schemas describe the interface, but you also need the actual Python functions that execute the work. Keep them small and pure so they are easy to test.
import requests
FAKE_DB = {
"user_123": "alice@example.com",
"user_456": "bob@example.com",
}
def get_user_email(user_id: str) -> str:
return FAKE_DB.get(user_id, "user not found")
def send_message(url: str, message: str) -> str:
response = requests.post(url, json={"text": message})
return f"status {response.status_code}"Step 3: Write the agent loop
Now the core. The loop does the following: send the conversation history and tools to the model, check if the response contains a tool call, execute the tool, append the result, and repeat. It stops when the model returns a plain text answer.
The key is that each tool result is appended as a message with role 'tool'. This keeps the conversation coherent.
from openai import OpenAI
import json
client = OpenAI()
def run_agent(user_input: str, max_iterations: int = 5):
messages = [{"role": "user", "content": user_input}]
tool_map = {
"get_user_email": get_user_email,
"send_message": send_message,
}
for _ in range(max_iterations):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
if not message.tool_calls:
return message.content
messages.append(message)
for tool_call in message.tool_calls:
fn_name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
result = tool_map[fn_name](**args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": result,
})
return "Max iterations reached"Step 4: Run it
Call the agent with a task that requires both tools. The model should first look up the email, then send a message to a webhook URL you provide. Use a local webhook endpoint for testing, such as webhook.site or a local server.
export OPENAI_API_KEY=your-key-here
python -c "
from agent import run_agent
print(run_agent('Send a message to user_123 saying hello'))
"- Watch the logs to see the sequence: tool call, tool result, final answer.
- If you get a 'tool_call_id' error, make sure you append the assistant message before the tool results.
- Try a task that needs multiple steps, like 'look up user_456 and then send them a status update'.
What makes it an agent
The loop is the agent. Without the loop, you just have a chat model. The loop gives the model the ability to act, observe, and act again. That is the pattern behind every agent framework, from LangChain to AutoGPT.
The difference is in the details: tool schemas, error handling, and stopping conditions. In production, you would add timeouts, retries, and a budget for the number of iterations.
- Tool schemas are contracts. The model must output valid JSON that matches them.
- Always cap the number of iterations to prevent infinite loops.
- Log every tool call and result for debugging and observability.
Recommended setup: a production-ready loop
For a real project, you want more than a toy. Here is a starter template that adds a system prompt, a max token limit, and a simple retry wrapper. Save it as agent.py and reuse it.
import json
import time
from openai import OpenAI
client = OpenAI()
SYSTEM_PROMPT = "You are a helpful assistant that can call tools. Always call tools when needed."
def call_model(messages, tools, max_retries=2):
for attempt in range(max_retries):
try:
return client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=tools,
tool_choice="auto",
)
except Exception as e:
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt)
def run_agent(user_input: str, tools, tool_map, max_iterations=5):
messages = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_input}]
for _ in range(max_iterations):
response = call_model(messages, tools)
message = response.choices[0].message
if not message.tool_calls:
return message.content
messages.append(message)
for tc in message.tool_calls:
fn = tool_map[tc.function.name]
args = json.loads(tc.function.arguments)
result = fn(**args)
messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
raise RuntimeError("Max iterations exceeded")- Replace the model ID with the one you have access to.
- Add a timeout to the HTTP client for real deployments.
- Use structured logging to capture each step.
Troubleshooting
If the agent does not call tools, check that your schemas are valid and that the model you use supports function calling.
If the tool result is ignored, ensure the tool message has the correct tool_call_id.
If the loop runs forever, lower max_iterations to 3 and inspect the intermediate messages.
If the model returns malformed JSON, add a try/except around json.loads and ask the model to correct itself.
FAQ
Q: Do I need a framework like LangChain?
A: No. The core loop is about 30 lines of code. Frameworks add abstractions, but start with the raw loop to understand the mechanics.
Q: Can I use other models?
A: Yes, any model with function calling works. The pattern is the same; only the API details change.
Q: How do I add memory?
A: Append the conversation history to the messages list. The model uses that as context.
Q: What about multiple tools in one response?
A: The loop handles that naturally by iterating over message.tool_calls.
Q: How do I stop the agent from making dangerous calls?
A: Add a permission check inside each tool function, or wrap the loop with a human approval step.
Next step
Now that you have a working loop, extend it: add a tool to read a file, or connect it to a real database. The pattern is the same.
Try this: modify the send_message function to log to a file instead of a webhook, and run the agent with a multi-step task. Watch the loop iterate.
Key takeaways
- Apply one concrete change from this post before collecting more reading.
- Prefer browser-side tools when the work involves secrets, tokens, or PII.
- Document the why next to the how so the next reviewer inherits context.
FAQ
- Who is this guide on ai-agents for?
- Working developers who need a practical take on why most ai agents are just fancy loops — not a marketing overview. Skim the sections, apply one tip, then come back when you hit an edge case.
- Do I need an account to use the related tools?
- No. code.live tools run in your browser with no signup. Nothing you paste is uploaded to a server for the client-side utilities linked from this post.
- How often is this article updated?
- This post was published November 16, 2026. Fundamentals stay stable; check linked tool pages and official docs when version-specific behavior matters.