Skip to main content

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.
bool
default:"False"
Whether to log communication steps.
bool
default:"False"
Whether to enable parallel execution where possible.
int
default:"None"
Maximum number of workers for parallel execution.

Methods

run()

Execute the social algorithm with the given task.
Parameters:
  • task (str): The task to execute using the social algorithm.
  • algorithm_args (Dict[str, Any]): Additional arguments for the algorithm.
Returns: 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.

run_async()

Execute the social algorithm asynchronously under the hood (via asyncio.run) but exposed as a regular synchronous method — call it directly, do not await it.
Parameters:
  • task (str): The task to execute using the social algorithm.
  • algorithm_args (Dict[str, Any]): Additional arguments for the algorithm.
Returns: SocialAlgorithmResult

add_agent()

Add an agent to the social algorithm.
Parameters:
  • agent (Agent): The agent to add.
Raises:
  • ValueError: If agent is not an instance of the Agent class.

remove_agent()

Remove an agent from the social algorithm.
Parameters:
  • agent_name (str): Name of the agent to remove.
The current implementation removes via del self.agents[agent_name], which indexes the agents list directly rather than looking up an agent by name. Passing a name (rather than a valid list index) will raise a TypeError, not AgentNotFoundError.

get_communication_history()

Get the communication history for this algorithm execution.
Returns: List of communication steps.

clear_communication_history()

Clear the communication history.

get_algorithm_info()

Get information about the social algorithm.
Returns: Information about the algorithm including ID, name, description, agent count, and configuration.

Data Models

CommunicationStep

Represents a single step in a social algorithm.

SocialAlgorithmResult

Result of executing a social algorithm.
Only the SocialAlgorithms class itself is exported from the top-level swarms package. CommunicationStep, SocialAlgorithmResult, and the exception classes below must be imported from the submodule:

Exception Classes

  • SocialAlgorithmError — Base exception for social algorithm errors
  • InvalidAlgorithmError — Raised when an invalid algorithm is provided
  • AgentNotFoundError — 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

The framework automatically logs all communication between agents when enable_communication_logging=True, including all agent.run() calls, agent.talk_to() calls, and timestamps with metadata.

Timeout Protection

Algorithms are executed with timeout protection to prevent infinite loops. The default is 300 seconds (5 minutes), customizable via max_execution_time.

Error Handling

Comprehensive error handling with InvalidAlgorithmError, AgentNotFoundError, TimeoutError, and graceful handling of agent execution failures.

Output Formatting

Results can be formatted as "dict" (default), "list", or "str".

Parallel Execution

When parallel_execution=True, the framework can execute independent operations in parallel for improved performance.

Integration

Best Practices

  1. Algorithm Design: Design algorithms to be modular and reusable. Break complex algorithms into smaller, composable functions
  2. Error Handling: Always include proper error handling. Check for required agents and validate inputs
  3. Logging: Use the built-in logging system to track execution and debug issues
  4. Timeout Management: Set appropriate timeouts based on algorithm complexity
  5. Agent Roles: Clearly define roles for each agent to ensure proper communication patterns
  6. Testing: Test with different agent configurations and edge cases
  7. Documentation: Document custom algorithms thoroughly, including expected inputs and outputs

Source Code

View the source code on GitHub