AWS Builder Center
What Is an AI Agent? Build One From Scratch on Amazon Bedrock

What Is an AI Agent? Build One From Scratch on Amazon Bedrock

An AI agent is an LLM with tools in a loop to achieve a goal. Build one with the Amazon Bedrock Converse API and no framework, rebuild it with Strands Agents, and learn the gotchas to fix before production.

AI Engineer | AWS Student Builder Group HUFLIT

TL;DR

An AI agent is an LLM with tools in a loop to achieve a goal. The code around the model (tools, loop, stop rule) is the harness. Below, you build one on Amazon Bedrock with no framework, then again in 15 lines with Strands Agents.
Level: 200 · You need: Python 3.10+,  boto3 , Bedrock access to a tool-capable model.

How tool use actually works

An LLM only predicts tokens. "Using a tool" means the model emits a structured request, and your code runs the tool and sends the result back. That code is also where you enforce limits, permissions and logging.



Build it: a raw agent loop on Bedrock

An order support agent with two tools. "Where is my order and when will it arrive?" needs two dependent calls, so the loop runs twice.

Step 1: Write the tools and their specs

Tools are plain Python functions. The spec is what the model reads, so the  description  is effectively a prompt.
python
import boto3

MODEL_ID = "us.amazon.nova-pro-v1:0" # any tool-capable model enabled in your account
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")

ORDERS = {"A123": {"status": "shipped", "carrier": "VNPost", "tracking": "VN998877"}}

def get_order(order_id: str) -> dict:
if order_id not in ORDERS:
raise ValueError(f"Order {order_id} not found")
return ORDERS[order_id]

def get_eta(carrier: str, tracking: str) -> dict:
return {"tracking": tracking, "eta_days": 2} # stub: call the carrier API here

TOOLS = {"get_order": get_order, "get_eta": get_eta}

TOOL_CONFIG = {"tools": [
{"toolSpec": {
"name": "get_order",
"description": "Look up an order by ID. Returns status, carrier and tracking number.",
"inputSchema": {"json": {"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"]}}}},
{"toolSpec": {
"name": "get_eta",
"description": "Get delivery ETA in days. Needs carrier and tracking from get_order.",
"inputSchema": {"json": {"type": "object",
"properties": {"carrier": {"type": "string"}, "tracking": {"type": "string"}},
"required": ["carrier", "tracking"]}}}},
]}

Step 2: Write the loop (this is the harness)

python
def run_agent(goal: str, max_turns: int = 8) -> str:
messages = [{"role": "user", "content": [{"text": goal}]}]

for turn in range(max_turns):
resp = bedrock.converse(
modelId=MODEL_ID,
system=[{"text": "You are an order support agent. Use tools. Never guess order data."}],
messages=messages,
toolConfig=TOOL_CONFIG,
)
msg = resp["output"]["message"]
messages.append(msg) # keep toolUse blocks verbatim

if resp["stopReason"] != "tool_use": # goal reached (or model gave up)
return "".join(b.get("text", "") for b in msg["content"])

results = []
for block in msg["content"]:
if "toolUse" not in block:
continue
call = block["toolUse"]
print(f"[turn {turn}] {call['name']}({call['input']})")
try:
output = TOOLS[call["name"]](**call["input"])
results.append({"toolResult": {"toolUseId": call["toolUseId"],
"content": [{"json": output}]}})
except Exception as e:
results.append({"toolResult": {"toolUseId": call["toolUseId"],
"content": [{"text": str(e)}],
"status": "error"}})
messages.append({"role": "user", "content": results})
return "Stopped: max_turns reached before the goal was met."

print(run_agent("Where is my order A123 and when will it arrive?"))

Step 3: Run it

Typical output (wording varies by model):
text
[turn 0] get_order({'order_id': 'A123'})
[turn 1] get_eta({'carrier': 'VNPost', 'tracking': 'VN998877'})
Your order A123 has shipped with VNPost (tracking VN998877) and should arrive in about 2 days.

Why the code looks like this

  •  stopReason  drives the loop.  tool_use  means run tools, anything else means done. Never regex the text.
  • Append the assistant message verbatim. Each  toolResult  must match a  toolUseId  from the previous turn.
  • Errors become data.  status: error  lets the model recover (try  A999 ) instead of crashing the run.
  •  max_turns  is the safety net against endless loops and runaway token cost.

Same agent with Strands Agents

Frameworks remove the boilerplate you just wrote. Here is the same agent with the open source  Strands Agents SDK  ( pip install strands-agents ):
python
from strands import Agent, tool
from strands.models import BedrockModel

ORDERS = {"A123": {"status": "shipped", "carrier": "VNPost", "tracking": "VN998877"}}

@tool
def get_order(order_id: str) -> dict:
"""Look up an order by ID. Returns status, carrier and tracking number."""
if order_id not in ORDERS:
raise ValueError(f"Order {order_id} not found")
return ORDERS[order_id]

@tool
def get_eta(carrier: str, tracking: str) -> dict:
"""Get delivery ETA in days. Needs carrier and tracking from get_order."""
return {"tracking": tracking, "eta_days": 2}

agent = Agent(
model=BedrockModel(model_id="us.amazon.nova-pro-v1:0"),
system_prompt="You are an order support agent. Use tools. Never guess order data.",
tools=[get_order, get_eta],
)
agent("Where is my order A123 and when will it arrive?")
Strands builds tool specs from type hints and docstrings, runs the loop, and turns exceptions into error results. Which framework you pick matters less than data, evals and observability, so use the one your team knows.

Gotchas before you ship

Gotcha
Fix
Context (and cost) grows every turn
Cap turns, return only the fields the model needs
Vague tool descriptions cause wrong calls
Write them like API docs: returns what, when to use, inputs from where
Side-effect tools (refund, delete)
Make them idempotent, add human approval
A fixed A, B, C workflow
Skip the agent and write a function
Agents trade latency, cost and determinism for flexibility. Use one only when the next step depends on what you just found.

Wrap-up

Agent = LLM + harness. The model predicts tokens; your harness turns them into actions and decides when to stop. Write the raw loop once and every framework becomes easy to read and debug.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article