📦 Inventory Checker
custom tool with mock DB via Strands + Bedrock
Concept
When community tools don't fit (internal APIs, proprietary databases), you write your own. The @tool decorator, type hints, and docstring are all Strands needs — the mock dictionary here stands in for a real database. The agent-facing interface is identical regardless of what's behind the tool.
Theory & Concepts
In this Inventory Checker demo, we tackle a fundamental limitation of AI: Large Language Models (LLMs) cannot natively access private or real-time data like an internal store database. We solve this through Custom Tool Integration. When the user asks "Do you have PROD-123 in stock?", the Bedrock agent doesn't try to guess the answer. Instead, it knows it has access to a tool named check_inventory.
This is made possible by the @tool decorator in inventory_agent.py. By adding this decorator, along with standard Python type hints (like product_id: str) and a descriptive docstring, the Strands framework automatically generates a tool schema. The agent reads this schema and understands that to answer an inventory question, it must output a specific tool call containing the product_id.
What makes this architecture powerful is the separation of concerns. In our demo, the check_inventory function simply looks up the product in a hardcoded mock dictionary ({"PROD-123": 15, ...}). However, because the agent only interacts with the tool's interface, you could easily swap this dictionary for a real SQL query against a production database. The agent's logic remains completely unchanged, proving how easily you can prototype and scale agentic workflows.
Request flow
Code flow
Backend
inventory_agent.py"""Module 2 demo — a custom tool: an online-store inventory check.
When community tools don't fit (internal API, proprietary database) you write
your own. The mock dictionary stands in for a real database; the agent-facing
part — decorator, type hints, docstring — is identical either way.
"""
from strands import Agent, tool
from strands.models.bedrock import BedrockModel
from config import MODEL_ID, agent_text
@tool
def check_inventory(product_id: str) -> str:
"""Check if a product is in stock.
Args:
product_id: The product ID to check (e.g., "PROD-123")
"""
# In production you'd query your real database here.
inventory = {"PROD-123": 15, "PROD-456": 0, "PROD-789": 8}
quantity = inventory.get(product_id, 0)
if quantity > 0:
return f"Product {product_id} is in stock. We have {quantity} units available."
return f"Product {product_id} is currently out of stock."
def check(question: str) -> str:
"""Answer a stock question using the check_inventory custom tool."""
agent = Agent(
model=BedrockModel(model_id=MODEL_ID),
tools=[check_inventory],
callback_handler=None,
)
return agent_text(agent(question))