Model-Driven AI Agents: Building Sophisticated Tools with Strands-Agents/sdk-python
This SDK is particularly exciting because it allows you to build sophisticated AI agents using a model-driven approach with minimal code.
From a software engineer's viewpoint, strands-agents/sdk-python offers several key benefits that simplify complex AI development
The traditional way of building an agent involves a lot of boilerplate code for managing tool calling, conversational history, state, and complex LLM prompting (like few-shot examples or RAG). This SDK abstracts away much of that complexity. You only need to define the agent's behavior using a model (like a structured configuration), and the SDK handles the rest. This drastically speeds up development.
Agents often need to produce structured data (e.g., JSON) to trigger downstream systems or actions. The SDK enforces this structure, making the agent's output more reliable and easier for your application code to consume. This reduces the time spent on parsing and validation.
AI agents need to interact with the real world (databases, APIs, services) through "tools" or "functions." The SDK provides a clear and straightforward way to define and inject these tools, allowing the agent to decide when and how to use them to fulfill a request.
You can install the SDK using pip.
pip install strands-agents-sdk
The core concept is creating an Agent instance and then defining its Model and Tools.
Here is a comprehensive example demonstrating how to build a simple "Weather & Date Agent" that can answer questions using both internal knowledge and external tools.
We'll define two simple Python functions that the agent can "call" as tools.
from datetime import datetime
import json
# Tool 1: Gets the current date and time
def get_current_datetime() -> str:
"""Returns the current date and time in ISO format."""
return datetime.now().isoformat()
# Tool 2: Simulates fetching weather data for a city
def get_city_weather(city: str) -> str:
"""
Fetches the current weather for a specified city.
Args:
city: The name of the city (e.g., 'Tokyo', 'London').
"""
if 'tokyo' in city.lower():
return json.dumps({"city": "Tokyo", "temperature": "25C", "condition": "Sunny"})
elif 'london' in city.lower():
return json.dumps({"city": "London", "temperature": "12C", "condition": "Cloudy"})
else:
return json.dumps({"error": "Weather data not available for this city"})
We instantiate the agent and pass it the tools it can use.
from strands_agents_sdk.agent import Agent
# IMPORTANT: You need to set up your LLM environment variables (e.g., OPENAI_API_KEY)
# The Agent class will automatically detect and use the LLM provider.
# List of tools the agent can use
tools_list = [get_current_datetime, get_city_weather]
# Initialize the Agent
# The 'model' argument specifies the LLM to use (e.g., 'gpt-4o' or 'claude-3-opus')
agent = Agent(
tools=tools_list,
model="gpt-4o" # Use a powerful model for best tool-use capability
)
print(" Agent initialized with two tools: get_current_datetime and get_city_weather.")
Now we can ask the agent questions. The key here is that the agent decides whether to answer directly or to first use a tool.
user_prompt_1 = "What is the weather like in London right now?"
print(f"\n--- User Prompt 1: {user_prompt_1} ---")
# The .invoke() method runs the agent
response_1 = agent.invoke(user_prompt_1)
# The response object contains the final answer and any tool calls made
print(f"\nFinal Agent Response:")
print(response_1.answer)
print("\n--- Tool History ---")
# If the agent used a tool, the details will be in the response's history
for call in response_1.history:
if call.tool_call:
# The agent decided to call this function
print(f"-> Agent called Tool: {call.tool_call.function_name}")
print(f"-> Arguments: {call.tool_call.args}")
# This is the actual output from the Python function
print(f"-> Tool Output: {call.tool_result.content}")
# Expected output: The agent calls get_city_weather('London'), gets the JSON,
# and then summarizes the result: "The weather in London is 12C and Cloudy."
user_prompt_2 = "Hello! What is your purpose?"
print(f"\n--- User Prompt 2: {user_prompt_2} ---")
response_2 = agent.invoke(user_prompt_2)
print(f"\nFinal Agent Response:")
print(response_2.answer)
print("\n--- Tool History ---")
# This time, the history will be empty or contain only the initial prompt
print("No tools were called for this request.")
# Expected output: The agent answers directly without using any tools,
# e.g., "I am an AI assistant designed to help you with dates and weather information."
The strands-agents/sdk-python library is essentially a high-level framework for orchestration.
| Feature | Software Engineering Benefit |
| Declarative Tooling | You define what a tool does (via its function signature and docstring), and the SDK/LLM handles when to call it. |
| State Management | Simplifies complex conversational flows and memory, allowing you to focus on business logic. |
| Model Abstraction | Easily swap out the underlying LLM (e.g., from GPT-4o to Claude) without rewriting your agent logic, fostering future-proofing. |
It allows you to transition from writing explicit, sequential programming (e.g., if-else blocks to check for user intent) to a declarative, intent-driven system where the LLM becomes the central routing engine.