@ -17,13 +17,10 @@ Key Features:
import os
import random
import json
import time
import hashlib
from typing import Dict , List , Optional , Union, Any, Set
from typing import Dict , List , Optional , Any
from dataclasses import dataclass , field
from enum import Enum
from datetime import datetime
from functools import lru_cache
from swarms import Agent
from swarms . structs . multi_agent_exec import run_agents_concurrently
@ -31,10 +28,6 @@ from swarms.structs.board_of_directors_swarm import (
BoardOfDirectorsSwarm ,
BoardMember ,
BoardMemberRole ,
BoardDecisionType ,
BoardSpec ,
BoardOrder ,
BoardDecision ,
enable_board_feature ,
)
from swarms . utils . loguru_logger import initialize_logger
@ -136,7 +129,9 @@ class CostTracker:
def add_tokens ( self , tokens : int ) :
""" Add tokens used and calculate cost. """
self . total_tokens_used + = tokens
self . total_cost_estimate = ( self . total_tokens_used / 1_000_000 ) * self . token_cost_per_1m
self . total_cost_estimate = (
self . total_tokens_used / 1_000_000
) * self . token_cost_per_1m
self . requests_made + = 1
def add_cache_hit ( self ) :
@ -154,8 +149,11 @@ class CostTracker:
" total_cost " : self . total_cost_estimate ,
" requests_made " : self . requests_made ,
" cache_hits " : self . cache_hits ,
" cache_hit_rate " : self . cache_hits / max ( 1 , self . requests_made + self . cache_hits ) ,
" budget_remaining " : max ( 0 , self . budget_limit - self . total_cost_estimate )
" cache_hit_rate " : self . cache_hits
/ max ( 1 , self . requests_made + self . cache_hits ) ,
" budget_remaining " : max (
0 , self . budget_limit - self . total_cost_estimate
) ,
}
@ -195,7 +193,9 @@ class MassAgentTemplate:
"""
self . data_source = data_source
self . agent_count = agent_count
self . enable_hierarchical_organization = enable_hierarchical_organization
self . enable_hierarchical_organization = (
enable_hierarchical_organization
)
self . enable_group_swarms = enable_group_swarms
self . enable_lazy_loading = enable_lazy_loading
self . enable_caching = enable_caching
@ -220,9 +220,15 @@ class MassAgentTemplate:
self . _organize_agents ( )
if self . verbose :
logger . info ( f " Mass Agent Template initialized with { len ( self . agents ) } agent profiles " )
logger . info ( f " Lazy loading: { self . enable_lazy_loading } , Caching: { self . enable_caching } " )
logger . info ( f " Budget limit: $ { budget_limit } , Batch size: { batch_size } " )
logger . info (
f " Mass Agent Template initialized with { len ( self . agents ) } agent profiles "
)
logger . info (
f " Lazy loading: { self . enable_lazy_loading } , Caching: { self . enable_caching } "
)
logger . info (
f " Budget limit: $ { budget_limit } , Batch size: { batch_size } "
)
def _load_agent_profiles ( self ) - > List [ Dict [ str , Any ] ] :
"""
@ -238,15 +244,20 @@ class MassAgentTemplate:
if self . data_source and os . path . exists ( self . data_source ) :
# Load from file - customize based on your data format
try :
if self . data_source . endswith ( ' .json ' ) :
with open ( self . data_source , ' r ' , encoding = ' utf-8 ' ) as f :
if self . data_source . endswith ( " .json " ) :
with open (
self . data_source , " r " , encoding = " utf-8 "
) as f :
agent_data = json . load ( f )
elif self . data_source . endswith ( ' .csv ' ) :
elif self . data_source . endswith ( " .csv " ) :
import pandas as pd
df = pd . read_csv ( self . data_source )
agent_data = df . to_dict ( ' records ' )
agent_data = df . to_dict ( " records " )
else :
logger . warning ( f " Unsupported data format: { self . data_source } " )
logger . warning (
f " Unsupported data format: { self . data_source } "
)
except Exception as e :
logger . error ( f " Error loading agent data: { e } " )
@ -265,7 +276,7 @@ class MassAgentTemplate:
skills = data [ " skills " ] ,
experience_level = data [ " experience_level " ] ,
agent = None , # Will be created on demand
is_loaded = False
is_loaded = False ,
)
self . agents [ data [ " name " ] ] = agent_profile
@ -300,7 +311,9 @@ class MassAgentTemplate:
return profile . agent
def _load_agents_batch ( self , agent_names : List [ str ] ) - > List [ Agent ] :
def _load_agents_batch (
self , agent_names : List [ str ]
) - > List [ Agent ] :
"""
Load multiple agents in a batch .
@ -319,7 +332,9 @@ class MassAgentTemplate:
return loaded_agents
def _get_cache_key ( self , task : str , agent_names : List [ str ] ) - > str :
def _get_cache_key (
self , task : str , agent_names : List [ str ]
) - > str :
"""
Generate a cache key for a task and agent combination .
@ -367,7 +382,9 @@ class MassAgentTemplate:
if self . enable_caching :
self . response_cache [ cache_key ] = response
if self . verbose :
logger . info ( f " Cached response for key: { cache_key [ : 20 ] } ... " )
logger . info (
f " Cached response for key: { cache_key [ : 20 ] } ... "
)
def _generate_synthetic_data ( self ) - > List [ Dict [ str , Any ] ] :
"""
@ -384,47 +401,107 @@ class MassAgentTemplate:
" name " : " Alex_Developer " ,
" role " : AgentRole . SPECIALIST ,
" category " : AgentCategory . TECHNICAL ,
" specialization " : [ " Python " , " Machine Learning " , " API Development " ] ,
" personality_traits " : [ " analytical " , " detail-oriented " , " problem-solver " ] ,
" skills " : [ " Python " , " TensorFlow " , " FastAPI " , " Docker " ] ,
" experience_level " : " senior "
" specialization " : [
" Python " ,
" Machine Learning " ,
" API Development " ,
] ,
" personality_traits " : [
" analytical " ,
" detail-oriented " ,
" problem-solver " ,
] ,
" skills " : [
" Python " ,
" TensorFlow " ,
" FastAPI " ,
" Docker " ,
] ,
" experience_level " : " senior " ,
} ,
{
" name " : " Sarah_Designer " ,
" role " : AgentRole . CREATOR ,
" category " : AgentCategory . CREATIVE ,
" specialization " : [ " UI/UX Design " , " Visual Design " , " Brand Identity " ] ,
" personality_traits " : [ " creative " , " user-focused " , " aesthetic " ] ,
" skills " : [ " Figma " , " Adobe Creative Suite " , " User Research " , " Prototyping " ] ,
" experience_level " : " senior "
" specialization " : [
" UI/UX Design " ,
" Visual Design " ,
" Brand Identity " ,
] ,
" personality_traits " : [
" creative " ,
" user-focused " ,
" aesthetic " ,
] ,
" skills " : [
" Figma " ,
" Adobe Creative Suite " ,
" User Research " ,
" Prototyping " ,
] ,
" experience_level " : " senior " ,
} ,
{
" name " : " Mike_Analyst " ,
" role " : AgentRole . ANALYST ,
" category " : AgentCategory . ANALYTICAL ,
" specialization " : [ " Data Analysis " , " Business Intelligence " , " Market Research " ] ,
" personality_traits " : [ " data-driven " , " curious " , " insightful " ] ,
" specialization " : [
" Data Analysis " ,
" Business Intelligence " ,
" Market Research " ,
] ,
" personality_traits " : [
" data-driven " ,
" curious " ,
" insightful " ,
] ,
" skills " : [ " SQL " , " Python " , " Tableau " , " Statistics " ] ,
" experience_level " : " expert "
" experience_level " : " expert " ,
} ,
{
" name " : " Lisa_Manager " ,
" role " : AgentRole . MANAGER ,
" category " : AgentCategory . STRATEGIC ,
" specialization " : [ " Project Management " , " Team Leadership " , " Strategic Planning " ] ,
" personality_traits " : [ " organized " , " leadership " , " strategic " ] ,
" skills " : [ " Agile " , " Scrum " , " Risk Management " , " Stakeholder Communication " ] ,
" experience_level " : " senior "
" specialization " : [
" Project Management " ,
" Team Leadership " ,
" Strategic Planning " ,
] ,
" personality_traits " : [
" organized " ,
" leadership " ,
" strategic " ,
] ,
" skills " : [
" Agile " ,
" Scrum " ,
" Risk Management " ,
" Stakeholder Communication " ,
] ,
" experience_level " : " senior " ,
} ,
{
" name " : " Tom_Coordinator " ,
" role " : AgentRole . COORDINATOR ,
" category " : AgentCategory . OPERATIONAL ,
" specialization " : [ " Process Optimization " , " Workflow Management " , " Resource Allocation " ] ,
" personality_traits " : [ " efficient " , " coordinated " , " systematic " ] ,
" skills " : [ " Process Mapping " , " Automation " , " Resource Planning " , " Quality Assurance " ] ,
" experience_level " : " senior "
}
" specialization " : [
" Process Optimization " ,
" Workflow Management " ,
" Resource Allocation " ,
] ,
" personality_traits " : [
" efficient " ,
" coordinated " ,
" systematic " ,
] ,
" skills " : [
" Process Mapping " ,
" Automation " ,
" Resource Planning " ,
" Quality Assurance " ,
] ,
" experience_level " : " senior " ,
} ,
]
# Generate the specified number of agents
@ -437,14 +514,18 @@ class MassAgentTemplate:
" role " : template [ " role " ] ,
" category " : template [ " category " ] ,
" specialization " : template [ " specialization " ] . copy ( ) ,
" personality_traits " : template [ " personality_traits " ] . copy ( ) ,
" personality_traits " : template [
" personality_traits "
] . copy ( ) ,
" skills " : template [ " skills " ] . copy ( ) ,
" experience_level " : template [ " experience_level " ]
" experience_level " : template [ " experience_level " ] ,
}
# Add some randomization for variety
if random . random ( ) < 0.3 :
agent_data [ " experience_level " ] = random . choice ( [ " junior " , " senior " , " expert " ] )
agent_data [ " experience_level " ] = random . choice (
[ " junior " , " senior " , " expert " ]
)
synthetic_data . append ( agent_data )
@ -470,7 +551,9 @@ class MassAgentTemplate:
verbose = self . verbose ,
)
def _generate_agent_system_prompt ( self , profile : AgentProfile ) - > str :
def _generate_agent_system_prompt (
self , profile : AgentProfile
) - > str :
"""
Generate a comprehensive system prompt for an agent .
@ -526,58 +609,54 @@ Remember: You are part of a large multi-agent system. Your unique combination of
- Report progress and any issues encountered
- Maintain quality standards in all work
- Collaborate with team members as needed """ ,
AgentRole . MANAGER : """
- Oversee team activities and coordinate efforts
- Set priorities and allocate resources
- Monitor progress and ensure deadlines are met
- Provide guidance and support to team members
- Make strategic decisions for the team """ ,
AgentRole . SPECIALIST : """
- Provide expert knowledge in specific domains
- Solve complex technical problems
- Mentor other agents in your area of expertise
- Stay updated on latest developments in your field
- Contribute specialized insights to projects """ ,
AgentRole . COORDINATOR : """
- Facilitate communication between different groups
- Ensure smooth workflow and process optimization
- Manage dependencies and resource allocation
- Track project timelines and milestones
- Resolve conflicts and bottlenecks """ ,
AgentRole . ANALYST : """
- Analyze data and extract meaningful insights
- Identify patterns and trends
- Provide evidence - based recommendations
- Create reports and visualizations
- Support decision - making with data """ ,
AgentRole . CREATOR : """
- Generate innovative ideas and solutions
- Design and develop new content or products
- Think creatively and outside the box
- Prototype and iterate on concepts
- Inspire and motivate other team members """ ,
AgentRole . VALIDATOR : """
- Review and validate work quality
- Ensure compliance with standards and requirements
- Provide constructive feedback
- Identify potential issues and risks
- Maintain quality assurance processes """ ,
AgentRole . EXECUTOR : """
- Implement plans and strategies
- Execute tasks with precision and efficiency
- Adapt to changing circumstances
- Ensure successful completion of objectives
- Maintain focus on results and outcomes """
- Maintain focus on results and outcomes """ ,
}
return responsibilities . get ( role , " Execute tasks according to your role and expertise. " )
return responsibilities . get (
role ,
" Execute tasks according to your role and expertise. " ,
)
def _organize_agents ( self ) :
""" Organize agents into groups and categories. """
@ -601,13 +680,15 @@ Remember: You are part of a large multi-agent system. Your unique combination of
category = category ,
agents = agent_names ,
leader = leader ,
total_agents = len ( agent_names )
total_agents = len ( agent_names ) ,
)
self . groups [ group_name ] = group
if self . verbose :
logger . info ( f " Organized agents into { len ( self . groups ) } groups " )
logger . info (
f " Organized agents into { len ( self . groups ) } groups "
)
def _create_group_swarms ( self ) :
""" Create Board of Directors swarms for each group. """
@ -623,28 +704,41 @@ Remember: You are part of a large multi-agent system. Your unique combination of
if group . leader and group . leader in self . agents :
leader_profile = self . agents [ group . leader ]
if leader_profile . agent :
board_members . append ( BoardMember (
agent = leader_profile . agent ,
role = BoardMemberRole . CHAIRMAN ,
voting_weight = 1.0 ,
expertise_areas = leader_profile . specialization
) )
board_members . append (
BoardMember (
agent = leader_profile . agent ,
role = BoardMemberRole . CHAIRMAN ,
voting_weight = 1.0 ,
expertise_areas = leader_profile . specialization ,
)
)
# Add other agents as board members
for agent_name in group . agents [ : 5 ] : # Limit to 5 board members
if agent_name != group . leader and agent_name in self . agents :
for agent_name in group . agents [
: 5
] : # Limit to 5 board members
if (
agent_name != group . leader
and agent_name in self . agents
) :
profile = self . agents [ agent_name ]
if profile . agent :
board_members . append ( BoardMember (
agent = profile . agent ,
role = BoardMemberRole . EXECUTIVE_DIRECTOR ,
voting_weight = 0.8 ,
expertise_areas = profile . specialization
) )
board_members . append (
BoardMember (
agent = profile . agent ,
role = BoardMemberRole . EXECUTIVE_DIRECTOR ,
voting_weight = 0.8 ,
expertise_areas = profile . specialization ,
)
)
# Create Board of Directors swarm
if board_members :
agents = [ member . agent for member in board_members if member . agent is not None ]
agents = [
member . agent
for member in board_members
if member . agent is not None
]
group . group_swarm = BoardOfDirectorsSwarm (
name = group_name ,
@ -655,11 +749,13 @@ Remember: You are part of a large multi-agent system. Your unique combination of
verbose = self . verbose ,
decision_threshold = 0.6 ,
enable_voting = True ,
enable_consensus = True
enable_consensus = True ,
)
if self . verbose :
logger . info ( f " Created { len ( [ g for g in self . groups . values ( ) if g . group_swarm ] ) } group swarms " )
logger . info (
f " Created { len ( [ g for g in self . groups . values ( ) if g . group_swarm ] ) } group swarms "
)
def get_agent ( self , agent_name : str ) - > Optional [ AgentProfile ] :
"""
@ -685,7 +781,9 @@ Remember: You are part of a large multi-agent system. Your unique combination of
"""
return self . groups . get ( group_name )
def get_agents_by_category ( self , category : AgentCategory ) - > List [ str ] :
def get_agents_by_category (
self , category : AgentCategory
) - > List [ str ] :
"""
Get all agents in a specific category .
@ -707,9 +805,15 @@ Remember: You are part of a large multi-agent system. Your unique combination of
Returns :
List [ str ] : List of agent names with the role
"""
return [ name for name , profile in self . agents . items ( ) if profile . role == role ]
return [
name
for name , profile in self . agents . items ( )
if profile . role == role
]
def run_mass_task ( self , task : str , agent_count : int = 10 ) - > Dict [ str , Any ] :
def run_mass_task (
self , task : str , agent_count : int = 10
) - > Dict [ str , Any ] :
"""
Run a task with multiple agents working in parallel with cost optimization .
@ -722,10 +826,16 @@ Remember: You are part of a large multi-agent system. Your unique combination of
"""
# Check budget before starting
if not self . cost_tracker . check_budget ( ) :
return { " error " : " Budget exceeded " , " cost_stats " : self . cost_tracker . get_stats ( ) }
return {
" error " : " Budget exceeded " ,
" cost_stats " : self . cost_tracker . get_stats ( ) ,
}
# Select random agents
selected_agent_names = random . sample ( list ( self . agents . keys ( ) ) , min ( agent_count , len ( self . agents ) ) )
selected_agent_names = random . sample (
list ( self . agents . keys ( ) ) ,
min ( agent_count , len ( self . agents ) ) ,
)
# Check cache first
cache_key = self . _get_cache_key ( task , selected_agent_names )
@ -737,7 +847,7 @@ Remember: You are part of a large multi-agent system. Your unique combination of
" results " : cached_result ,
" total_agents " : len ( selected_agent_names ) ,
" cached " : True ,
" cost_stats " : self . cost_tracker . get_stats ( )
" cost_stats " : self . cost_tracker . get_stats ( ) ,
}
# Process in batches to control memory and cost
@ -745,12 +855,18 @@ Remember: You are part of a large multi-agent system. Your unique combination of
total_processed = 0
for i in range ( 0 , len ( selected_agent_names ) , self . batch_size ) :
batch_names = selected_agent_names [ i : i + self . batch_size ]
batch_names = selected_agent_names [
i : i + self . batch_size
]
# Check budget for this batch
if not self . cost_tracker . check_budget ( ) :
logger . warning ( f " Budget exceeded after processing { total_processed } agents " )
logger . warning ( f " Current cost: $ { self . cost_tracker . total_cost_estimate : .4f } , Budget: $ { self . cost_tracker . budget_limit : .2f } " )
logger . warning (
f " Budget exceeded after processing { total_processed } agents "
)
logger . warning (
f " Current cost: $ { self . cost_tracker . total_cost_estimate : .4f } , Budget: $ { self . cost_tracker . budget_limit : .2f } "
)
break
# Load agents for this batch
@ -761,20 +877,30 @@ Remember: You are part of a large multi-agent system. Your unique combination of
# Run batch
try :
batch_results = run_agents_concurrently ( batch_agents , task )
batch_results = run_agents_concurrently (
batch_agents , task
)
all_results . extend ( batch_results )
total_processed + = len ( batch_agents )
# Estimate tokens used (more realistic approximation)
# Include both input tokens (task) and output tokens (response)
task_tokens = len ( task . split ( ) ) * 1.3 # ~1.3 tokens per word
response_tokens = len ( batch_agents ) * 200 # ~200 tokens per response
task_tokens = (
len ( task . split ( ) ) * 1.3
) # ~1.3 tokens per word
response_tokens = (
len ( batch_agents ) * 200
) # ~200 tokens per response
total_tokens = int ( task_tokens + response_tokens )
self . cost_tracker . add_tokens ( total_tokens )
if self . verbose :
logger . info ( f " Processed batch { i / / self . batch_size + 1 } : { len ( batch_agents ) } agents " )
logger . info ( f " Current cost: $ { self . cost_tracker . total_cost_estimate : .4f } , Budget remaining: $ { self . cost_tracker . budget_limit - self . cost_tracker . total_cost_estimate : .2f } " )
logger . info (
f " Processed batch { i / / self . batch_size + 1 } : { len ( batch_agents ) } agents "
)
logger . info (
f " Current cost: $ { self . cost_tracker . total_cost_estimate : .4f } , Budget remaining: $ { self . cost_tracker . budget_limit - self . cost_tracker . total_cost_estimate : .2f } "
)
except Exception as e :
logger . error ( f " Error processing batch: { e } " )
@ -790,11 +916,15 @@ Remember: You are part of a large multi-agent system. Your unique combination of
" results " : all_results ,
" total_agents " : total_processed ,
" cached " : False ,
" cost_stats " : self . cost_tracker . get_stats ( )
" cost_stats " : self . cost_tracker . get_stats ( ) ,
}
def run_mass_task_optimized ( self , task : str , agent_count : int = 1000 ,
max_cost : float = 10.0 ) - > Dict [ str , Any ] :
def run_mass_task_optimized (
self ,
task : str ,
agent_count : int = 1000 ,
max_cost : float = 10.0 ,
) - > Dict [ str , Any ] :
"""
Run a task with cost - optimized mass execution for large - scale operations .
@ -816,7 +946,9 @@ Remember: You are part of a large multi-agent system. Your unique combination of
self . cost_tracker . budget_limit = max_cost
# Use smaller batches for better cost control
self . batch_size = min ( 25 , self . batch_size ) # Smaller batches for cost control
self . batch_size = min (
25 , self . batch_size
) # Smaller batches for cost control
result = self . run_mass_task ( task , agent_count )
@ -827,7 +959,9 @@ Remember: You are part of a large multi-agent system. Your unique combination of
self . cost_tracker . budget_limit = original_budget
self . batch_size = original_batch_size
def run_group_task ( self , group_name : str , task : str ) - > Dict [ str , Any ] :
def run_group_task (
self , group_name : str , task : str
) - > Dict [ str , Any ] :
"""
Run a task with a specific group using their Board of Directors swarm .
@ -840,7 +974,9 @@ Remember: You are part of a large multi-agent system. Your unique combination of
"""
group = self . groups . get ( group_name )
if not group or not group . group_swarm :
return { " error " : f " Group { group_name } not found or no swarm available " }
return {
" error " : f " Group { group_name } not found or no swarm available "
}
# Run task with group swarm
result = group . group_swarm . run ( task )
@ -849,7 +985,7 @@ Remember: You are part of a large multi-agent system. Your unique combination of
" group " : group_name ,
" task " : task ,
" result " : result ,
" agents_involved " : group . agents
" agents_involved " : group . agents ,
}
def get_system_stats ( self ) - > Dict [ str , Any ] :
@ -862,7 +998,9 @@ Remember: You are part of a large multi-agent system. Your unique combination of
stats = {
" total_agents " : len ( self . agents ) ,
" total_groups " : len ( self . groups ) ,
" loaded_agents " : len ( [ a for a in self . agents . values ( ) if a . is_loaded ] ) ,
" loaded_agents " : len (
[ a for a in self . agents . values ( ) if a . is_loaded ]
) ,
" categories " : { } ,
" roles " : { } ,
" experience_levels " : { } ,
@ -871,23 +1009,29 @@ Remember: You are part of a large multi-agent system. Your unique combination of
" lazy_loading " : self . enable_lazy_loading ,
" caching " : self . enable_caching ,
" batch_size " : self . batch_size ,
" budget_limit " : self . cost_tracker . budget_limit
}
" budget_limit " : self . cost_tracker . budget_limit ,
} ,
}
# Category breakdown
for category in AgentCategory :
stats [ " categories " ] [ category . value ] = len ( self . get_agents_by_category ( category ) )
stats [ " categories " ] [ category . value ] = len (
self . get_agents_by_category ( category )
)
# Role breakdown
for role in AgentRole :
stats [ " roles " ] [ role . value ] = len ( self . get_agents_by_role ( role ) )
stats [ " roles " ] [ role . value ] = len (
self . get_agents_by_role ( role )
)
# Experience level breakdown
experience_counts = { }
for profile in self . agents . values ( ) :
level = profile . experience_level
experience_counts [ level ] = experience_counts . get ( level , 0 ) + 1
experience_counts [ level ] = (
experience_counts . get ( level , 0 ) + 1
)
stats [ " experience_levels " ] = experience_counts
return stats
@ -909,89 +1053,113 @@ def demonstrate_mass_agent_template():
enable_caching = True ,
batch_size = 25 ,
budget_limit = 50.0 , # $50 budget limit
verbose = True
verbose = True ,
)
# Show system statistics
stats = template . get_system_stats ( )
print ( f " \n SYSTEM STATISTICS: " )
print ( " \n SYSTEM STATISTICS: " )
print ( f " Total Agents: { stats [ ' total_agents ' ] } " )
print ( f " Loaded Agents: { stats [ ' loaded_agents ' ] } (lazy loading active) " )
print (
f " Loaded Agents: { stats [ ' loaded_agents ' ] } (lazy loading active) "
)
print ( f " Total Groups: { stats [ ' total_groups ' ] } " )
print ( f " \n COST OPTIMIZATION: " )
cost_stats = stats [ ' cost_stats ' ]
print ( f " Budget Limit: $ { cost_stats [ ' budget_remaining ' ] + cost_stats [ ' total_cost ' ] : .2f } " )
print ( " \n COST OPTIMIZATION: " )
cost_stats = stats [ " cost_stats " ]
print (
f " Budget Limit: $ { cost_stats [ ' budget_remaining ' ] + cost_stats [ ' total_cost ' ] : .2f } "
)
print ( f " Budget Used: $ { cost_stats [ ' total_cost ' ] : .2f } " )
print ( f " Budget Remaining: $ { cost_stats [ ' budget_remaining ' ] : .2f } " )
print ( f " Cache Hit Rate: { cost_stats [ ' cache_hit_rate ' ] : .1% } " )
print ( f " \n CATEGORY BREAKDOWN: " )
for category , count in stats [ ' categories ' ] . items ( ) :
print ( " \n CATEGORY BREAKDOWN: " )
for category , count in stats [ " categories " ] . items ( ) :
print ( f " { category } : { count } agents " )
print ( f " \n ROLE BREAKDOWN: " )
for role , count in stats [ ' roles ' ] . items ( ) :
print ( " \n ROLE BREAKDOWN: " )
for role , count in stats [ " roles " ] . items ( ) :
print ( f " { role } : { count } agents " )
print ( f " \n EXPERIENCE LEVEL BREAKDOWN: " )
for level , count in stats [ ' experience_levels ' ] . items ( ) :
print ( " \n EXPERIENCE LEVEL BREAKDOWN: " )
for level , count in stats [ " experience_levels " ] . items ( ) :
print ( f " { level } : { count } agents " )
# Demonstrate cost-optimized mass task execution
print ( f " \n COST-OPTIMIZED MASS TASK DEMONSTRATION: " )
print ( " \n COST-OPTIMIZED MASS TASK DEMONSTRATION: " )
print ( " - " * 40 )
# Small task first (low cost)
small_result = template . run_mass_task (
" What is the most important skill for a software developer? " ,
agent_count = 5
agent_count = 5 ,
)
print ( f " Small Task Results: " )
print ( " Small Task Results: " )
print ( f " Agents Used: { len ( small_result [ ' agents_used ' ] ) } " )
print ( f " Cached: { small_result . get ( ' cached ' , False ) } " )
print ( f " Cost: $ { small_result [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } " )
# Large task to demonstrate full capability
print ( f " \n Large Task Demonstration (Full Capability): " )
print ( " \n Large Task Demonstration (Full Capability): " )
large_result = template . run_mass_task (
" Analyze the benefits of cloud computing for small businesses " ,
agent_count = 200 # Use more agents to show capability
agent_count = 200 , # Use more agents to show capability
)
print ( f " Agents Used: { len ( large_result [ ' agents_used ' ] ) } " )
print ( f " Cached: { large_result . get ( ' cached ' , False ) } " )
print ( f " Cost: $ { large_result [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } " )
print ( f " Budget Remaining: $ { large_result [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } " )
print (
f " Budget Remaining: $ { large_result [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } "
)
# Show what happens with cost limits
print ( f " \n Cost-Limited Task Demonstration: " )
print ( " \n Cost-Limited Task Demonstration: " )
cost_limited_result = template . run_mass_task_optimized (
" What are the key principles of agile development? " ,
agent_count = 100 ,
max_cost = 2.0 # Show cost limiting in action
max_cost = 2.0 , # Show cost limiting in action
)
print ( f " Agents Used: { len ( cost_limited_result [ ' agents_used ' ] ) } " )
print ( f " Cached: { cost_limited_result . get ( ' cached ' , False ) } " )
print ( f " Cost: $ { cost_limited_result [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } " )
print ( f " Budget Remaining: $ { cost_limited_result [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } " )
print (
f " Cost: $ { cost_limited_result [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } "
)
print (
f " Budget Remaining: $ { cost_limited_result [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } "
)
# Show final cost statistics
final_stats = template . get_system_stats ( )
print ( f " \n FINAL COST STATISTICS: " )
print ( f " Total Cost: $ { final_stats [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } " )
print ( f " Budget Remaining: $ { final_stats [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } " )
print ( f " Cache Hit Rate: { final_stats [ ' cost_stats ' ] [ ' cache_hit_rate ' ] : .1% } " )
print ( f " Total Requests: { final_stats [ ' cost_stats ' ] [ ' requests_made ' ] } " )
print ( " \n FINAL COST STATISTICS: " )
print (
f " Total Cost: $ { final_stats [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } "
)
print (
f " Budget Remaining: $ { final_stats [ ' cost_stats ' ] [ ' budget_remaining ' ] : .2f } "
)
print (
f " Cache Hit Rate: { final_stats [ ' cost_stats ' ] [ ' cache_hit_rate ' ] : .1% } "
)
print (
f " Total Requests: { final_stats [ ' cost_stats ' ] [ ' requests_made ' ] } "
)
print ( f " Cache Hits: { final_stats [ ' cost_stats ' ] [ ' cache_hits ' ] } " )
print ( f " \n DEMONSTRATION COMPLETED SUCCESSFULLY! " )
print ( f " ✅ Cost optimization working: $ { final_stats [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } spent " )
print ( f " ✅ Lazy loading working: { final_stats [ ' loaded_agents ' ] } / { final_stats [ ' total_agents ' ] } agents loaded " )
print ( f " ✅ Caching working: { final_stats [ ' cost_stats ' ] [ ' cache_hit_rate ' ] : .1% } hit rate " )
print ( " \n DEMONSTRATION COMPLETED SUCCESSFULLY! " )
print (
f " ✅ Cost optimization working: $ { final_stats [ ' cost_stats ' ] [ ' total_cost ' ] : .2f } spent "
)
print (
f " ✅ Lazy loading working: { final_stats [ ' loaded_agents ' ] } / { final_stats [ ' total_agents ' ] } agents loaded "
)
print (
f " ✅ Caching working: { final_stats [ ' cost_stats ' ] [ ' cache_hit_rate ' ] : .1% } hit rate "
)
if __name__ == " __main__ " :