Your first AI agent
Combine a model, tools, and a loop so it can plan and act across multiple steps.
Prerequisites
- Tool use & function calling
- Retrieval-augmented generation (RAG)
You will learn
- Explain what makes something an agent versus a single call
- Build an agent loop that runs until the task is done
- Add stopping conditions so the loop is safe and bounded
Telugu lo nerchuko · Watch in Telugu
An agent is what you get when you let the model use tools in a loop. Instead of one request and one answer, the model takes a step, sees the result, decides the next step, and repeats until the task is finished. This unlocks multi-step work — but a loop with no guardrails is also how you get runaway cost.
Overview
A single tool call answers one question. An agent strings many together: it can search, then read a result, then call another tool based on what it found, then summarise. The model drives the sequence. Your job is to provide the tools, run the loop, feed results back, and decide when to stop.
Key ideas
One call versus an agent
In Week 3's tool lesson you ran the loop once by hand. An agent generalises that into a while loop that keeps going as long as the model keeps requesting tools, and ends when the model produces a final answer.
The agent loop
from anthropic import Anthropic
client = Anthropic()
def run_agent(user_message, tools, tool_functions, max_steps=8):
messages = [{"role": "user", "content": user_message}]
for step in range(max_steps):
response = client.messages.create(
model="claude-sonnet-4-6", max_tokens=1024,
tools=tools, messages=messages,
)
messages.append({"role": "assistant", "content": response.content})
if response.stop_reason != "tool_use":
return response.content[0].text # final answer
# Execute every tool the model asked for this turn
results = []
for block in response.content:
if block.type == "tool_use":
output = tool_functions[block.name](**block.input)
results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": str(output),
})
messages.append({"role": "user", "content": results})
return "Stopped: reached the step limit before finishing."Stopping conditions keep it safe
The max_steps cap is not optional. A model can loop — calling a tool, getting an unexpected result, trying again — and without a limit it burns tokens until your budget runs out. Always bound the loop by step count, and consider a wall-clock timeout and a token budget too.
Designing good tools for agents
Agents work best with a small set of sharp, well-described tools. Too many tools confuse the model about which to pick; vague descriptions cause wrong choices. Give each tool one clear job and a precise description, and return results in a form the model can read and reason about.
Quick recap
- An agent is a model using tools in a loop until the task is done.
- The loop runs while the model requests tools and ends on a final answer.
- Always bound the loop with a step limit; add timeouts and token budgets too.
- Give the agent a few sharp tools with precise descriptions, not many vague ones.
- Log the full trace so you can debug the agent's decisions.