# a_initiate_group_chat

## ``autogen.agentchat.a_initiate_group_chat`async`

```
a_initiate_group_chat(pattern, messages, max_rounds=20, safeguard_policy=None, safeguard_llm_config=None, mask_llm_config=None)
```

Initialize and run a group chat using a pattern for configuration, asynchronously.

| PARAMETER            | DESCRIPTION                                                                                           |
|---------------------|-------------------------------------------------------------------------------------------------------|
| `pattern`           | Pattern object that encapsulates the chat configuration.<br>**TYPE:**`Pattern`                       |
| `messages`          | Initial message(s).<br>**TYPE:**`list[dict[str, Any]] | str`                                       |
| `max_rounds`       | Maximum number of conversation rounds.<br>**TYPE:**`int`**DEFAULT:**`20`                             |
| `safeguard_policy`  | Optional safeguard policy dict or path to JSON file.<br>**TYPE:**`dict[str, Any] | str | None`**DEFAULT:**`None` |
| `safeguard_llm_config` | Optional LLM configuration for safeguard checks.<br>**TYPE:**`LLMConfig | None`**DEFAULT:**`None`    |
| `mask_llm_config`  | Optional LLM configuration for masking.<br>**TYPE:**`LLMConfig | None`**DEFAULT:**`None`             |

| RETURNS             | DESCRIPTION                                                                                           |
|---------------------|-------------------------------------------------------------------------------------------------------|
| `ChatResult`        | Conversations chat history.<br>**TYPE:**`ChatResult`                                               |
| `ContextVariables`  | Updated Context variables.<br>**TYPE:**`ContextVariables`                                           |
| `Agent`             | "ConversableAgent": Last speaker.                                                                    |

Source code in `autogen/agentchat/group/multi_agent_chat.py`

```python
@export_module("autogen.agentchat")
async def a_initiate_group_chat(
    pattern: "Pattern",
    messages: list[dict[str, Any]] | str,
    max_rounds: int = 20,
    safeguard_policy: dict[str, Any] | str | None = None,
    safeguard_llm_config: LLMConfig | None = None,
    mask_llm_config: LLMConfig | None = None,
) -> tuple[ChatResult, ContextVariables, "Agent"]:
    """Initialize and run a group chat using a pattern for configuration, asynchronously.
    Args:
        pattern: Pattern object that encapsulates the chat configuration.
        messages: Initial message(s).
        max_rounds: Maximum number of conversation rounds.
        safeguard_policy: Optional safeguard policy dict or path to JSON file.
        safeguard_llm_config: Optional LLM configuration for safeguard checks.
        mask_llm_config: Optional LLM configuration for masking.
    Returns:
        ChatResult:         Conversations chat history.
        ContextVariables:   Updated Context variables.
        "ConversableAgent":   Last speaker.
    """
    # Let the pattern prepare the group chat and all its components
    # Only passing the necessary parameters that aren't already in the pattern
    (
        _,  # agents,
        _,  # wrapped_agents,
        _,  # user_agent,
        context_variables,
        _,  # initial_agent,
        _,  # group_after_work,
        _,  # tool_execution,
        _,  # groupchat,
        manager,
        processed_messages,
        last_agent,
        _,  # group_agent_names,
        _,  # temp_user_list,
    ) = pattern.prepare_group_chat(
        max_rounds=max_rounds,
        messages=messages,
    )
    # Apply safeguards if provided
    if safeguard_policy:
        from .safeguards import apply_safeguard_policy
        apply_safeguard_policy(
            groupchat_manager=manager,
            policy=safeguard_policy,
            safeguard_llm_config=safeguard_llm_config,
            mask_llm_config=mask_llm_config,
        )
    # Start or resume the conversation
    if len(processed_messages) > 1:
        last_agent, last_message = await manager.a_resume(messages=processed_messages)
        clear_history = False
    else:
        last_message = processed_messages[0]
        clear_history = True
    if last_agent is None:
        raise ValueError("No agent selected to start the conversation")
    chat_result = await last_agent.a_initiate_chat(
        manager,
        message=last_message,  # type: ignore[arg-type]
        clear_history=clear_history,
        summary_method=pattern.summary_method,
    )
    # Recalculate cost to include ALL agents in the group chat
    # initiate_chat only gathers cost from [sender, recipient],
    # but in group chat we need to include all participating agents
    all_agents = list(manager.groupchat.agents) + [manager]
    chat_result.cost = cast(CostDict, gather_usage_summary(all_agents))
    cleanup_temp_user_messages(chat_result)
    return chat_result, context_variables, manager.last_speaker
```
