mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add AI budget policy and period deployment config (#25122)
Closes https://linear.app/codercom/issue/AIGOV-283/add-deployment-config-for-ai-budget-policy-and-period Adds `CODER_AI_BUDGET_POLICY` and `CODER_AI_BUDGET_PERIOD` deployment options for AI Governance cost controls.
This commit is contained in:
@@ -574,6 +574,34 @@ var PostgresAuthDrivers = []string{
|
||||
// based on max open connections.
|
||||
const PostgresConnMaxIdleAuto = "auto"
|
||||
|
||||
// AIBudgetPolicy determines how the effective group is selected when a user
|
||||
// belongs to multiple groups with AI budgets configured.
|
||||
type AIBudgetPolicy string
|
||||
|
||||
const (
|
||||
// AIBudgetPolicyHighest selects the group with the highest spend limit.
|
||||
AIBudgetPolicyHighest AIBudgetPolicy = "highest"
|
||||
)
|
||||
|
||||
// AIBudgetPolicies lists the supported AIBudgetPolicy values.
|
||||
var AIBudgetPolicies = []string{
|
||||
string(AIBudgetPolicyHighest),
|
||||
}
|
||||
|
||||
// AIBudgetPeriod determines when accumulated AI spend resets to zero,
|
||||
// aligned to UTC calendar boundaries.
|
||||
type AIBudgetPeriod string
|
||||
|
||||
const (
|
||||
// AIBudgetPeriodMonth resets spend at the start of each UTC calendar month.
|
||||
AIBudgetPeriodMonth AIBudgetPeriod = "month"
|
||||
)
|
||||
|
||||
// AIBudgetPeriods lists the supported AIBudgetPeriod values.
|
||||
var AIBudgetPeriods = []string{
|
||||
string(AIBudgetPeriodMonth),
|
||||
}
|
||||
|
||||
// DeploymentValues is the central configuration values the coder server.
|
||||
type DeploymentValues struct {
|
||||
Verbose serpent.Bool `json:"verbose,omitempty"`
|
||||
@@ -3893,6 +3921,27 @@ Write out the current server config as YAML to stdout.`,
|
||||
YAML: "circuit_breaker_max_requests",
|
||||
},
|
||||
|
||||
{
|
||||
Name: "AI Budget Policy",
|
||||
Description: "Determines the effective group when a user belongs to multiple groups with AI budgets. \"highest\" selects the group with the largest spend limit, and is currently the only supported value.",
|
||||
Flag: "ai-budget-policy",
|
||||
Env: "CODER_AI_BUDGET_POLICY",
|
||||
Value: serpent.EnumOf(&c.AI.BridgeConfig.BudgetPolicy, AIBudgetPolicies...),
|
||||
Default: string(AIBudgetPolicyHighest),
|
||||
Group: &deploymentGroupAIBridge,
|
||||
YAML: "budget_policy",
|
||||
},
|
||||
{
|
||||
Name: "AI Budget Period",
|
||||
Description: "Determines when accumulated AI spend resets to zero, aligned to UTC calendar boundaries. Only \"month\" is currently supported.",
|
||||
Flag: "ai-budget-period",
|
||||
Env: "CODER_AI_BUDGET_PERIOD",
|
||||
Value: serpent.EnumOf(&c.AI.BridgeConfig.BudgetPeriod, AIBudgetPeriods...),
|
||||
Default: string(AIBudgetPeriodMonth),
|
||||
Group: &deploymentGroupAIBridge,
|
||||
YAML: "budget_period",
|
||||
},
|
||||
|
||||
// AI Bridge Proxy Options
|
||||
{
|
||||
Name: "AI Bridge Proxy Enabled",
|
||||
@@ -4107,6 +4156,9 @@ type AIBridgeConfig struct {
|
||||
StructuredLogging serpent.Bool `json:"structured_logging" typescript:",notnull"`
|
||||
SendActorHeaders serpent.Bool `json:"send_actor_headers" typescript:",notnull"`
|
||||
AllowBYOK serpent.Bool `json:"allow_byok" typescript:",notnull"`
|
||||
// Budget settings for AI Governance cost controls.
|
||||
BudgetPolicy string `json:"budget_policy,omitempty" typescript:",notnull"`
|
||||
BudgetPeriod string `json:"budget_period,omitempty" typescript:",notnull"`
|
||||
// Circuit breaker protects against cascading failures from upstream AI
|
||||
// provider overload (503, 529).
|
||||
CircuitBreakerEnabled serpent.Bool `json:"circuit_breaker_enabled" typescript:",notnull"`
|
||||
|
||||
@@ -768,6 +768,66 @@ func TestRetentionConfigParsing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAIBudgetConfigParsing(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("Defaults", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := codersdk.DeploymentValues{}
|
||||
opts := dv.Options()
|
||||
|
||||
require.NoError(t, opts.SetDefaults())
|
||||
|
||||
assert.Equal(t, string(codersdk.AIBudgetPolicyHighest), dv.AI.BridgeConfig.BudgetPolicy)
|
||||
assert.Equal(t, string(codersdk.AIBudgetPeriodMonth), dv.AI.BridgeConfig.BudgetPeriod)
|
||||
})
|
||||
|
||||
t.Run("AcceptsSupportedValues", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := codersdk.DeploymentValues{}
|
||||
opts := dv.Options()
|
||||
|
||||
require.NoError(t, opts.SetDefaults())
|
||||
require.NoError(t, opts.ParseEnv([]serpent.EnvVar{
|
||||
{Name: "CODER_AI_BUDGET_POLICY", Value: string(codersdk.AIBudgetPolicyHighest)},
|
||||
{Name: "CODER_AI_BUDGET_PERIOD", Value: string(codersdk.AIBudgetPeriodMonth)},
|
||||
}))
|
||||
|
||||
assert.Equal(t, string(codersdk.AIBudgetPolicyHighest), dv.AI.BridgeConfig.BudgetPolicy)
|
||||
assert.Equal(t, string(codersdk.AIBudgetPeriodMonth), dv.AI.BridgeConfig.BudgetPeriod)
|
||||
})
|
||||
|
||||
t.Run("RejectsUnsupportedPolicy", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := codersdk.DeploymentValues{}
|
||||
opts := dv.Options()
|
||||
|
||||
require.NoError(t, opts.SetDefaults())
|
||||
err := opts.ParseEnv([]serpent.EnvVar{
|
||||
{Name: "CODER_AI_BUDGET_POLICY", Value: "invalid"},
|
||||
})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid choice")
|
||||
})
|
||||
|
||||
t.Run("RejectsUnsupportedPeriod", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := codersdk.DeploymentValues{}
|
||||
opts := dv.Options()
|
||||
|
||||
require.NoError(t, opts.SetDefaults())
|
||||
err := opts.ParseEnv([]serpent.EnvVar{
|
||||
{Name: "CODER_AI_BUDGET_PERIOD", Value: "invalid"},
|
||||
})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid choice")
|
||||
})
|
||||
}
|
||||
|
||||
func TestComputeMaxIdleConns(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user