Overview
The Social Algorithms framework provides a flexible system for defining custom social algorithms that control how agents communicate and interact with each other in multi-agent systems. This framework allows you to upload any arbitrary social algorithm as a callable that defines the sequence of communication between agents.Installation
Class Definition
Attributes
str
default:"None"
Unique identifier for the algorithm. If None, a UUID will be generated.
str
default:"SocialAlgorithm"
Human-readable name for the algorithm.
str
Description of what the algorithm does.
List[AgentType]
default:"None"
List of agents that will participate in the algorithm.
Callable
default:"None"
The callable that defines the communication sequence. Must accept (agents, task, **kwargs) as parameters.
float
default:"300.0"
Maximum time allowed for algorithm execution in seconds.
OutputType
default:"dict"
Format of the output from the algorithm.
bool
default:"False"
Whether to enable verbose logging.
enable_communication_logging, parallel_execution and max_workers were
removed. Agent messages are now always recorded, and the two parallel options
were stored but never used. Passing them is accepted and ignored.Attributes
Conversation
The transcript of every agent message, kept across runs. Read it with the
standard Conversation API, for example
social_alg.conversation.get_str().Constructor Validation
The constructor calls_validate_inputs() before returning, which raises:
Methods
run()
Execute the social algorithm with the given task.task(str): The task to execute using the social algorithm.algorithm_args(Dict[str, Any]): Additional arguments for the algorithm.
SocialAlgorithmResult — The result of executing the social algorithm.
Raises:
InvalidAlgorithmError: If no social algorithm is defined.TimeoutError: If the algorithm execution exceeds max_execution_time.
add_agent()
Add an agent to the social algorithm.agent(Agent): The agent to add.
ValueError: If agent is not an instance of the Agent class.
remove_agent()
Remove an agent from the social algorithm.agent_name(str): Name of the agent to remove.
AgentNotFoundError: If no agent withagent_nameis found.
get_communication_history()
Get the recorded agent messages, oldest first.{"role", "content", ...} dict.
clear_communication_history()
Clear the communication history.get_algorithm_info()
Get information about the social algorithm.Data Models
SocialAlgorithmResult
Result of executing a social algorithm.Only the
SocialAlgorithms class itself is exported from the top-level swarms package. SocialAlgorithmResult and the exception classes below must be imported from the submodule:Exception Classes
SocialAlgorithmError— Base exception for social algorithm errorsInvalidAlgorithmError— Raised when an invalid algorithm is providedAgentNotFoundError— Raised when a required agent is not found
Usage Examples
Basic Social Algorithm
Research and Development Team
Competitive Evaluation
Negotiation Algorithm
Advanced Features
Communication Logging
Everyagent.run() and agent.talk_to() call made while the algorithm runs is recorded into conversation, under the calling agent’s own name. This is always on; the algorithm does not have to report anything itself.
Timeout Protection
Algorithms are executed with timeout protection to prevent infinite loops. The default is 300 seconds (5 minutes), customizable viamax_execution_time.
Error Handling
Comprehensive error handling withInvalidAlgorithmError, AgentNotFoundError, TimeoutError, and graceful handling of agent execution failures.
Output Formatting
Results can be formatted as"dict" (default), "list", or "str".
Integration
Best Practices
- Algorithm Design: Design algorithms to be modular and reusable. Break complex algorithms into smaller, composable functions
- Error Handling: Always include proper error handling. Check for required agents and validate inputs
- Logging: Use the built-in logging system to track execution and debug issues
- Timeout Management: Set appropriate timeouts based on algorithm complexity
- Agent Roles: Clearly define roles for each agent to ensure proper communication patterns
- Testing: Test with different agent configurations and edge cases
- Documentation: Document custom algorithms thoroughly, including expected inputs and outputs