Strands Agent Builder Documentation

repository·main·Indexed 19 days ago

https://github.com/strands-agents/agent-builder

A model-driven toolkit for building, testing, and extending AI agents and tools via a terminal interface. It integrates with Amazon Bedrock for model execution and Knowledge Bases for tool and configuration persistence. The toolkit includes the `strands` CLI for interactive agent development, tool creation, and support for multiple model providers including Bedrock and Ollama.

Tokens
4.9K
Snippets
17
Records
21
Agent score
65%

What's inside Strands Agent Builder

  1. Set up Amazon Bedrock Knowledge Base for Strands

    main

    Strands uses Amazon Bedrock Knowledge Bases to persist tools, configurations, and history.

    Console Setup

    1. Create Knowledge Base: In the Amazon Bedrock console, select CreateKnowledge base with vector store.
    2. Configure IAM: Use a new service role or an existing one with Bedrock permissions.
    3. Data Source: Select Custom for Strands Agent.
    4. Embeddings: Select a model (e.g., Amazon Titan Text Embeddings V2) and choose float32 or binary.
    5. Vector Store: Use Quick create (recommended) or a custom OpenSearch Serverless/Aurora/S3 setup.
    6. Sync: Once created, click Sync in the data source section.

    Get Your Knowledge Base ID

    • Console: Copy from the knowledge base details page.
    • CLI: Use aws bedrock-agent list-knowledge-bases.
    • Format: 10 characters (e.g., ABCDEFGHIJ).
  2. Quick Start with Strands CLI

    main

    The strands command provides several ways to interact with the agent builder from your terminal:

    • Interactive Mode: Launch a rich CLI for agent development.
    • Tool Creation: Pass a natural language description to create and test a tool immediately.
    • Agent Building: Pipe a specification file into strands to build a specialized agent.
    • Knowledge Base Integration: Use the --kb flag to load and extend existing tools from an Amazon Bedrock Knowledge Base.
    # Run interactive mode for agent development
    strands
    
    # Build a custom tool and use it immediately
    strands "Create a tool named sentiment_analyzer that analyzes text sentiment and test it with some examples"
    
    # Pipe content to build an agent based on specifications
    cat agent-spec.txt | strands "Build a specialized agent based on these specifications"
    
    # Use with knowledge base to extend existing tools
    strands --kb YOUR_KB_ID "Load my previous calculator tool and enhance it with scientific functions"
  3. Use and Configure Knowledge Base

    main

    You can interact with your Knowledge Base using the --kb flag or by setting the STRANDS_KNOWLEDGE_BASE_ID environment variable.

    Usage Examples:

    # Load and extend tools from your knowledge base
    strands --kb YOUR_KB_ID "Load my data_visualizer tool and add 3D plotting capabilities"
    
    # Set a default knowledge base via environment variable
    export STRANDS_KNOWLEDGE_BASE_ID="YOUR_KB_ID"
    strands "Find my most recent agent configuration and make it more efficient"
    # Load and extend tools from your knowledge base
    strands --kb YOUR_KB_ID "Load my data_visualizer tool and add 3D plotting capabilities"
    
    # Or set a default knowledge base via environment variable
    export STRANDS_KNOWLEDGE_BASE_ID="YOUR_KB_ID"
    strands "Find my most recent agent configuration and make it more efficient"
  4. Customize System Prompts

    main

    You can define the agent's persona and instructions using environment variables or a local file.

    • Environment Variable: Use STRANDS_SYSTEM_PROMPT.
    • Local File: Create a file named .prompt in your current directory.
    # Via environment variable
    export STRANDS_SYSTEM_PROMPT="You are a Python expert."
    
    # Or local file
    echo "You are a security expert." > .prompt
  5. Understand the CallbackHandler event lifecycle

    main

    The CallbackHandler manages the visual state of the agent's execution through several phases:

    1. Event Loop Initialization: When init_event_loop is true, a blue 'retrieving memories...' spinner is shown. When start_event_loop is true, it updates to 'thinking...'.
    2. Tool Execution: When current_tool_use is detected, a ToolSpinner is created. It tracks the size of the input string to show progress (e.g., 🛠️ web_search: 45 chars).
    3. Tool Completion: When a message with role: 'user' contains a toolResult, the handler calculates the duration of the tool execution and updates the spinner to a success (green) or failure (red) state.
    4. Cleanup: Once a tool result is processed, the handler clears the tool_histories and resets the current_spinner.
  6. Use a Knowledge Base with Strands

    main

    You can connect an agent to a knowledge base to enable retrieval-augmented generation (RAG).

    1. Identification: Provide the knowledge base ID via the --kb flag or set the STRANDS_KNOWLEDGE_BASE_ID environment variable.
    2. Retrieval: When a knowledge base ID is present, the CLI automatically calls agent.tool.retrieve using the user's input before processing the query.
    3. Persistence: If a knowledge base is active, the conversation (query and response) is automatically stored in the knowledge base using store_conversation_in_kb after each turn.
    # Using environment variable
    export STRANDS_KNOWLEDGE_BASE_ID="my-kb-123"
    strands "Search the docs for installation"
  7. Configure Model Settings and Tokens

    main

    Strands uses optimized defaults for Amazon Bedrock (Claude Sonnet 4). You can override these using environment variables.

    • STRANDS_MAX_TOKENS: Sets the maximum token output for responses (Default: 32767).
    • STRANDS_BUDGET_TOKENS: Sets the budget for agent thinking/reasoning (Default: 2048).
    # Maximum tokens for responses
    export STRANDS_MAX_TOKENS=32000
    
    # Budget for agent thinking/reasoning
    export STRANDS_BUDGET_TOKENS=1024
  8. Use a Custom Model Provider

    main

    You can switch from the default bedrock provider to ollama or a custom provider using the --model-provider and --model-config flags.

    Using Ollama

    strands --model-provider ollama --model-config '{"model_id": "<ID>"}'

    Using a Custom Python Provider

    To use a custom provider, create a Python module in $CWD/.models/ that exposes an instance function:

    1. Create .models/custom_model.py:
    from mymodels import CustomModel
    
    def instance(**config):
        return CustomModel(**config)
    1. Run strands with the provider name:
    strands --model-provider custom_model --model-config <JSON|FILE>
    # Example using Ollama
    strands --model-provider ollama --model-config '{"model_id": "<ID>"}'
  9. Integrated Tools Reference

    main

    Strands includes over 12 built-in tools for agentic workflows. Note that some tools like cron, python_repl, and shell are not available on Windows.

    • agent_graph: Manage graphs of agents
    • calculator: Mathematical operations
    • cron: Task scheduling
    • current_time: Date and time
    • editor: File editing (line edits, search, undo)
    • environment: Manage environment variables
    • generate_image: AI image generation (Amazon Bedrock)
    • http_request: API calls and web data
    • image_reader: Image analysis
    • journal: Task and log management
    • load_tool: Dynamic runtime tool loading
    • memory: Persistence via Amazon Bedrock Knowledge Bases
    • nova_reels: AI video generation (Amazon Bedrock)
    • python_repl: Python code execution
    • retrieve: Semantic retrieval (Amazon Bedrock Knowledge Bases)
    • shell: Shell command execution
    • slack: Slack integration
    • speak: Text-to-speech (macOS say or Amazon Polly)
    • stop: Force stop the agent loop
    • store_in_kb: Save content to knowledge bases
    • strand: Create nested agent instances
    • swarm: Coordinate multiple agents
    • think: Parallel agentic reasoning branches
    • use_aws: AWS service interaction
    • use_llm: Run a new event loop with custom prompts
    • workflow: Orchestrate sequenced workflows
  10. Configure Strands Agent Builder via environment variables

    main

    You can customize the behavior of the Agent Builder using several environment variables. These allow you to control the model used, token limits, thinking capabilities, caching strategies, and system prompts without modifying code or configuration files.

    | Environment Variable | Description | Default |
    |----------------------|-------------|---------|
    | STRANDS_MODEL_ID | Claude model ID to use for inference | us.anthropic.claude-sonnet-4-20250514-v1:0 |
    | STRANDS_MAX_TOKENS | Maximum tokens for agent responses | 32768 |
    | STRANDS_BUDGET_TOKENS | Token budget for agent thinking/reasoning | 2048 |
    | STRANDS_THINKING_TYPE | Type of thinking capability | enabled |
    | STRANDS_ANTHROPIC_BETA | Anthropic beta features (comma-separated) | interleaved-thinking-2025-05-14 |
    | STRANDS_CACHE_TOOLS | Tool caching strategy | default |
    | STRANDS_CACHE_PROMPT | Prompt caching strategy | default |
    | STRANDS_SYSTEM_PROMPT | Custom system prompt (overrides .prompt file) | None |
    | STRANDS_KNOWLEDGE_BASE_ID | Default Knowledge Base ID | None |
    | STRANDS_TOOL_CONSOLE_MODE | Enable rich console UI | enabled |
    | BYPASS_TOOL_CONSENT | Skip tool confirmation prompts | false |