AWS Strands Agents Team Releases Strands Harness: An Open-Source Agent Harness With 28% Lower Token Cost at Comparable Accuracy
Back to Tutorials
techTutorialbeginner

AWS Strands Agents Team Releases Strands Harness: An Open-Source Agent Harness With 28% Lower Token Cost at Comparable Accuracy

September 21, 20268 views4 min read

Learn how to set up and use the Strands Harness, an open-source agent framework from AWS that reduces token costs by 28% while maintaining accuracy. This beginner-friendly tutorial walks you through creating your first AI agent.

Introduction

In this tutorial, you'll learn how to use the Strands Harness, an open-source tool developed by AWS that helps developers build and deploy AI agents more efficiently. The Strands Harness reduces token costs by 28% while maintaining accuracy, making it ideal for cost-conscious developers. This tutorial will guide you through setting up the harness, creating a simple agent, and running it locally.

Prerequisites

  • Basic understanding of Python programming
  • Python 3.8 or higher installed on your machine
  • Git installed for cloning repositories
  • Basic knowledge of command-line tools

Step-by-Step Instructions

1. Setting Up Your Development Environment

1.1 Install Python and pip

First, ensure you have Python 3.8 or higher installed. You can check your version by running:

python --version

If you don't have Python installed, download it from python.org.

1.2 Create a Virtual Environment

Creating a virtual environment isolates your project dependencies. Run the following commands:

python -m venv strands_env
source strands_env/bin/activate  # On Windows: strands_env\Scripts\activate

This creates a new environment named 'strands_env' and activates it.

2. Installing Strands Harness

2.1 Clone the Repository

Next, clone the Strands Harness repository from GitHub:

git clone https://github.com/strands-agents/strands-harness.git
 cd strands-harness

This downloads the complete source code to your local machine.

2.2 Install Dependencies

Install the required packages using pip:

pip install -r requirements.txt

This command installs all dependencies listed in the requirements file, including the core Strands Harness components.

3. Creating Your First Agent

3.1 Understand the Agent Structure

The Strands Harness provides a standard agent structure. The main components include:

  • Agent configuration: Defines how the agent behaves
  • Task execution: How the agent performs tasks
  • Response handling: How the agent processes results

3.2 Create a Simple Agent File

Create a new Python file named simple_agent.py in the project root:

import os
from strands_harness import Agent

# Initialize the agent with default settings
agent = Agent()

def main():
    # Define a simple task
    task = "What is the capital of France?"
    
    # Execute the task
    result = agent.execute(task)
    
    # Print the result
    print(f"Result: {result}")

if __name__ == "__main__":
    main()

This code sets up a basic agent that answers a question about geography. The Agent class handles all the complexity behind the scenes.

4. Running Your Agent

4.1 Execute the Agent

Run your agent by executing:

python simple_agent.py

You should see the agent respond to your question about the capital of France. The harness automatically manages token usage and response handling.

4.2 Observe Token Efficiency

One of the key features of Strands Harness is its token efficiency. You can monitor the token usage by adding a debug statement:

print(f"Tokens used: {agent.get_token_count()}")

This shows how the harness optimizes token usage compared to traditional approaches.

5. Customizing Your Agent

5.1 Modify Agent Behavior

You can customize how your agent behaves by modifying the configuration:

from strands_harness import Agent

# Create an agent with custom settings
agent = Agent(
    model="gpt-4",
    max_tokens=100,
    temperature=0.5
)

# Define a more complex task
task = "Explain the benefits of using Strands Harness in AI development"
result = agent.execute(task)
print(f"Result: {result}")

Here, we're specifying a model, maximum tokens, and temperature to control the agent's behavior.

5.2 Add Custom Task Handling

To add custom task handling, extend the agent's capabilities:

from strands_harness import Agent

class CustomAgent(Agent):
    def process_task(self, task):
        # Add custom logic here
        return super().execute(task)

# Use the custom agent
custom_agent = CustomAgent()
task = "Summarize the benefits of open-source AI tools"
result = custom_agent.execute(task)
print(f"Custom result: {result}")

This approach allows you to extend the base agent with your own logic while maintaining the harness's efficiency.

6. Testing and Optimization

6.1 Test Different Configurations

Experiment with different configurations to see how they affect performance and token usage:

from strands_harness import Agent

# Test different models
models = ["gpt-3.5-turbo", "gpt-4"]
for model in models:
    agent = Agent(model=model)
    result = agent.execute("What is AI?")
    print(f"{model} result: {result}")
    print(f"Tokens used: {agent.get_token_count()}")

This code tests two different models to compare their efficiency.

6.2 Monitor Performance

Use the built-in monitoring tools to track your agent's performance:

agent = Agent()
result = agent.execute("What is machine learning?")
print(f"Execution time: {agent.get_execution_time()} seconds")
print(f"Token efficiency: {agent.get_token_efficiency()}")

These metrics help you understand how efficiently your agent is using resources.

Summary

In this tutorial, you've learned how to set up and use the Strands Harness for building AI agents. You created a simple agent, ran it locally, and explored customization options. The harness offers significant token cost savings while maintaining accuracy, making it an excellent choice for developers looking to optimize their AI applications. By following these steps, you've gained hands-on experience with a powerful tool that bridges the gap between prototype development and production deployment.

Source: MarkTechPost

Related Articles