本指南介绍如何管理项目现有的速率限制并创建月度支出提醒。速率限制用于控制项目在一定时间内的模型用量。当月度支出达到阈值时,支出提醒会通知您的团队,但不会阻止 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。max_requests_per_1_minute限制项目每分钟可向该模型发送的请求数。max_tokens_per_1_minute限制项目使用该模型时每分钟可处理的 Token 数。
请只设置适用于所选记录的字段。其他类型的记录可能提供不同的上限,例如每分钟的图像数、每分钟的音频数据量(以兆字节计)、每天的请求数,或 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,确认配置不会产生其他变更。如果计划显示配置漂移,请先确认是否有其他管理员或自动化流程修改了速率限制或支出提醒,再应用下一次更新。