Skip to main content
SocialAlgorithms runs your own Python function over a group of agents. The function, called a social algorithm, decides who speaks, in what order and with what input. It can be a pipeline, a review loop, a vote or anything else you can write in Python. While your function runs, every agent call is recorded into one transcript, so you don’t have to collect the messages yourself. run returns that transcript, ending with whatever your function returned. Reference: SocialAlgorithms.

1. Write an algorithm

A social algorithm is any callable that takes (agents, task, **kwargs). It receives the agents in the order you passed them. Here a researcher gathers facts, a critic checks them and a writer turns both into a summary:
output_type="final" on each agent makes agent.run return only the reply. With the Agent default, run returns more of the agent’s conversation, which grows each time your algorithm calls the same agent again. The constructor raises ValueError if agents is empty or holds anything that is not an Agent, and InvalidAlgorithmError if social_algorithm is not callable. run raises InvalidAlgorithmError if no algorithm was set.

2. Read the transcript

run returns the transcript, formatted by output_type. The default is "dict", a list of messages, each with role, content and an ISO timestamp: Every call is recorded with the agent’s final reply, whatever the agent’s own output_type. You don’t need to report anything from your function. Other output_type values:
  • "final" returns the last message, which is your function’s return value, as it was returned. A dict stays a dict.
  • "str" returns the transcript as one string, one [timestamp] role: content line per message.
  • "list", "json" and "yaml" give the messages in those formats.

3. Pass settings to the algorithm

Anything extra your algorithm needs goes in algorithm_args, as a dict, or as keyword arguments to run. The function is called as social_algorithm(agents, task, **algorithm_args, **kwargs), and a keyword argument wins when both set the same name. Here a writer and an editor refine a pitch for a set number of rounds:
The pitch is written for site reliability engineers, because the keyword argument overrides algorithm_args. With output_type="final", result is the dict the function returned. algorithm_args itself is never changed.

4. Agents talking to agents

agent.talk_to(other, message) has one agent think about a message and then send its reply to another agent. Inside a social algorithm it is recorded in three steps: the outgoing message under the sender’s name, with the receiver in the message’s metadata, then the sender’s reply, then the receiver’s reply.
get_communication_history() returns a copy of the transcript as a list of message dicts, whatever the output_type. Recording works by wrapping each agent’s run and talk_to for the length of the call, and putting them back afterwards, even when the algorithm fails. Agents outside this SocialAlgorithms are never touched.

5. Reuse, clear and change the group

The transcript is kept across runs: a second run adds its task and messages after the first run’s. Call clear_communication_history() to start over. add_agent and remove_agent change the group between runs, and get_algorithm_info() describes the setup. This continues from section 4:
remove_agent raises AgentNotFoundError for a name that is not in the group. Import it, and the other exceptions, from swarms.structs.social_algorithms.

Tips

  • Handle failures in your function. An error from your algorithm or from an agent is logged and raised from run. Catch it inside the algorithm if the rest of the run should go on.
  • Build in a time limit yourself. There is no built-in timeout, so put one in your algorithm if it needs one, for example a cap on rounds.
  • Track cost with usage, which sums the token counts of every agent in the group across every run.
  • Turn on logs with verbose=True to log each recorded message and how long the run took.

Next steps