Skip to content

Manage Usage Limits with Go

Usage-control rules attach one or more active limits to a subject route. They are evaluated by realtime wallet reads and realtime usage debit flows.

rule, err := client.Manager.CreateUsageControlRuleForProject(ctx, "proj_gateway", types.CreateUsageControlRuleRequest{
BillingAccountID: "acct_001",
SubjectType: "org",
SubjectID: "org_001",
Currency: "USD",
Limits: []types.CreateRuleAccountLimitRequest{
{
MetricScope: "tokens",
Measure: "quantity",
Period: "month",
LimitAmount: decimal.RequireFromString("1000000"),
Timezone: "UTC",
Action: "block",
WebhookEvent: "usage_limit.exhausted",
},
{
MetricScope: "amount",
Measure: "amount",
Period: "month",
LimitAmount: decimal.RequireFromString("500"),
Timezone: "UTC",
Action: "alert",
},
},
})
if err != nil {
return err
}
fmt.Println(rule.RuleAccount.ID, len(rule.Limits))

BillingAccountID, SubjectType, and SubjectID are required. If the subject route does not already exist, Meterry creates it for the billing account before attaching limits.

Field Description
Currency Currency for amount-based limits. Inherits request currency when omitted.
MetricScope The scope being limited, such as tokens, requests, tool_calls, or amount.
SelectorType / SelectorValue Optional selector for narrower limits, such as a model family or feature.
Measure The counting method for the limit, for example quantity or amount.
Period Limit period, such as day, week, or month.
LimitAmount Decimal limit amount.
Timezone Window timezone.
Action Behavior when the limit is crossed, such as alert or block.
WebhookEvent Optional event name emitted when the limit is crossed.
rules, err := client.Manager.ListUsageControlRulesForProject(ctx, "proj_gateway", "acct_001")
if err != nil {
return err
}
for _, rule := range rules {
for _, limit := range rule.Limits {
fmt.Println(limit.ID, limit.MetricScope, limit.LimitAmount)
}
}

Limits are disabled by ID.

deleted, err := client.Manager.DeleteRuleAccountLimitForProject(ctx, "proj_gateway", "racl_001")
if err != nil {
return err
}
fmt.Println(deleted.Deleted)

Realtime limit snapshots include the active limit window state.

snapshot, err := client.Manager.ReadRealtimeUsageControlLimitsForProject(ctx, "proj_gateway", types.ReadVirtualWalletRequest{
SubjectType: "org",
SubjectID: "org_001",
Currency: "USD",
})
if err != nil {
return err
}
for _, limit := range snapshot.Limits {
fmt.Println(limit.MetricScope, limit.UsedAmount, limit.RemainingAmount, limit.Exceeded)
}
Method Description
CreateUsageControlRule Creates subject-route limits. Uses req.ProjectID when set.
CreateUsageControlRuleForProject Creates limits under /projects/:project_id/usage-control-rules.
SetSubjectRouteAndUsageLimits Alias for creating a usage-control rule.
SetSubjectRouteAndUsageLimitsForProject Project-scoped alias for creating a route with limits.
ListUsageControlRules Lists limits for an account without an explicit project path.
ListUsageControlRulesForProject Lists limits for an account under /projects/:project_id/usage-control-rules.
ListUsageControlRulesWithRequest Lists limits with a request struct.
DeleteRuleAccountLimit Disables a limit by ID.
DeleteRuleAccountLimitForProject Project-scoped disable by ID.