What is an AI Agent?
An agent uses an LLM to plan and take multiple actions toward a goal.
An AI agent is a system where an LLM decides what to do next, uses tools to do it, observes the result, and repeats — until a goal is reached. It is tool use in a loop with autonomy.
Chatbot vs. agent
- A chatbot answers one message at a time.
- An agent breaks a goal into steps and works through them, calling tools as needed, without a human directing each step.
Example
Goal: "Find the cheapest flight next Friday and add it to my calendar." An agent might search flights (tool), compare results (reasoning), and create a calendar event (tool) — several steps from one instruction.
Example
{
"goal": "Summarize this week's support tickets and email the summary to the team.",
"agent_steps": [
"fetch_tickets(range='7d')",
"summarize(tickets)",
"send_email(to='team', body=summary)"
]
}When to use it
- A research agent is given the goal 'find and summarise the top 5 papers on RAG published in 2024' and autonomously searches, reads, and writes the summary.
- A DevOps agent monitors a CI pipeline, identifies failing tests, edits the relevant code, and re-runs the pipeline without human intervention.
- A customer-service agent handles a return request end-to-end: looks up the order, checks the return policy, initiates the refund, and sends a confirmation email.
More examples
Minimal agentic loop structure
Shows the minimal agent structure: a loop that calls tools until a 'finish' action signals the task is complete.
from openai import OpenAI
import json
client = OpenAI()
def search_web(query): return f'Search results for "{query}": [result1, result2]'
def finish(answer): return answer # terminal action
FNS = {'search_web': search_web, 'finish': finish}
tools = [
{'type':'function','function':{'name':'search_web','description':'Search the web.','parameters':{'type':'object','properties':{'query':{'type':'string'}},'required':['query']}}},
{'type':'function','function':{'name':'finish','description':'Return final answer.','parameters':{'type':'object','properties':{'answer':{'type':'string'}},'required':['answer']}}},
]
messages = [{'role':'system','content':'You are an agent. Use tools to complete the task.'},
{'role':'user','content':'Find the capital of Australia.'}]
for _ in range(5):
r = client.chat.completions.create(model='gpt-4o-mini', messages=messages, tools=tools)
msg = r.choices[0].message
messages.append(msg)
if not msg.tool_calls: break
for tc in msg.tool_calls:
result = FNS[tc.function.name](**json.loads(tc.function.arguments))
messages.append({'role':'tool','tool_call_id':tc.id,'content':str(result)})
if tc.function.name == 'finish':
print('Done:', result); break
else: continue
breakAgent with goal and tool set
Wraps the agent loop in a function so you pass a goal and get the final answer back after autonomous tool use.
from openai import OpenAI
import json
client = OpenAI()
tools = [
{'type':'function','function':{'name':'read_file','description':'Read a file by path.','parameters':{'type':'object','properties':{'path':{'type':'string'}},'required':['path']}}},
{'type':'function','function':{'name':'write_file','description':'Write content to a file.','parameters':{'type':'object','properties':{'path':{'type':'string'},'content':{'type':'string'}},'required':['path','content']}}},
]
def run_agent(goal):
messages = [
{'role':'system','content':'Complete the goal using the available tools.'},
{'role':'user','content':goal}
]
def read_file(path): return f'Contents of {path}: hello world'
def write_file(path, content): return f'Written {len(content)} chars to {path}'
FNS = {'read_file': read_file, 'write_file': write_file}
for _ in range(6):
r = client.chat.completions.create(model='gpt-4o-mini', messages=messages, tools=tools)
msg = r.choices[0].message
messages.append(msg)
if not msg.tool_calls:
return msg.content
for tc in msg.tool_calls:
res = FNS[tc.function.name](**json.loads(tc.function.arguments))
messages.append({'role':'tool','tool_call_id':tc.id,'content':str(res)})
print(run_agent('Read hello.txt and write its content in uppercase to upper.txt'))Distinguish agent vs single LLM call
Contrasts a one-shot LLM call (guessing) with an agent that uses real tools to act on the environment.
from openai import OpenAI
client = OpenAI()
# Single call: one-shot, no autonomy
r = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role':'user','content':'What files are in /tmp?'}]
)
print('Single call:', r.choices[0].message.content[:80])
# ^ model guesses; no real file access
# Agent: could call a 'list_directory' tool, get the real answer,
# then reason about it — multiple turns, real actions.
print('Agent: would call list_directory("/tmp") and return real results.')
Discussion