Back to snippets

strands_agents_custom_tool_decorator_quickstart.py

python

This quickstart demonstrates how to define a custom tool using the

Agent Votes
0
1
0% positive
strands_agents_custom_tool_decorator_quickstart.py
1import os
2from strands_agents_tools import tool, StrandsAgent
3
4# Define a custom tool using the @tool decorator
5@tool
6def get_weather(location: str) -> str:
7    """Returns the current weather for a given location."""
8    # In a real scenario, you would call a weather API here
9    return f"The weather in {location} is sunny and 25°C."
10
11# Initialize the Strands Agent with your API key
12# Ensure STRANDS_API_KEY is set in your environment variables
13agent = StrandsAgent(api_key=os.getenv("STRANDS_API_KEY"))
14
15# Register the tool with the agent
16agent.register_tool(get_weather)
17
18# Run the agent with a prompt that triggers the tool
19response = agent.run("What is the weather like in San Francisco?")
20
21print(response)