如需完整的文件索引,請參閱 llms.txt。在文件頁面的網址末尾加上 .md ,即可取得該頁面的 Markdown 版本。
主要導覽
主題

使用 Terraform 管理速率限制與支出警示

讓專案速率限制與設定一致,並設定支出警示。

本指南說明如何管理專案現有的速率限制,並建立每月支出警示。速率限制會為專案在一定時間內的模型用量設定上限。當每月累計使用費用達到門檻時,支出警示會通知團隊,但不會停止 API 請求,也不會強制執行支出上限。

完成主要流程後,你就能重複使用同一份設定來執行下列操作:

  • 讀取現有專案可用的速率限制記錄。
  • 管理一個模型的請求數與 Token 數上限。
  • 在專案每月支出達到門檻時,透過電子郵件發送警示。

開始前的準備

完成 Terraform 提供者設定,並將管理員 API 金鑰匯出為環境變數 OPENAI_ADMIN_KEY。你還需要準備:

  • 現有專案的 ID。
  • 至少一個用來接收支出警示的電子郵件地址。

評估這個流程時,請使用測試專案。下一節會說明如何找出文字模型的速率限制記錄。專案可用的速率限制記錄由 OpenAI 建立。Terraform 會更新這些現有記錄,不會建立新記錄。

查詢專案的速率限制

讀取專案可用的速率限制記錄:

data "openai_project_rate_limits" "current" {
  project_id = "proj_123"
}

output "project_rate_limits" {
  value = data.openai_project_rate_limits.current.rate_limits
}

這個資料來源會發出唯讀請求:

  • project_id 指定要查詢的專案。
  • rate_limits 中的每個物件各代表一筆可用的模型速率限制記錄,包含該記錄的 id、model 和適用的限制值。
  • 執行 terraform plan 或 terraform apply 後,即可在輸出中查看這些記錄。

找出 model 與你要管理的模型相符的記錄,並複製該記錄的 id。下一個資源會使用這個值作為 rate_limit_id。請明確指定這個 ID 作為輸入值,以免提供者或 API 變更後選到其他記錄。

管理現有的速率限制

為所選文字模型的記錄設定請求數與 Token 數上限:

resource "openai_project_rate_limit" "application" {
  project_id                = "proj_123"
  rate_limit_id             = "rl-gpt-3.5-turbo"
  max_requests_per_1_minute = 500
  max_tokens_per_1_minute   = 200000
}

各參數的用途如下:

  • project_id 指定要變更速率限制的專案。
  • rate_limit_id 指定現有的模型速率限制記錄。這是記錄 ID,不是模型 ID。
  • max_requests_per_1_minute 限制專案每分鐘可向該模型發送的請求數。
  • max_tokens_per_1_minute 限制專案每分鐘可透過該模型處理的 Token 數。

請只設定適用於所選記錄的欄位。其他類型的記錄可能提供不同的上限,例如每分鐘的圖片數、每分鐘的音訊資料量(以 MB 為單位)、每日請求數,或 Batch 每日輸入的 Token 數。設定值不得超過組織與專案可用的上限。

雖然第一次產生的 Terraform 計畫會將這個資源列為新增項目,但提供者實際上會更新現有的速率限制記錄,再將該記錄存入 Terraform 狀態。變更已設定的上限時,提供者會再次更新記錄。

從設定中移除 openai_project_rate_limit 時,Terraform 會從狀態中移除該記錄,但不會重設或刪除遠端的速率限制。 如果接下來要改用其他流程管理這筆記錄,請先將遠端的速率限制設為所需的值,再移除資源。

設定專案支出警示

建立專案的每月支出警示:

resource "openai_project_spend_alert" "monthly" {
  project_id                          = "proj_123"
  threshold_amount                    = 20000
  currency                            = "USD"
  interval                            = "month"
  notification_channel_type           = "email"
  notification_channel_recipients     = ["platform-alerts@example.com"]
  notification_channel_subject_prefix = "OpenAI project spend"
}

警示設定包含觸發警示的支出條件與通知管道:

  • project_id 指定專案,警示只計算該專案的支出。
  • threshold_amount 是以美分為單位的每月支出門檻。20000 代表 200 美元。
  • currency 必須設為 USD。
  • interval 必須設為 month。
  • notification_channel_type 必須設為 email。
  • notification_channel_recipients 必須包含至少一位收件者。
  • notification_channel_subject_prefix 是選填欄位,其文字會加在警示郵件主旨的開頭。

Terraform 會建立警示,並儲存建立時產生的 alert_id。變更門檻或通知欄位會更新警示。移除資源會刪除遠端警示。

支出警示只會發送通知,不會強制限制支出。請為每個門檻訂定事件應變或管理措施,並另外使用速率限制來控制請求量。

設定組織支出警示

如果要以整個組織的支出判斷是否達到門檻,請使用組織警示:

resource "openai_organization_spend_alert" "monthly" {
  threshold_amount                = 100000
  currency                        = "USD"
  interval                        = "month"
  notification_channel_type       = "email"
  notification_channel_recipients = ["platform-alerts@example.com"]
}

這個資源使用的門檻單位、週期、幣別和通知欄位都與專案警示相同。由於計算的是整個組織的支出,這個資源不接受 project_id。在此範例中,組織每月支出達到 1,000 美元後,系統就會發送電子郵件。

你可以同時管理專案與組織的警示。如果專案與組織的警示由不同團隊處理,請為兩者設定不同的門檻與收件者。

執行完整範例

前面的範例使用具體數值說明各個資源。以下完整設定改用變數來指定各環境的值,並整合三項操作:查詢專案的速率限制、管理其中一筆速率限制,以及建立一個專案支出警示。

將以下設定儲存為 main.tf:

terraform {
  required_version = ">= 1.0"

  required_providers {
    openai = {
      source  = "openai/openai"
      version = ">= 1.0.0"
    }
  }
}

provider "openai" {}

variable "project_id" {
  type = string
}

variable "rate_limit_id" {
  type        = string
  description = "Existing rate-limit record for the text model to manage."
}

variable "max_requests_per_minute" {
  type = number
}

variable "max_tokens_per_minute" {
  type = number
}

variable "project_spend_threshold_cents" {
  type        = number
  description = "Monthly project spend threshold in cents."

  validation {
    condition     = var.project_spend_threshold_cents > 0
    error_message = "The project spend threshold must be greater than zero."
  }
}

variable "alert_recipients" {
  type = list(string)

  validation {
    condition     = length(var.alert_recipients) > 0
    error_message = "Provide at least one spend-alert recipient."
  }
}

data "openai_project_rate_limits" "current" {
  project_id = var.project_id
}

resource "openai_project_rate_limit" "application" {
  project_id                = var.project_id
  rate_limit_id             = var.rate_limit_id
  max_requests_per_1_minute = var.max_requests_per_minute
  max_tokens_per_1_minute   = var.max_tokens_per_minute
}

resource "openai_project_spend_alert" "monthly" {
  project_id                          = var.project_id
  threshold_amount                    = var.project_spend_threshold_cents
  currency                            = "USD"
  interval                            = "month"
  notification_channel_type           = "email"
  notification_channel_recipients     = var.alert_recipients
  notification_channel_subject_prefix = "OpenAI project spend"
}

output "available_rate_limits" {
  value = data.openai_project_rate_limits.current.rate_limits
}

output "managed_rate_limit_model" {
  value = openai_project_rate_limit.application.model
}

output "project_spend_alert_id" {
  value = openai_project_spend_alert.monthly.alert_id
}

建立 terraform.tfvars,填入現有專案的 ID、先前查到的文字模型速率限制記錄 ID、已核准的上限、以美分為單位的支出門檻,以及警示收件者:

project_id    = "proj_123"
rate_limit_id = "rl-gpt-3.5-turbo"

max_requests_per_minute = 500
max_tokens_per_minute   = 200000

project_spend_threshold_cents = 20000
alert_recipients               = ["platform-alerts@example.com"]

設定請求數與 Token 數時,請勿超過專案目前可用的上限。計畫中的 available_rate_limits 輸出會列出目前的記錄與限制值,方便比對。

初始化 Terraform 並儲存計畫,然後檢查並套用該計畫:

terraform init
terraform fmt
terraform validate
terraform plan -out=tfplan
terraform show tfplan
terraform apply tfplan

第一次產生的計畫應列出兩個要新增的資源。Terraform 會將速率限制資源列為要新增至狀態的項目,但套用計畫時,實際上會更新 OpenAI 現有的速率限制記錄。另一個新增項目會建立專案支出警示。套用計畫後,執行 terraform output 即可列出可用的速率限制、受管理記錄所對應的模型,以及警示 ID。

再次執行 terraform plan,確認這份設定不會產生其他變更。如果計畫顯示實際狀態與設定有落差,請先確認是否有其他管理員或自動化流程變更了速率限制或支出警示,再套用更新。