Building Your First AI Agent: From Chatbots to Goal-Oriented Models
Learn how to transform a simple chatbot into a functional AI agent that not only communicates but also performs tasks effectively.
Building Your First AI Agent: From Chatbots to Goal-Oriented Models
Every developer faces the moment: “It's great at answering questions, but why can't it just do the work?”
What is an AI Agent?
Simply put, an AI Agent is a combination of LLM (model) + state + tools + loop.
Key Features
- Operates on a goal-oriented basis rather than merely conversational
- Utilizes a repeating loop of “think → act → observe → next action”
- Designed to call upon tools like calculators, files, databases, and APIs
Difference between Chatbots and Agents
- Chatbot: Input → Text Response
- Agent: Goal → Plan → Execute Tools → Validate Results → Next Action For instance, if you ask, “Organize this week's development tasks,” a chatbot just lists them. An agent, however, would check your calendar/issues tracker, prioritize, and draft a summary document.
Planning Your Agent: Start with Minimal Features
Attaching a huge framework from the beginning makes debugging tough. A minimal agent just needs these four components:
1️⃣ State: Stores goals, logs, and interim results 2️⃣ Policy: Decides the next action (model call) 3️⃣ Tools: Executes real actions (functions) 4️⃣ Loop: Repeats until termination conditions are met
Tool Design Tips
- Tools should focus on clear result-oriented tasks (calculating, fetching, storing) rather than “text generation.”
- Keep input/output simple for enhanced reliability.
- Always account for potential failures (exception handling, retries).
Hands-On Code: Creating a Tool-Based Agent in Python
Here's a simple agent with calculator and memo-saving tools. The model assumes OpenAI API compatibility, focusing on “select tool → execute → observe and react.”
import json
# --- Tools ---
def tool_calc(expression: str) -> str:
try:
# Demo purposes: Use a safe parser in real services
result = eval(expression, {"__builtins__": {}})
return str(result)
except Exception as e:
return f"calc_error: {e}"
MEMO = []
def tool_save_memo(text: str) -> str:
MEMO.append(text)
return f"saved({len(MEMO)}): {text[:30]}"
TOOLS = {
"calc": tool_calc,
"save_memo": tool_save_memo,
}
# --- Agent loop (assumes model returns 'next action' in JSON) ---
def fake_llm_decide(goal: str, observation: str) -> str:
"""
In practice, replace with LLM call.
Example return:
{"action":"tool","tool":"calc","input":"(12000*3)*0.9"}
{"action":"tool","tool":"save_memo","input":"Result: ..."}
{"action":"finish","output":"Final answer ..."}
"""
if not observation:
return json.dumps({"action":"tool","tool":"calc","input":"(12000*3)*0.9"}, ensure_ascii=False)
if "saved" not in observation:
return json.dumps({"action":"tool","tool":"save_memo","input":f"Calc result: {observation}"}, ensure_ascii=False)
return json.dumps({"action":"finish","output":"Calculation finished and saved."}, ensure_ascii=False)
def run_agent(goal: str, max_steps: int = 5):
state = {"goal": goal, "steps": []}
observation = ""
for step in range(max_steps):
decision = json.loads(fake_llm_decide(goal, observation))
state["steps"].append({"decision": decision, "observation": observation})
if decision["action"] == "finish":
return decision["output"], state
if decision["action"] == "tool":
tool_name = decision["tool"]
tool_input = decision["input"]
observation = TOOLS[tool_name](tool_input)
continue
observation = "unknown_action"
return "max_steps_reached", state
print(run_agent("Apply 10% discount on three items (each $120) and save the results.")[0])
Important Points from This Example
- Separating Decision from Observation simplifies debugging
- Clearly defined termination conditions (step limits, finish action)
- Loop doesn’t break even if tools fail (error strings passed as observations)
Example Scenarios: Agent Ideas for Immediate Use
Combining “repetition + rules + external data” creates immediate value in practical patterns.
- Release Note Agent: Summarizes issue lists → Categorizes changes → Generates release text → Saves files
- Code Review Assistant: Summarizes diffs → Detects risky sections → Creates checklists
- Error Triage: Parses logs → Searches similar issues (internal DB) → Organizes reproduction steps
Common Pitfalls and Solutions
Checklist for Enhancing Stability
- Validate Tool Input: Especially strict with numbers/dates/paths
- Separate Permissions: Differentiating “read tools” and “write tools” minimizes mistakes
- Store Observation Logs: Enables troubleshooting and improvements
- Limit Maximum Steps: Prevents infinite loops
Conclusion: Effective Agents Prioritize Flow Over Dialogue
The essence of creating an AI agent is not just smart models, but designing tools and loops to accomplish tasks. Start with the minimal structure introduced today, and expand further with 🔹API/DB tools 🔹planner stages 🔹verification stages for a robust agent.
#aiagent #development #python #llm #agent #automation #softwaredevelopment #productdevelopment
⬇️ If this helped, please click the ad below! It supports me a lot 🙇♂️ ⬇️
Related Posts
Multi-Agent Programming: Building a Collaborative System with "Multiple Mini-Developers"
Multi-agent programming involves designing software where autonomous components work together to achieve a common goal, akin to specialized developers collaborating on a project.
From Solo Coding to Team-Like AI: The Journey with Developer Devin and OpenHands
Explore how AI tools like Devin and OpenHands are transforming coding processes from solo efforts to collaborative workflows, delivering faster results for developers.
Is Local LLM Really Useful? A Developer's Perspective on Practical Value
Explore the benefits and challenges of using local LLMs versus cloud options, focusing on control, security, and cost-effectiveness.