在這個教學中,你將分析驅動 Unity Gateway 基礎模型支出的原因。 你可以依使用者在你所管理的模型服務中歸屬性成本,然後透過系統表新增標籤以追蹤團隊、專案或使用案例的支出:
-
system.billing.usage:Azure Databricks 提供的模型所產生、以 Databricks 單位 (DBU) 計算的 Azure Databricks 使用量。 -
system.billing.list_prices:列出價格以將 DBU 換算成美元。 -
system.ai_gateway.usage:每次請求的請求數與 token 使用量。 -
system.ai_gateway.external_model_spend:外部供應商模型的預估成本(以美元計)。
Prerequisites
- 一個或多個有流量的 Unity Gateway 模型服務。 參見 建立自訂模型服務 。
- 已為您的帳戶啟用 可計費使用量系統資料表。 請參閱 啟用系統數據表。
- 存取上述系統資料表,這預設需要帳號和 metastore 管理員角色。 請參閱 授與系統表格的存取權。
Note
本教學中的金額估算是基於 標價。 他們排除協商折扣和帳單抵免。 用它們做歸因和趨勢分析,帳單則用來對帳。
步驟一:查看哪些用戶在推動消費
Tip
Genie Code (代理模式)可以幫你做到這點。 請嘗試以下範例提示:
Query system.billing.usage for MODEL_SERVING records from the past 30 days where usage_metadata.ai_gateway.endpoint_name is set. Convert DBUs to list-price USD using system.billing.list_prices at the price effective for each record, break down by model service, destination model, and requesting user, and sort by cost.
請前往 AI Gateway > 管理 > 使用儀表板 >成本分析,該介面可依 Databricks 提供的或外部供應商模型、端點及使用者細分支出,無需撰寫 SQL。 「成本明細」中的「依成本排序的前幾名使用者 (USD)」圖表會將支出最高的使用者排名。
或者,直接查詢系統資料表,看看誰在哪裡用最多。 你查詢的表格取決於模型類型:
Databricks 提供的模型
查詢 system.billing.usage 並加入 system.billing.list_prices ,將 DBU 使用率轉換為美元。 欄位 identity_metadata.run_by 識別發出每個請求的主體:
SELECT
u.usage_metadata.ai_gateway.endpoint_name AS model_service_name,
u.usage_metadata.ai_gateway.destination_model AS destination_model,
u.identity_metadata.run_by AS run_by,
SUM(u.usage_quantity * lp.pricing.effective_list.default) AS list_cost_usd
FROM system.billing.usage u
JOIN system.billing.list_prices lp
ON u.sku_name = lp.sku_name
AND u.cloud = lp.cloud
AND u.usage_unit = lp.usage_unit
AND u.usage_end_time >= lp.price_start_time
AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)
WHERE u.billing_origin_product = 'MODEL_SERVING'
AND u.usage_metadata.ai_gateway.endpoint_name IS NOT NULL
AND u.identity_metadata.run_by IS NOT NULL
AND u.usage_unit = 'DBU'
AND lp.currency_code = 'USD'
AND u.usage_date >= current_date() - INTERVAL 30 DAYS
GROUP BY model_service_name, destination_model, run_by
ORDER BY list_cost_usd DESC;
外部模型
對於透過模型提供者服務轉發給外部提供者的請求,Azure Databricks 會根據代幣使用量及提供者公布的價格估算美元成本,並依模型提供者服務中設定的任何價格乘數進行調整。 此查詢回傳與 Azure Databricks 提供的模型提供者服務查詢相同的細分:
SELECT
usage_metadata.endpoint_name AS model_provider_service_name,
identity_metadata.run_by AS run_by,
SUM(usage_quantity) AS estimated_provider_cost_usd
FROM system.ai_gateway.external_model_spend
WHERE identity_metadata.run_by IS NOT NULL
AND usage_start_time >= current_timestamp() - INTERVAL 30 DAYS
GROUP BY run_by, model_provider_service_name
ORDER BY estimated_provider_cost_usd DESC;
步驟二:依團隊或專案設定使用與支出追蹤
按使用者劃分的支出會告訴你 誰,但不會告訴你 是哪個團隊或專案。 要將代幣使用歸因於團隊或專案,請根據你的模型服務如何映射到團隊或專案來標記流量:
- 服務(端點)標籤:指每個團隊或專案都有自己的模型服務。 這個標籤會套用到所有透過該模型服務路由的請求,所以你只需要設定一次。
- 請求標籤:當多個團隊或專案共用相同的模型服務時。 呼叫者會為每個請求設定標籤,因此你在共享服務中歸屬性使用量。
服務標籤
將像 team 或 project 這樣的標記新增至模型服務。 每個經由它路由的請求都會繼承該標籤。
- 在工作區介面中,進入 AI Gateway 並開啟模型服務。
- 在模型服務 總覽 頁面的 標籤 區塊下,新增一個標籤,例如
team=ml-platform。
服務標籤會顯示在 endpoint_tags 的 system.ai_gateway.usage 欄位中,並傳遞到 custom_tags 的 system.billing.usage 欄位。 關於建立與設定模型服務的詳細資訊,請參見 建立自訂模型服務 。
請求標記
使用 project HTTP 標頭(一個將字串索引鍵對應至字串值的 JSON 物件),為個別請求附加例如 Databricks-Ai-Gateway-Request-Tags 這樣的標籤:
Databricks-Ai-Gateway-Request-Tags: {"project": "chatbot", "team": "ml-platform"}
請求標籤會出現在 request_tags 欄位中 system.ai_gateway.usage。 關於以 OpenAI SDK、Anthropic SDK 和 REST API 設定標頭的範例,請參見請求標籤。
Note
使用哪個標籤來歸因成本取決於模型:
-
Azure Databricks 提供的模型:使用 服務標籤。 只有服務標籤會傳播到
system.billing.usage。 -
外部提供者模式:使用 服務標籤或請求標籤。 兩者皆會傳遞至
system.ai_gateway.external_model_spend,用於擷取外部模型支出估算。
步驟三:分析各團隊或專案的使用情況
Tip
Genie Code (代理模式)可以幫你做到這點。 請嘗試以下範例提示:
Query system.ai_gateway.usage for the past 30 days to show request count and total tokens by the 'team' service tag in endpoint_tags, model service, and destination model. Sort by total tokens, highest first.
標籤就緒後,在 system.ai_gateway.usage 中彙整使用量,其中會擷取每個路由至 Azure Databricks 提供及外部供應商模型的請求的請求和權杖指標。 服務標籤依 endpoint_tags['team'] 分組,或請求標籤依 request_tags['project'] 分組:
服務標籤
SELECT
endpoint_tags['team'] AS team,
endpoint_name,
destination_model,
COUNT(*) AS request_count,
SUM(total_tokens) AS total_tokens
FROM system.ai_gateway.usage
WHERE endpoint_tags['team'] IS NOT NULL
GROUP BY endpoint_tags['team'], endpoint_name, destination_model
ORDER BY total_tokens DESC;
請求標記
SELECT
request_tags['project'] AS project,
endpoint_name,
destination_model,
COUNT(*) AS request_count,
SUM(total_tokens) AS total_tokens
FROM system.ai_gateway.usage
WHERE request_tags['project'] IS NOT NULL
GROUP BY request_tags['project'], endpoint_name, destination_model
ORDER BY total_tokens DESC;
若要探索不使用 SQL 的兩種分解,請使用 成本分析頁面的標籤篩選器。
步驟四:按團隊或專案分析美元支出
Tip
Genie Code (代理模式)可以幫你做到這點。 請嘗試以下範例提示:
Query system.billing.usage for MODEL_SERVING records from the past 30 days where usage_metadata.ai_gateway.endpoint_name is set. Convert DBUs to list-price USD using system.billing.list_prices at the price effective for each record, group by the 'team' service tag in custom_tags, and sort by cost.
設定好標籤後,即可將以美元計的支出歸屬於團隊或專案。 若想依標籤分組支出而不寫 SQL,請到 AI Gateway > 管理 > 使用儀表板 >成本分析 ,並使用 基於標籤的成本分析 區塊。 依端點標籤或請求標籤篩選,可查看 依端點標籤群組 的成本及 按請求標籤群組預估成本。
要使用 SQL 對支出進行歸因時,您查詢的資料表取決於模型類型:
Databricks 提供的模型
服務標籤會傳播到 custom_tags 欄位 system.billing.usage,因此你可以像第一步對用戶排名一樣,將以美元計價的支出歸因於團隊。 加入 system.billing.list_prices 以將 DBU 使用轉換為美元:
SELECT
u.custom_tags['team'] AS team,
SUM(u.usage_quantity * lp.pricing.effective_list.default) AS list_cost_usd
FROM system.billing.usage u
JOIN system.billing.list_prices lp
ON u.sku_name = lp.sku_name
AND u.cloud = lp.cloud
AND u.usage_unit = lp.usage_unit
AND u.usage_end_time >= lp.price_start_time
AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)
WHERE u.billing_origin_product = 'MODEL_SERVING'
AND u.usage_metadata.ai_gateway.endpoint_name IS NOT NULL
AND u.custom_tags['team'] IS NOT NULL
AND u.usage_unit = 'DBU'
AND lp.currency_code = 'USD'
AND u.usage_date >= current_date() - INTERVAL 30 DAYS
GROUP BY team
ORDER BY list_cost_usd DESC;
外部模型
對於 外部模型, system.ai_gateway.external_model_spend 已經以 custom_tags美元記錄預估成本,並在 中包含服務標籤與請求標籤,因此你可以依 custom_tags.request_tags 專案或團隊分組,分配服務間的支出:
SELECT
custom_tags.request_tags['team'] AS team,
SUM(usage_quantity) AS estimated_provider_cost_usd
FROM system.ai_gateway.external_model_spend
WHERE custom_tags.request_tags['team'] IS NOT NULL
AND usage_date >= current_date() - INTERVAL 30 DAYS
GROUP BY team
ORDER BY estimated_provider_cost_usd DESC;
步驟五:分析團隊或專案的代幣成本
Tip
Genie Code (代理模式)可以幫你做到這點。 請嘗試以下範例提示:
Query system.billing.usage and system.ai_gateway.usage for the past 30 days to show list-price USD per million tokens by model service, destination model, and the 'team' service tag. Convert DBUs to dollars using system.billing.list_prices at the price effective for each record, and join the two tables on usage date.
總支出告訴你預算的去向,但不代表你是否支付了公平的費用。 以每百萬代幣的成本追蹤推論支出,可以比較目的地模型、發現路由變更後的迴歸,或是合理化將流量轉移到更便宜的模型。 將成本資料表與 system.ai_gateway.usage 依模型服務、目標模型和團隊進行聯結。 兩個分頁都會依 team 服務標籤分組,因此結果與步驟 3 相符:
Databricks 提供的模型
在整個窗口內整合標價支出和代幣,然後加入模型服務、目標模型和團隊。 加入 system.billing.list_prices 以將 DBU 使用轉換為美元:
WITH cost AS (
SELECT
u.usage_metadata.ai_gateway.endpoint_name AS endpoint_name,
u.usage_metadata.ai_gateway.destination_model AS destination_model,
u.custom_tags['team'] AS team,
SUM(u.usage_quantity * lp.pricing.effective_list.default) AS list_cost_usd
FROM system.billing.usage u
JOIN system.billing.list_prices lp
ON u.sku_name = lp.sku_name
AND u.cloud = lp.cloud
AND u.usage_unit = lp.usage_unit
AND u.usage_end_time >= lp.price_start_time
AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)
WHERE u.billing_origin_product = 'MODEL_SERVING'
AND u.usage_metadata.ai_gateway.endpoint_name IS NOT NULL
AND u.custom_tags['team'] IS NOT NULL
AND u.usage_unit = 'DBU'
AND lp.currency_code = 'USD'
AND u.usage_date >= current_date() - INTERVAL 30 DAYS
GROUP BY ALL
),
tokens AS (
SELECT
endpoint_name,
destination_model,
endpoint_tags['team'] AS team,
SUM(total_tokens) AS total_tokens
FROM system.ai_gateway.usage
WHERE endpoint_tags['team'] IS NOT NULL
AND endpoint_name IS NOT NULL
AND event_time >= current_timestamp() - INTERVAL 30 DAYS
GROUP BY ALL
)
SELECT
c.endpoint_name,
c.destination_model,
c.team,
c.list_cost_usd,
t.total_tokens,
1000000 * c.list_cost_usd / NULLIF(t.total_tokens, 0) AS list_cost_usd_per_1m_tokens
FROM cost c
JOIN tokens t
ON c.endpoint_name = t.endpoint_name
AND c.destination_model = t.destination_model
AND c.team = t.team
ORDER BY list_cost_usd_per_1m_tokens DESC;
外部模型
在整個視窗內彙整支出與代幣,然後加入模型提供者服務、目的地模型及團隊。 兩個表格涵蓋相同的請求,因此以週期為單位的總數不需要在加入中加入時間鍵:
WITH spend AS (
SELECT
usage_metadata.endpoint_name AS endpoint_name,
usage_metadata.model AS destination_model,
custom_tags.endpoint_tags['team'] AS team,
SUM(usage_quantity) AS estimated_provider_cost_usd
FROM system.ai_gateway.external_model_spend
WHERE custom_tags.endpoint_tags['team'] IS NOT NULL
AND usage_date >= current_date() - INTERVAL 30 DAYS
GROUP BY ALL
),
tokens AS (
SELECT
endpoint_name,
destination_model,
endpoint_tags['team'] AS team,
SUM(total_tokens) AS total_tokens
FROM system.ai_gateway.usage
WHERE endpoint_tags['team'] IS NOT NULL
AND destination_model IS NOT NULL
AND event_time >= current_timestamp() - INTERVAL 30 DAYS
GROUP BY ALL
)
SELECT
s.endpoint_name,
s.destination_model,
s.team,
s.estimated_provider_cost_usd,
t.total_tokens,
1000000 * s.estimated_provider_cost_usd / NULLIF(t.total_tokens, 0) AS estimated_cost_usd_per_1m_tokens
FROM spend s
JOIN tokens t
ON s.endpoint_name = t.endpoint_name
AND s.destination_model = t.destination_model
AND s.team = t.team
ORDER BY estimated_cost_usd_per_1m_tokens DESC;
如果你使用請求標籤進行團隊歸因,請在代幣彙整中將 custom_tags.endpoint_tags['team'] 替換為 custom_tags.request_tags['team'],並將 endpoint_tags['team'] 替換為 request_tags['team']。
Note
Unity Gateway 不會記錄非串流、非嵌入回應超過 1 MiB 的令牌使用量,因此在這些回應常見區域,每個令牌的成本可能很高。 比較同一工作負載內的費率,而不是跨彼此無關的工作負載進行比較。