Introduction
This is the most important lesson of the series — you will implement a complete agent loop from scratch, without using any framework. Understanding this loop in depth will help you debug and optimize agents more effectively when using the framework later.
1. The Agent Loop — Core Architecture
class SimpleAgent:
def __init__(self, model="gpt-4o-mini", max_steps=10):
self.client = OpenAI()
self.model = model
self.max_steps = max_steps
self.tools = ToolRegistry()
self.messages = []
self.cost_tracker = CostTracker()
def run(self, user_input: str) -> str:
self.messages = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": user_input}
]
for step in range(self.max_steps):
response = self.client.chat.completions.create(
model=self.model,
messages=self.messages,
tools=self.tools.schemas,
)
message = response.choices[0].message
self.messages.append(message)
self.cost_tracker.track(self.model, response.usage)
# No tool calls → final answer
if not message.tool_calls:
return message.content
# Execute tools
for tool_call in message.tool_calls:
result = self.tools.execute(
tool_call.function.name,
json.loads(tool_call.function.arguments)
)
self.messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result),
})
return "Max steps reached without final answer."
2. Token Budget Management
def trim_messages(messages, max_tokens=100000):
"""Giữ messages trong budget token."""
# Luôn giữ system prompt và user message gần nhất
essential = [messages[0], messages[-1]]
middle = messages[1:-1]
# Xóa messages cũ nhất nếu vượt budget
while estimate_tokens(essential + middle) > max_tokens and middle:
middle.pop(0)
return [essential[0]] + middle + [essential[1]]
3. Stopping Conditions
Agent needs to know when to stop:
- LLM decides to stop at its own discretion (no additional tools are called)
- Max steps reached — safety net
- Token budget exceeded — avoid unexpected costs
- Error threshold — too many tool failures in a row
- User interrupt — human-in-the-loop
Summary
- Agent loop = while loop calls LLM → check tool_calls → execute → repeat
- Token budget management prevents agents from "going wild"
- Multiple stopping conditions for safety
- Logging every step to debug
Exercises
- Implement SimpleAgent class complete with 5+ tools
- Add token budget management
- Add conversation history persistence (save/load from file)
- Test with complex tasks that require 5+ steps