Skip to main content

Query Usage Summary

Query the usage summary and model and tool details for a workspace, agent, or session.

POST
/api/v1/agentstudio/usage/summary
cURL
curl --request POST \
  --url 'https://{workspace_id}.{region}.maas.aliyuncs.com/api/v1/agentstudio/usage/summary' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "dimension": "workspace",
  "time_range": {
    "start_time": "2026-11-02T10:15:30+08:00",
    "end_time": "2026-11-02T14:00:01+08:00"
  },
  "granularity": "hour"
}
'
{
  "request_id": "req_summary_example",
  "data": {
    "query_range": {
      "start_time": "2026-11-02T10:00:00+08:00",
      "end_time": "2026-11-02T15:00:00+08:00"
    },
    "data_updated_at": "2026-11-02T14:10:00+08:00",
    "usage": {
      "total": {
        "input_tokens": {
          "value": 1000,
          "unit": "tokens"
        },
        "output_tokens": {
          "value": 200,
          "unit": "tokens"
        },
        "total_tokens": {
          "value": 1200,
          "unit": "tokens"
        },
        "cache_read_tokens": {
          "value": 400,
          "unit": "tokens"
        },
        "cache_creation_tokens": {
          "value": 100,
          "unit": "tokens"
        },
        "cache_hit_ratio": {
          "value": 0.4,
          "unit": "ratio"
        },
        "model_call_count": {
          "value": 5,
          "unit": "count"
        },
        "tool_call_count": {
          "value": 3,
          "unit": "count"
        },
        "builtin_tool_call_count": {
          "value": 0,
          "unit": "count"
        },
        "mcp_tool_call_count": {
          "value": 3,
          "unit": "count"
        },
        "custom_tool_call_count": {
          "value": 0,
          "unit": "count"
        },
        "turns": {
          "value": 2,
          "unit": "count"
        },
        "steps": {
          "value": 8,
          "unit": "count"
        },
        "interrupt_error_count": {
          "value": 0,
          "unit": "count"
        },
        "model_failed_count": {
          "value": 1,
          "unit": "count"
        },
        "tool_failed_count": {
          "value": 1,
          "unit": "count"
        },
        "runtime_ms": {
          "value": 60000,
          "unit": "ms"
        }
      },
      "models": [
        {
          "model_name": "qwen-plus",
          "usage": {
            "input_tokens": {
              "value": 1000,
              "unit": "tokens"
            },
            "output_tokens": {
              "value": 200,
              "unit": "tokens"
            },
            "total_tokens": {
              "value": 1200,
              "unit": "tokens"
            },
            "cache_read_tokens": {
              "value": 400,
              "unit": "tokens"
            },
            "cache_creation_tokens": {
              "value": 100,
              "unit": "tokens"
            },
            "cache_hit_ratio": {
              "value": 0.4,
              "unit": "ratio"
            },
            "call_count": {
              "value": 5,
              "unit": "count"
            },
            "failed_count": {
              "value": 1,
              "unit": "count"
            }
          }
        }
      ],
      "tools": [
        {
          "source": "mcp",
          "tool_name": "web_search",
          "usage": {
            "call_count": {
              "value": 3,
              "unit": "count"
            },
            "failed_count": {
              "value": 1,
              "unit": "count"
            }
          }
        }
      ]
    }
  }
}

Query dimensions

dimensionScopeParameters
workspaceUsage of the current workspacetime_range is required. Do not pass agent_id, agent_version, or session_id
agentUsage of a specific agent in the current workspaceagent_id and time_range are required. agent_version is optional. Do not pass session_id
sessionLifetime cumulative usage of a specific session in the current workspacePass only dimension and session_id. Do not pass agent filters, time_range, or granularity
agent_id and session_id must be non-empty single values. time_range and granularity do not accept explicit null. Passing agent filters with the workspace dimension, or time_range or granularity with the session dimension, returns HTTP 400 / invalid_parameter. For time formats, granularity selection, and range alignment rules, see Time range and granularity. Session queries return the same usage structure without query_range or session metadata such as title and status. The service verifies session ownership before querying lifetime cumulative usage. If the session exists but has no statistics yet, each metric value is null and the model and tool detail arrays are empty; the session is not reported as nonexistent. This endpoint returns the total and all model and tool details in one response and does not provide pagination. The combined limit for model and tool detail items per request is 100,000; when exceeded, HTTP 400 / query_limit_exceeded is returned. For the usage object structure and empty result rules, see Usage object.

Time range and granularity

This section applies to workspace and agent summary and trend queries. Session summaries always query lifetime cumulative usage and do not use a time range or granularity.

Time format

Start and end times are second-level RFC 3339 strings with a time zone. Z and numeric offsets are supported. Seconds can be non-zero and do not need to align to a whole minute, for example:
  • 2026-11-02T10:00:30+08:00
  • 2026-11-02T02:00:30Z, which represents the same instant as the previous example.
Unix timestamps, strings without a time zone, and fractional seconds are not accepted, for example 2026-11-02T10:00:00.123+08:00. Response times always use +08:00; runtime is still expressed in milliseconds.

Automatic granularity selection

If granularity is omitted, the granularity is selected from the original request range:
Original rangeGranularity
No more than 72 hourshour
More than 72 hours, and the end time is no later than the start time plus 3 calendar months in UTC+8day
All other casesmonth
If granularity is specified explicitly, the specified granularity is used. hour, day, and month are supported.

Actual query range

The service aligns the start time backward and the end time forward to complete periods in UTC+8 (Asia/Shanghai). The actual range is returned in query_range.
GranularityAlignment boundary
hourThe top of each hour
day00:00 UTC+8 each day
month00:00 UTC+8 on the first day of each month; calendar month lengths vary
For example, an hour query for 10:15:30—14:00:01 UTC+8 covers 10:00:00—15:00:00. If the end time is already on a boundary, it is not extended; for example, 10:00:00—14:00:00 remains unchanged. All ranges are left-closed and right-open: the start instant is included and the end instant is excluded. To compare a summary with a trend, use the same query conditions and check query_range. Ratio metrics must be recalculated from the summed numerator and denominator; they cannot be added or averaged directly.

Usage object

data.usage in summary responses and data.points[].usage in trend responses share the same structure:
FieldTypeDescription
totalobjectSummary metrics for the current range
modelsarrayModel details grouped by model_name
toolsarrayTool details grouped by source and tool_name; tools with the same name but different sources are returned separately

Metric values and units

Each metric contains value and unit:
{
  "input_tokens": {"value": 1000, "unit": "tokens"},
  "tool_call_count": {"value": 3, "unit": "count"},
  "runtime_ms": {"value": 60000, "unit": "ms"},
  "cache_hit_ratio": {"value": 0.4, "unit": "ratio"}
}
unitvalue typeMeaning
tokensint64 / nullNumber of tokens
countint64 / nullNumber of calls
msint64 / nullMilliseconds
rationumber / nullDecimal ratio, for example 0.4 means 40%
If a metric is unknown, the object and unit are still returned, for example {'{'}"value":null,"unit":"tokens"{'}'}. A known zero value is returned as 0. Do not treat null as zero.

Summary metrics (total)

FieldUnitDescription
input_tokenstokensNumber of model input tokens
output_tokenstokensNumber of model output tokens
total_tokenstokensSum of input and output tokens; null if either is unknown
cache_read_tokenstokensNumber of input tokens read from cache
cache_creation_tokenstokensNumber of input tokens used to create cache
cache_hit_ratioratiocache_read_tokens / input_tokens. null if input is 0, input is unknown, or cache reads are unknown; rounded to 8 decimal places
model_call_countcountNumber of model calls, counted from recorded model request end events, including successful and failed calls
tool_call_countcountNumber of tool calls, counted at call start, including successful, failed, and unfinished calls
builtin_tool_call_countcountNumber of built-in tool calls
mcp_tool_call_countcountNumber of MCP tool calls
custom_tool_call_countcountNumber of custom tool calls
turnscountNumber of interaction turns, counted from events with role=user
stepscountNumber of execution steps: one step for each model request end and one for each tool call start
interrupt_error_countcountNumber of error events that are not model or tool call failures; model and tool call failures are counted in their respective failure metrics
model_failed_countcountNumber of model call failures
tool_failed_countcountNumber of confirmed tool call failures
runtime_msmsTotal duration of settled runtime segments; unsettled segments are not included
Cache read and cache creation tokens are already part of input usage; do not add them again when calculating total_tokens. total, models, and tools are different views of the same range; do not add the total and the details together.

Model details (models)

FieldTypeDescription
models[].model_namestring / nullModel name
models[].usageobjectUsage metrics for the model
models[].usage contains the following metrics:
FieldUnitDescription
input_tokens, output_tokens, total_tokenstokensInput, output, and total tokens for the model; calculation rules are the same as the summary
cache_read_tokens, cache_creation_tokenstokensCache read and cache creation tokens for the model
cache_hit_ratioratioCache hit ratio for the model; calculation rules are the same as the summary
call_countcountNumber of calls to the model
failed_countcountNumber of failed calls to the model

Tool details (tools)

FieldTypeDescription
tools[].sourcestring / nullTool source: builtin (built-in tool), mcp (MCP tool), custom (custom tool); null if unknown
tools[].tool_namestring / nullTool name
tools[].usage.call_countobjectNumber of calls, unit=count
tools[].usage.failed_countobjectNumber of confirmed call failures, unit=count
Tool details do not include token or runtime metrics. The summary endpoint does not guarantee the order of the model and tool detail arrays; the trend endpoint returns points in ascending time order. If a model name, tool name, or source is unknown, the detail is still returned and the corresponding field can be null.

Data updates and empty results

  • Statistics are updated continuously as usage is processed, so recent calls may not appear yet. Results may be briefly cached; consecutive requests are not guaranteed to reflect the latest statistics immediately.
  • data_updated_at is the latest known source update time among the records in this result. It does not mean that all data before that time has been processed, and it is not the request time.
  • If a summary query has no matching records, usage.total and the units of all metrics are still returned, each metric value is null, models and tools are [], and data_updated_at is null. Workspace and agent queries still return query_range.
  • If a trend query has no matching records, an empty points array is returned. Missing periods are not filled with zeros automatically.

Error codes

HTTP statusError codeDescription and handling
400invalid_parameterInvalid parameters, including unsupported dimensions, granularity, time format, page size, or malformed request body. Correct the request based on error.message
400invalid_page_tokenThe page token is malformed, or the identity, original query conditions, actual granularity, or page_size do not match the token. Use the token returned by the previous page and keep the query conditions unchanged. Trend queries only
400query_limit_exceededThe combined model and tool detail items exceed 100,000: calculated over the entire query range for summaries and over the current page for trends. For summaries, narrow the time range or query a more specific dimension; for trends, reduce page_size or use a finer granularity
404not_foundThe session does not exist or does not belong to the current account and workspace. Session summary queries only
410page_token_expiredThe page token has expired. Remove page_token and restart from the first page. Trend queries only
429rate_limitBoth usage endpoints share the rate limit for the Alibaba Cloud account and workspace. Reduce concurrency and retry later
503service_unavailableThe usage query or pagination service is temporarily unavailable. Retry later
500internal_errorInternal service error. Keep request_id for troubleshooting

Authorizations

string
header
required

DashScope API Key

Body

application/json
enum<string>
required

查询维度,区分大小写。取值仅为 workspace、agent、session

workspace,agent,session
string

dimension=agent 时必填,指定一个 Agent;其他维度不传

string | null

仅 dimension=agent 可用。指定 Agent 版本;不传或为 null 时合并全部版本。不能是空字符串或纯空白;按原始字符串精确匹配,不自动去除首尾空格

string

dimension=session 时必填。使用创建 Session 时返回的 ID;必须为非空、非纯空白的单个字符串,其他维度不传

object

workspace、agent 维度必填;session 维度不接受。时间范围按 UTC+8 对齐

enum<string>

workspace、agent 维度可用;不传时自动选择。影响实际统计区间的对齐方式。session 维度不传。不接受显式 null

hour,day,month

Response

200-application/json
string

请求标识,可用于问题排查

object

用量查询结果