# gather_usage_summary

## ``autogen.gather_usage_summary [#](https://docs.ag2.ai/0.9/docs/api-reference/autogen/gather_usage_summary/#autogen.gather_usage_summary "Permanent link")

```
gather_usage_summary(agents)
```

Gather usage summary from all agents.

| PARAMETER | DESCRIPTION |
| --- | --- |
| `agents` | (list): List of agents.<br>**TYPE:**`list[Agent]` |

| RETURNS | DESCRIPTION |
| --- | --- |
| `dictionary` | A dictionary containing two keys: - "usage_including_cached_inference": Cost information on the total usage, including the tokens in cached inference. - "usage_excluding_cached_inference": Cost information on the usage of tokens, excluding the tokens in cache. No larger than "usage_including_cached_inference".<br>**TYPE:**`dict[str, dict[str, Any]]` |

Example:

```
{
    "usage_including_cached_inference": {
        "total_cost": 0.0006090000000000001,
        "gpt-35-turbo": {
            "cost": 0.0006090000000000001,
            "prompt_tokens": 242,
            "completion_tokens": 123,
            "total_tokens": 365,
        },
    },
    "usage_excluding_cached_inference": {
        "total_cost": 0.0006090000000000001,
        "gpt-35-turbo": {
            "cost": 0.0006090000000000001,
            "prompt_tokens": 242,
            "completion_tokens": 123,
            "total_tokens": 365,
        },
    },
}
```

Note: If none of the agents incurred any cost (not having a client), then the usage_including_cached_inference and usage_excluding_cached_inference will be `{'total_cost': 0}`.

Source code in `autogen/agentchat/utils.py`

|     |     |
| --- | --- |
| ```<br> 36<br> 37<br> 38<br> 39<br> 40<br> 41<br> 42<br> 43<br> 44<br> 45<br> 46<br> 47<br> 48<br> 49<br> 50<br> 51<br> 52<br> 53<br> 54<br> 55<br> 56<br> 57<br> 58<br> 59<br> 60<br> 61<br> 62<br> 63<br> 64<br> 65<br> 66<br> 67<br> 68<br> 69<br> 70<br> 71<br> 72<br> 73<br> 74<br> 75<br> 76<br> 77<br> 78<br> 79<br> 80<br> 81<br> 82<br> 83<br> 84<br> 85<br> 86<br> 87<br> 88<br> 89<br> 90<br> 91<br> 92<br> 93<br> 94<br> 95<br> 96<br> 97<br> 98<br> 99<br>100<br>101<br>``` | ````<br>@export_module("autogen")<br>def gather_usage_summary(agents: list[Agent]) -> dict[str, dict[str, Any]]:<br>    r"""Gather usage summary from all agents.<br>    Args:<br>        agents: (list): List of agents.<br>    Returns:<br>        dictionary: A dictionary containing two keys:<br>            - "usage_including_cached_inference": Cost information on the total usage, including the tokens in cached inference.<br>            - "usage_excluding_cached_inference": Cost information on the usage of tokens, excluding the tokens in cache. No larger than "usage_including_cached_inference".<br>    Example:<br>    ```python<br>    {<br>        "usage_including_cached_inference": {<br>            "total_cost": 0.0006090000000000001,<br>            "gpt-35-turbo": {<br>                "cost": 0.0006090000000000001,<br>                "prompt_tokens": 242,<br>                "completion_tokens": 123,<br>                "total_tokens": 365,<br>            },<br>        },<br>        "usage_excluding_cached_inference": {<br>            "total_cost": 0.0006090000000000001,<br>            "gpt-35-turbo": {<br>                "cost": 0.0006090000000000001,<br>                "prompt_tokens": 242,<br>                "completion_tokens": 123,<br>                "total_tokens": 365,<br>            },<br>        },<br>    }<br>    ```<br>    Note:<br>    If none of the agents incurred any cost (not having a client), then the usage_including_cached_inference and usage_excluding_cached_inference will be `{'total_cost': 0}`.<br>    """<br>    def aggregate_summary(usage_summary: dict[str, Any], agent_summary: dict[str, Any]) -> None:<br>        if agent_summary is None:<br>            return<br>        usage_summary["total_cost"] += agent_summary.get("total_cost", 0)<br>        for model, data in agent_summary.items():<br>            if model != "total_cost":<br>                if model not in usage_summary:<br>                    usage_summary[model] = data.copy()<br>                else:<br>                    usage_summary[model]["cost"] += data.get("cost", 0)<br>                    usage_summary[model]["prompt_tokens"] += data.get("prompt_tokens", 0)<br>                    usage_summary[model]["completion_tokens"] += data.get("completion_tokens", 0)<br>                    usage_summary[model]["total_tokens"] += data.get("total_tokens", 0)<br>    usage_including_cached_inference = {"total_cost": 0}<br>    usage_excluding_cached_inference = {"total_cost": 0}<br>    for agent in agents:<br>        if getattr(agent, "client", None):<br>            aggregate_summary(usage_including_cached_inference, agent.client.total_usage_summary)  # type: ignore[attr-defined]<br>            aggregate_summary(usage_excluding_cached_inference, agent.client.actual_usage_summary)  # type: ignore[attr-defined]<br>    return {<br>        "usage_including_cached_inference": usage_including_cached_inference,<br>        "usage_excluding_cached_inference": usage_excluding_cached_inference,<br>    }<br>```` |
