OpenAI Terraform 速率限制与支出
本文管理已有项目速率限制,并创建月度支出告警。速率限制约束项目在一定时间内的模型用量。支出告警在月度用量费用达到阈值时通知团队,但不会停止 API 请求,也不会强制设置支出上限。
完成主要流程后,你将得到一套可重复使用的配置,用于:
- 读取已有项目可用的速率限制记录。
- 管理一个模型的请求数和词元数限制。
- 在项目月度支出达到阈值时发送邮件告警。
开始之前
完成 Terraform 提供程序配置,并将管理 API 密钥导出为 OPENAI_ADMIN_KEY。此外还需要:
- 一个已有项目的 ID。
- 至少一个接收支出告警的邮箱地址。
评估此流程时使用测试项目。下一节会确定文本模型对应的速率限制记录。项目可用的速率限制记录由 OpenAI 创建;Terraform 更新这些记录,不会新建记录。
查找项目速率限制
读取项目可用的速率限制记录:
来源示例 1(terraform):
## [中文] 只读列出项目已有速率限制记录;选中目标model的记录并保存其id,不能将模型名直接用作记录ID。
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 的变化导致选中其他记录。
管理已有速率限制
管理所选文本模型记录的请求数与词元数限制:
来源示例 2(terraform):
## [中文] 声明管理已有速率限制记录的每分钟请求数和词元数;应用会更新已有记录,不新建模型速率限制。
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。max_requests_per_1_minute限制项目每分钟可向该模型发送的请求数。max_tokens_per_1_minute限制项目每分钟可针对该模型处理的词元数。
只设置适用于所选记录的字段。其他类型记录可能提供每分钟图像数、每分钟音频兆字节数、每日请求数或每日批处理输入词元数等限制。配置值不能超过组织和项目可用的额度。
虽然第一次 Terraform 计划将此资源显示为新增,提供程序实际更新的是已有速率限制记录,随后将其写入 Terraform 状态。修改已配置的限制会再次发送更新请求。
从配置中移除 openai_project_rate_limit 会将记录从 Terraform 状态中移除,但不会重置或删除远程速率限制。如果以后由其他流程管理该记录,先将远程值调整为期望值,再移除资源。
配置项目支出告警
创建月度项目支出告警:
来源示例 3(terraform):
## [中文] 声明项目月度邮件支出告警;20000美分为200美元,告警不会自动停止请求或强制封顶支出。
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。修改阈值或通知字段会更新告警;移除该资源会删除远程告警。
支出告警提供通知,不是硬性限额。应为每个阈值制定事件响应或管理处理流程,并另行使用速率限制约束请求量。
配置组织支出告警
如果阈值应覆盖整个组织的支出,使用组织告警:
来源示例 4(terraform):
## [中文] 声明组织月度邮件支出告警;100000美分为1000美元,范围为整个组织,不填写project_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:
来源示例 5(terraform):
## [中文] 完整配置已有速率限制发现、单条速率限制更新和项目支出告警;阈值以美分计,校验阈值大于零且收件人非空。
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、经过批准的限制值、以美分计的阈值和告警收件人:
来源示例 6(terraform):
## [中文] 填写已有项目和速率限制记录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"]
选择的请求数与词元数不能超过项目当前可用的额度。计划中的 available_rate_limits 输出会显示当前记录和值,供你比较。
初始化 Terraform,然后检查并应用保存的计划:
来源示例 7(bash):
## [中文] 初始化、检查并保存计划,显示计划后应用;预计一项接管并更新已有速率记录,一项创建支出告警。
terraform init
terraform fmt
terraform validate
terraform plan -out=tfplan
terraform show tfplan
terraform apply tfplan
第一个计划应包含两个待新增资源。Terraform 将速率限制资源描述为新增到状态,但应用时实际更新已有 OpenAI 速率限制记录。另一项新增会创建项目支出告警。应用后,terraform output 会输出可用速率限制、受管记录关联的模型,以及告警 ID。
再次运行 terraform plan,确认配置不会继续产生变更。如果出现漂移,先确认其他管理员或自动化流程是否修改了速率限制或支出告警,再应用下一次更新。