It shows how to use strands agent.
Strands Agent
Strands agent는 AI agent 구축 및 실행을 위해 설계된 오픈소스 SDK입니다. 계획(planning), 사고 연결(chaining thoughts), 도구 호출, Reflection과 같은 agent 기능을 쉽게 활용할 수 있습니다. 이를 통해 LLM model과 tool을 연결하며, 모델의 추론 능력을 이용하여 도구를 계획하고 실행합니다. 현재 Amazon Bedrock, Anthropic, Meta의 모델을 지원하며, Accenture, Anthropic, Meta와 같은 기업들이 참여하고 있습니다.
Strands agent는 아래와 같은 Agent Loop가지고 있으므로, 적절한 tool을 선택하여 실행하고, reasoning을 통해 반복적으로 필요한 동작을 수행합니다.
Tool들을 아래와 같이 병렬로 처리할 수 있습니다.
agent = Agent(
max_parallel_tools=4
)
Strands Agent 활용 방법
Model 설정
모델 설정과 관련된 paramter는 아래와 같습니다.
- max_tokens: Maximum number of tokens to generate in the response
- model_id: The Bedrock model ID (e.g., "us.anthropic.claude-3-7-sonnet-20250219-v1:0")
- stop_sequences: List of sequences that will stop generation when encountered
- temperature: Controls randomness in generation (higher = more random)
- top_p: Controls diversity via nucleus sampling (alternative to temperature)
- cache_prompt: Cache point type for the system prompt
- cache_tools: Cache point type for tools
- guardrail_id: ID of the guardrail to apply
- guardrail_trace: Guardrail trace mode. Defaults to enabled.
- guardrail_version: Version of the guardrail to apply
- guardrail_stream_processing_mode: The guardrail processing mode
- guardrail_redact_input: Flag to redact input if a guardrail is triggered. Defaults to True.
- guardrail_redact_input_message: If a Bedrock Input guardrail triggers, replace the input with this message.
- guardrail_redact_output: Flag to redact output if guardrail is triggered. Defaults to False.
- guardrail_redact_output_message: If a Bedrock Output guardrail triggers, replace output with this message.
Reasoning mode와 standard mode의 설정이 다르므로 아래와 같이 설정합니다.
def get_model():
profile = models[0]
if profile['model_type'] == 'nova':
STOP_SEQUENCE = '"\n\n<thinking>", "\n<thinking>", " <thinking>"'
elif profile['model_type'] == 'claude':
STOP_SEQUENCE = "\n\nHuman:"
if model_type == 'claude':
maxOutputTokens = 4096 # 4k
else:
maxOutputTokens = 5120 # 5k
maxReasoningOutputTokens=64000
thinking_budget = min(maxOutputTokens, maxReasoningOutputTokens-1000)
if reasoning_mode=='Enable':
model = BedrockModel(
boto_client_config=Config(
read_timeout=900,
connect_timeout=900,
retries=dict(max_attempts=3, mode="adaptive"),
),
model_id=model_id,
max_tokens=64000,
stop_sequences = [STOP_SEQUENCE],
temperature = 1,
additional_request_fields={
"thinking": {
"type": "enabled",
"budget_tokens": thinking_budget,
}
},
)
else:
model = BedrockModel(
boto_client_config=Config(
read_timeout=900,
connect_timeout=900,
retries=dict(max_attempts=3, mode="adaptive"),
),
model_id=model_id,
max_tokens=maxOutputTokens,
stop_sequences = [STOP_SEQUENCE],
temperature = 0.1,
top_p = 0.9,
additional_request_fields={
"thinking": {
"type": "disabled"
}
}
)
return model
Agent의 실행
아래와 같이 system prompt, model, tool 정보를 가지고 agent를 생성합니다.
def create_agent():
system = (
"당신의 이름은 서연이고, 질문에 대해 친절하게 답변하는 사려깊은 인공지능 도우미입니다."
"상황에 맞는 구체적인 세부 정보를 충분히 제공합니다."
"모르는 질문을 받으면 솔직히 모른다고 말합니다."
)
model = get_model()
agent = Agent(
model=model,
system_prompt=system,
tools=[
calculator,
current_time,
use_aws
],
)
return agent
Agent는 stream으로 결과를 주므로, 아래와 같이 event에서 "data"만을 추출한 후에 full_response로 저장한 후에 markdown으로 표시합니다.
def run_strands_agent(question, history_mode, st):
agent = create_agent()
message_placeholder = st.empty()
full_response = ""
async def process_streaming_response():
nonlocal full_response
agent_stream = agent.stream_async(question)
async for event in agent_stream:
if "data" in event:
full_response += event["data"]
message_placeholder.markdown(full_response)
asyncio.run(process_streaming_response())
return full_response
대화 이력의 활용
대화 내용을 이용해 대화를 이어나가고자 할 경우에 아래와 같이 SlidingWindowConversationManager을 이용해서 window_size만큼 이전 대화를 가져와 활용할 수 있습니다. 상세한 코드는 chat.py을 참조합니다.
from strands.agent.conversation_manager import SlidingWindowConversationManager
conversation_manager = SlidingWindowConversationManager(
window_size=10,
)
agent = Agent(
model=model,
system_prompt=system,
tools=[
calculator,
current_time,
use_aws
],
conversation_manager=conversation_manager
)
MCP 활용
아래와 같이 MCPClient로 stdio_mcp_client을 지정한 후에 list_tools_sync을 이용해 tool 정보를 추출합니다. MCP tool은 strands tool과 함께 아래처럼 사용할 수 있습니다.
from strands.tools.mcp import MCPClient
from strands_tools import calculator, current_time, use_aws
stdio_mcp_client = MCPClient(lambda: stdio_client(
StdioServerParameters(command="uvx", args=["awslabs.aws-documentation-mcp-server@latest"])
))
with stdio_mcp_client as client:
aws_documentation_tools = client.list_tools_sync()
logger.info(f"aws_documentation_tools: {aws_documentation_tools}")
tools=[
calculator,
current_time,
use_aws
]
tools.extend(aws_documentation_tools)
if history_mode == "Enable":
logger.info("history_mode: Enable")
agent = Agent(
model=model,
system_prompt=system,
tools=tools,
conversation_manager=conversation_manager
)
else:
logger.info("history_mode: Disable")
agent = Agent(
model=model,
system_prompt=system,
tools=tools
)
또한, wikipedia 검색을 위한 MCP server의 예는 아래와 같습니다. 상세한 코드는 mcp_server_wikipedia.py을 참조합니다.
from mcp.server.fastmcp import FastMCP
import wikipedia
import logging
import sys
logging.basicConfig(
level=logging.INFO, # Default to INFO level
format='%(filename)s:%(lineno)d | %(message)s',
handlers=[
logging.StreamHandler(sys.stderr)
]
)
logger = logging.getLogger("rag")
mcp = FastMCP(
"Wikipedia",
dependencies=["wikipedia"],
)
@mcp.tool()
def search(query: str):
logger.info(f"Searching Wikipedia for: {query}")
return wikipedia.search(query)
@mcp.tool()
def summary(query: str):
return wikipedia.summary(query)
@mcp.tool()
def page(query: str):
return wikipedia.page(query)
@mcp.tool()
def random():
return wikipedia.random()
@mcp.tool()
def set_lang(lang: str):
wikipedia.set_lang(lang)
return f"Language set to {lang}"
if __name__ == "__main__":
mcp.run()
Stands Agent 활용하기
Repository를 clone 합니다.
git clone https://github.com/kyopark2014/strands-agent/
필요한 라이브러리를 설치합니다. 여기서 필요한 라이브러리는 strands-agents, strands-agents-tools 입니다.
cd strands-agent && pip install -r requirements.txt
만약 streamlit이 설치되어 있지 않다면 streamlit을 참조하여 설치합니다. 이후 아래와 같이 실행합니다.
streamlit run application/app.py
실행하면 아래와 같은 화면이 보여집니다. Agent를 선택하면 Sprends agent를 실행하고 동작을 확인할 수 있습니다. Agent(Chat)을 선택하면 대화 이력을 포함한 동작을 확인합니다. 모델 버튼을 클릭해서 적절한 모델을 선택할 수 있습니다.
실행 결과
"us-west-2의 AWS bucket 리스트는?"와 같이 입력하면, aws cli를 통해 필요한 operation을 수행하고 얻어진 결과를 아래와 같이 보여줍니다.
MCP로 wikipedia를 설정하고 "strand에 대해 설명해주세요."라고 질문하면 wikipedia의 search tool을 이용하여 아래와 같은 결과를 얻습니다.