AI agents: what they are and how to build one
What an AI agent is, how it differs from a chatbot, and how to build one in Python from scratch and then with LangChain.

What is an AI agent?
An AI agent is a language model that can act, not only talk: you give it a goal and tools, it decides which tool to use, reads the result, and continues until it has an answer.
| Chatbot | Agent | |
|---|---|---|
| What it does | Replies with text | Calls tools, then replies |
| Where facts come from | What the model learned | Your data and systems, through tools |
| Example | "How do I track my order?" and it explains | It checks your order and tells you its status |
The four parts
- Model (LLM): decides.
- Tools: functions you write.
- Instructions: its role and limits.
- Loop: run the tool and return the result to the model until it finishes.
Build it from scratch
No framework, so you see what really happens:
ollama pull qwen2.5:3b
pip install ollama
from ollama import chat
ORDERS = {"A100": "shipped", "B200": "preparing"}
def get_order_status(order_id: str) -> str:
"""Get the status of an order.
Args:
order_id: The order number, for example A100.
"""
return ORDERS.get(order_id.upper(), "order not found")
TOOLS = {"get_order_status": get_order_status}
messages = [
{"role": "system", "content": "You are a shop assistant. Use the tools for order questions. Never guess."},
{"role": "user", "content": "Where is my order A100?"},
]
for step in range(5): # a hard limit, so the agent can never loop forever
response = chat(model="qwen2.5:3b", messages=messages, tools=list(TOOLS.values()), options={"temperature": 0})
messages.append(response.message)
if not response.message.tool_calls: # no tool needed: this is the final answer
print(response.message.content)
break
for call in response.message.tool_calls:
tool = TOOLS.get(call.function.name)
result = tool(**call.function.arguments) if tool else "unknown tool"
print(f"[tool] {call.function.name}({call.function.arguments}) -> {result}")
messages.append({"role": "tool", "tool_name": call.function.name, "content": result})
The output we got:
[tool] get_order_status({'order_id': 'A100'}) -> shipped
Your order A100 has been shipped.
Notice that your code runs the tool. The model only asks.
Then with LangChain
The same loop becomes shorter:
pip install -U langchain langchain-ollama
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.tools import tool
ORDERS = {"A100": "shipped", "B200": "preparing"}
@tool
def get_order_status(order_id: str) -> str:
"""Get the status of an order by its number, for example A100."""
return ORDERS.get(order_id.upper(), "order not found")
agent = create_agent(
model=init_chat_model("ollama:qwen2.5:3b", temperature=0),
tools=[get_order_status],
system_prompt="You are a shop assistant. Use the tools for order questions. Never guess.",
)
result = agent.invoke({"messages": [{"role": "user", "content": "Where is my order B200?"}]})
print(result["messages"][-1].content)
Rules before you trust it
- A maximum number of steps, so it can't loop forever.
- The fewest permissions possible for each tool.
- Human approval before anything that can't be undone (LangGraph example).
- Don't trust outside text: a page or message may contain instructions meant to trick the agent.
- Log every tool call so you know what happened.
When you don't need an agent
If the steps are known and fixed, write ordinary code. It is cheaper, faster and easier to test.
Keep learning
Code tested on 30 November 2026 with Python 3.10, LangChain 1.4 and LangGraph 1.2.