> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trae.cn/llms.txt
> Use this file to discover all available pages before exploring further.

查询指定时间范围内，指定部门在各个模型中的 Token 用量和扣费金额。

## 所需权限 {#所需权限}

`statistics:read`

## 请求说明 {#请求说明}

* 请求方式：`POST`
* 请求地址：`{域名}/openapi/v1/statistics/department-model-usage`

## 注意事项 {#hUSXyuGSc}


* 时间跨度最长为 180 天。
* 成员在查询期内调整过部门时，其用量按发生时的部门归属拆分统计。

## 请求参数 {#请求参数}

### 请求头参数 {#请求头参数}

<!-- @cols-width: 172,122,596 -->
| **Header 参数**  | **是否必填**  | **描述**  |
| --- | --- | --- |
| `Authorization`  | 是  | 固定取值为 `Bearer {access_token}`。关于如何获取访问令牌，参考[鉴权](enterprise_authentication)。  |
| `Content-Type`  | 是  | 固定取值为 `application/json`。  |

### 请求体参数 {#请求体}

<!-- @cols-width: 183,111,100,100,419 -->
| **参数**  | **类型**  | **是否必填**  | **默认值**  | **描述**  |
| --- | --- | --- | --- | --- |
| `start_time`  | int  | 是  | \-  | 开始时间，Unix 秒级时间戳。  |
| `end_time`  | int  | 是  | \-  | 结束时间，Unix 秒级时间戳。  |
| `department_ids`  | array  | 是  | \-  | 目标部门的 ID 列表。范围为 1 到 100 个。 | \
| | | | | | \
| | | | | 部门 ID 可以通过 “[获取单个父部门的子部门](/enterprise_get-the-subdepartments-for-a-specified-parent-department)” 接口或 ”[按关键词搜索部门](/enterprise_query-department-by-keyword)“ 接口获取。  |
| `include_subtree`  | bool  | 否  | `true`  | 是否返回目标部门的所有后代部门的数据： | \
| | | | | | \
| | | | | * `true` / `null` / 省略：包含目标部门的所有后代部门的数据 | \
| | | | | * `false`：仅包含目标部门的数据  |

## 响应参数 {#响应参数}

该接口独有的响应参数如下，参数位于 `data` 中。通用响应参数参考[此文档](/enterprise_general-response-schema)。

<!-- @cols-width: 363,106,439 -->
| **参数**  | **类型**  | **描述**  |
| --- | --- | --- |
| `items`  | array  |   |
| `items[].department_id`  | string  | 部门的 ID，以十进制字符串返回。  |
| `items[].department_name`  | string  | 部门的名称。  |
| `items[].full_path`  | string  | 部门的完整路径。各级部门名称以 `/` 分隔。  |
| `items[].model_usage`  | array  | 部门的用量信息，按模型汇总。  |
| `items[].model_usage[].model_name`  | string  | 产生用量的模型的展示名，取值随模型配置变化。  |
| `items[].model_usage[].model_type`  | string  | 产生用量的模型功能类型，取值： | \
| | | | \
| | | * `Chat`：AI 问答 | \
| | | * `CUE`：代码补全  |
| `items[].model_usage[].model_source`  | string  | 模型来源，取值： | \
| | | | \
| | | * `Trae`：TRAE 内置模型 | \
| | | * `Custom`：企业自定义模型  |
| `items[].model_usage[].usage`  | object  | Token 用量汇总信息。  |
| `items[].model_usage[].usage.input_tokens`  | int  | 输入 Token 消耗总数。  |
| `items[].model_usage[].usage.output_tokens`  | int  | 输出 Token 消耗总数。  |
| `items[].model_usage[].amount`  | object  | 折算后的实际扣费金额。 | \
| | | | \
| | | ***提示***：`amount` 对象中的参数仅统计 TRAE 内置模型（`model_source` 为 `Trae`）的用量金额。  |
| `items[].model_usage[].amount.basic_amount`  | string  | 折算后，所使用的基础会话额度对应的金额消耗。返回十进制金额字符串，固定保留 6 位小数，例如 `1.500000`。  |
| `items[].model_usage[].amount.pay_go_amount`  | string  | 折算后的按量计费消耗金额。返回十进制金额字符串，固定保留 6 位小数，例如 `1.500000`。  |
| `items[].model_usage[].amount.total_amount`  | string  | 折算后的总消耗金额，即 `basic_amount` 与 `pay_go_amount` 之和。返回十进制金额字符串，固定保留 6 位小数，例如 `1.500000`。  |
| `items[].items[].model_usage[].amount.currency`  | string  | 扣费金额的币种，取值： | \
| | | | \
| | | * `CNY`：人民币 | \
| | | * `USD`：美金  |

## 示例 {#示例}

### 请求示例 {#请求示例}

```Bash
curl -X POST "${HOST}/openapi/v1/statistics/department-model-usage" \
  -H "Authorization: Bearer ${ACCESS_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
    "start_time": 1780000000,
    "end_time": 1780600000,
    "department_ids": [
      "20001"
    ],
    "include_subtree": true
  }'
```

### 响应示例 {#响应示例}

```JSON
{
  "code": 0,
  "message": "success",
  "request_id": "req_xxx",
  "data": {
    "items": [
      {
        "department_id": "20001",
        "department_name": "研发一部",
        "full_path": "示例企业/研发一部",
        "model_usage": [
          {
            "model_name": "model-a",
            "model_type": "Chat",
            "model_source": "Trae",
            "usage": {
              "input_tokens": 900000,
              "output_tokens": 210000
            },
            "amount": {
              "basic_amount": "88.000000",
              "pay_go_amount": "12.000000",
              "total_amount": "100.000000",
              "currency": "CNY"
            }
          }
        ]
      }
    ]
  }
}
```

## 错误码 {#错误码}

若该接口的请求返回错误码，参考[错误码](/enterprise_error-codes)文档进行排查。
