Building Adaptive AI Agents with the Strands Python SDK

Modern AI development is shifting toward creating intelligent agents capable of managing complex workflows and maintaining context. Traditional frameworks often rely on rigid, predefined chains, which can limit adaptability. The Strands SDK introduces a model-driven approach, allowing the underlying Large Language Model (LLM) to handle the planning and reasoning dynamically, rather than forcing the developer to hardcode the logic.

Core Components

A Strands Agent is built using three primary elements:

  1. The Model (The Brain): Handles reasoning and decision-making. The framework is agnostic regarding the model provider, supporting:

    • Amazon Bedrock: Access to various foundation models.
    • Anthropic API: Direct integration with Claude models.
    • Llama API: Support for Meta's Llama series.
    • Ollama: Local execution of open-source models for development.
    • OpenAI: Accessible via LiteLLM.
    • Custom Providers: Flexibility to integrate proprietary model services.
  2. The Tools (The Hands): Functions that allow the agent to interact with external systems. Any Python function can be converted into a tool using the @tool decorator. Additionally, it supports tools hosted on a Model Context Protocol (MCP) server.

  3. The Prompt (The Instructions): Natural language directives defining the task. This typically includes a system prompt (persona/behavior) and a user prompt (specific task).

The Agentic Loop

The framework operates on an iterative "Agentic Loop" rather than a fixed script:

  1. Context Analysis: The SDK aggregates the user prompt, conversation history, and tool descriptions.
  2. LLM Invocation: This context is sent to the configured LLM to plan the next step.
  3. Decision Making: The model decides whether to respond to the user, ask a question, or execute a tool.
  4. Execution: If a tool is chosen, the SDK executes the function and captures the output or eror.
  5. Iteration: The result is fed back into the loop until the model determines the task is complete.

Practical Implementation

The following example demonstrates setting up an agent with custom logic and built-in utilities.

Environment Setup

Ensure you have Python 3.10+ installed. Set up a virtual environment and install the necessary packages:

python -m venv.venv
source.venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install strands-agents strands-agents-tools strands-agents-builder

Agent Code

This script defines a custom functon to generate random numbers and combines it with standard tools for calculation and code execution.

from strands import Agent, tool
import random
from strands_tools import calculator, current_time, python_repl

# Define a custom capability using the decorator
@tool
def get_random_number(faces: int):
    """
    Simulates rolling a die with a specific number of faces.
    
    Args:
        faces: The maximum number (sides) of the die.
        
    Returns:
        A random integer between 1 and the number of faces.
    """
    return random.randint(1, faces)

# Initialize the agent with a selection of tools
assistant = Agent(
    tools=[calculator, current_time, python_repl, get_random_number]
)

# Define a complex query requiring multiple steps
user_query = """
Please perform the following actions:
1. Tell me the current time.
2. Divide 3111696 by 74088.
3. Roll a 20-sided die.
4. Generate a Python script that accomplishes the above tasks and verify it using the REPL tool before outputting it.
"""

# Execute the task
assistant(user_query)

The @tool decorator allows the LLM to understand the function's purpose via the docstring and type hints. When executed, the agent autonomously decides the order of operations, calls the tools, and generates the final script.

Model Configuration

While Strands defaults to Amazon Bedrock (Claude 3.7 Sonnet), switching models is straightforward.

Using Anthropic API:

from strands.models import AnthropicModel

claude_config = AnthropicModel(
    client_args={"api_key": "your-key-here"},
    model_id="claude-3-opus-20240229",
    params={"temperature": 0.5}
)
# agent = Agent(model=claude_config)

Using Ollama (Local):

from strands.models import OllamaModel

local_model = OllamaModel(
    host="http://localhost:11434",
    model_id="llama3"
)
# agent = Agent(model=local_model)

Debugging

To inspect the agent's decision-making process, enable debug logging:

import logging
from strands import Agent

# Configure logging to see the loop steps
logging.basicConfig(format='%(levelname)s | %(name)s | %(message)s')
logging.getLogger("strands").setLevel(logging.DEBUG)

bot = Agent()
bot("What is the weather?")  # Check console for detailed loop logs

Tags: AI Agents Strands SDK python LLM Amazon Bedrock

Posted on Tue, 18 Aug 2026 16:46:56 +0000 by efficacious