Skip to content

Models

Models in Bedrock Swarm provide the foundation for agent intelligence through integration with Amazon Bedrock's language models. This document explains the core concepts and architecture of the model system.

Model Architecture

The model system follows a layered architecture:

graph TD
    A[Model Factory] --> B[Base Model]
    B --> C[Claude Model]
    B --> D[Titan Model]
    A --> E[Model Registry]
    E --> F[Model Configurations]

Core Components

  1. Model Factory
  2. Central point for model creation
  3. Handles model registration
  4. Manages configurations
  5. Provides model validation

  6. Base Model

  7. Abstract interface for all models
  8. Defines common operations
  9. Handles token management
  10. Provides error handling

  11. Model Implementations

  12. Claude Model (Anthropic)
  13. Titan Model (Amazon)
  14. Custom model implementations

Model Registry

The model registry maintains configurations for supported models:

BEDROCK_MODEL_REGISTRY = {
    "us.anthropic.claude-3-5-sonnet": {
        "20241022-v2:0": {
            "class": ClaudeModel,
            "config": {
                "max_tokens": 200000,
                "default_tokens": 4096,
            },
        }
    },
    "amazon.titan-text-express": {
        "v1": {
            "class": TitanModel,
            "config": {
                "max_tokens": 8000,
                "default_tokens": 2048,
            },
        }
    }
}

Model Operations

Models support these core operations:

  1. Request Formatting
  2. System prompt integration
  3. Message formatting
  4. Parameter validation
  5. Token counting

  6. Response Processing

  7. Content extraction
  8. Stream handling
  9. Error processing
  10. Token tracking

  11. Error Handling

  12. Token limit validation
  13. API error handling
  14. Response parsing
  15. Retry logic

Model Selection

Choose models based on these factors:

  1. Capabilities
  2. Context window size
  3. Response quality
  4. Specialized features
  5. Performance characteristics

  6. Resource Requirements

  7. Token usage
  8. Response time
  9. Cost considerations
  10. Memory usage

  11. Integration Needs

  12. API compatibility
  13. Tool support
  14. Stream processing
  15. Error handling

Best Practices

  1. Model Configuration

    # Use model factory for creation
    model = ModelFactory.create_model(
        "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
    )
    
    # Configure model parameters
    model.set_config({
        "max_tokens": 100000,
        "temperature": 0.7
    })
    

  2. Token Management

    # Validate token count
    token_count = model.validate_token_count(
        max_tokens=5000
    )
    
    # Track token usage
    response = model.invoke(message)
    used_tokens = model.last_token_count
    

  3. Error Handling

    try:
        response = model.invoke(message)
    except ModelInvokeError as e:
        # Handle API errors
        logger.error(f"Model error: {e}")
    except TokenLimitError as e:
        # Handle token limit errors
        logger.error(f"Token limit exceeded: {e}")
    

Model Integration

Integrate models with agents:

from bedrock_swarm.agents import BedrockAgent
from bedrock_swarm.models.factory import ModelFactory

# Create agent with model
agent = BedrockAgent(
    name="calculator",
    model_id="us.anthropic.claude-3-5-sonnet-20241022-v2:0",
    tools=[CalculatorTool()],
    system_prompt="You are a calculation specialist."
)

# Model is automatically created and configured
response = agent.process_message("Calculate 2 + 2")

See Also