A chatbot answers a question. An AI agent tries to complete a task. Given a goal such as "find the cheapest supplier and draft an order email", an agent can break the goal into steps, use tools like search or a database, check intermediate results and continue until it finishes or needs help. Agents are one of the most discussed ideas in AI, so it is worth understanding what is real and what is hype.
A working definition
An AI agent is a system in which a language model repeatedly decides what to do next, performs actions through tools, observes the results and updates its plan, until a goal is reached or a limit is hit.
Four ingredients define most agents.
- A model that reasons about the task and chooses actions.
- Tools the model can call: web search, calculator, code execution, file access, email, calendar, a database or any API.
- Memory to keep track of what has happened, both short-term (the current conversation and results) and sometimes long-term (stored notes or documents).
- A loop that runs until the task is done.
The basic agent loop
goal -> model decides next action -> tool runs -> result returned -> model decides again -> ... -> final answer
Each pass is often called a step. The model sees the goal, the list of available tools and everything that has happened so far, and responds with either a tool call or a final answer.
Planning
Good agents plan before and during action. Common approaches:
- Plan then execute: write a list of steps first, then carry them out one by one.
- Reason and act (ReAct style): alternate between thinking about what to do and taking an action, adapting after each observation.
- Reflection: after trying, the agent checks its result against the goal and fixes mistakes.
- Sub-agents: a main agent hands parts of the job to specialised helpers.
Planning helps with multi-step tasks, but every extra step can introduce errors, so simple tasks are often better handled by a normal program or a single prompt.
Memory
- Working memory is the text in the model's context window: instructions, past steps and tool outputs.
- Long-term memory is information saved outside the model, such as notes in a database or vector store, retrieved when relevant.
- Summarisation compresses old steps so the context window does not overflow.
A minimal agent in Python
The sketch below shows the loop using a language model API with tool use. It gives the model two tools and lets it call them until it has an answer.
import json
import anthropic
client = anthropic.Anthropic()
def get_price(item: str) -> str:
prices = {"rice": 120, "lentils": 210, "oil": 330}
return json.dumps({"item": item, "price_per_unit": prices.get(item.lower())})
def calculate(expression: str) -> str:
allowed = set("0123456789+-*/(). ")
if not set(expression) <= allowed:
return "Invalid expression"
return str(eval(expression, {"__builtins__": {}})) # tiny demo only
TOOLS = [
{"name": "get_price", "description": "Look up price per unit of a grocery item.",
"input_schema": {"type": "object", "properties": {"item": {"type": "string"}}, "required": ["item"]}},
{"name": "calculate", "description": "Evaluate a simple arithmetic expression.",
"input_schema": {"type": "object", "properties": {"expression": {"type": "string"}}, "required": ["expression"]}},
]
FUNCS = {"get_price": get_price, "calculate": calculate}
messages = [{"role": "user", "content": "How much do 3 units of rice and 2 units of oil cost in total?"}]
for step in range(6): # hard limit prevents endless loops
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=500,
tools=TOOLS, messages=messages)
if reply.stop_reason != "tool_use":
print(reply.content[0].text)
break
messages.append({"role": "assistant", "content": reply.content})
results = []
for block in reply.content:
if block.type == "tool_use":
output = FUNCS[block.name](**block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
messages.append({"role": "user", "content": results})
Notice the step limit and the restricted calculate tool. In real systems, never pass model-generated text to eval without strong safeguards; use a proper expression parser or sandbox.
Where agents are useful
- Research tasks that need several searches and a written summary
- Customer-support flows that check an order, a policy and then reply
- Data tasks such as querying a database and building a short report
- Developer helpers that read code, run tests and propose fixes
- Workflow automation connecting email, spreadsheets and calendars
Risks and limitations
- Compounding errors: a small mistake early can derail later steps.
- Cost and time: many model calls add up.
- Unpredictability: the same goal can take different paths on different runs.
- Tool misuse: an agent with email or payment access can cause real damage if it misunderstands.
- Prompt injection: text inside a web page or document can try to hijack the agent's instructions.
- Over-trust: people may accept actions without checking.
Safer agent design
- Give the agent the minimum permissions it needs.
- Require human approval before irreversible actions such as sending money or deleting data.
- Set step, time and cost limits.
- Log every action and tool result for review.
- Validate tool inputs in code, not only in the prompt.
- Test on realistic scenarios, including adversarial ones.
Frequently Asked Questions
Is an agent different from a chatbot?
A chatbot mainly replies in conversation. An agent can take actions through tools and pursue a goal over multiple steps.
Do agents work reliably today?
They work well on narrow, well-defined tasks with good tools and monitoring. Open-ended autonomy is still error-prone.
Do I need special frameworks?
No. You can start with a simple loop like the one above. Frameworks can help once you need features such as tracing, memory and multi-agent coordination.
Should every automation be an agent?
No. If the steps are fixed and known, a normal script or workflow is cheaper, faster and more predictable.
Conclusion
An AI agent combines a language model with tools, memory and a loop to pursue goals. Used carefully, with limits and human approval for important actions, agents can automate multi-step work. Used carelessly, they can fail in surprising ways.