Spend limits

Budgets and spend limits can be attached to multiple levels to enforce your spending policies and rules. Limits can be added at the organization, API keys, users, apps and teams. Each limit has a daily, weekly and monthly window and is defined in USD. Limits and teams can be managed directly from the dashboard or via the API.

Amounts are exact USD in every SDK, never a float.

Setup

The examples below use api and limits from here. Every client reads STREAM_API_KEY and STREAM_API_SECRET from the environment, and spend limits are server side only.

using GetStream.VisionAgents;

using var api = new VisionAgentsClient();
var limits = api.SpendLimits;

Organization limits

// The organization's ceiling
await limits.CreateAsync(new LimitOptions
{
    Subject = LimitSubject.Organization,
    Interval = LimitInterval.Monthly,
    Amount = Usd.Dollars(20_000),
});

API key limits

// Default: every key of app 1234 may spend $50 a day
await limits.CreateAsync(new LimitOptions
{
    Subject = LimitSubject.ApiKey,
    AppId = "1234",
    Interval = LimitInterval.Daily,
    Amount = Usd.Dollars(50),
});

// One key gets its own limit, replacing the default
await limits.CreateAsync(new LimitOptions
{
    Subject = LimitSubject.ApiKey,
    AppId = "1234",
    ApiKey = "mz7kq2xa9b",
    Interval = LimitInterval.Daily,
    Amount = Usd.Dollars(10),
    Note = "CI key",
});

// Later: raise it. Find the row first if you lack its id
var rows = await limits.ListAsync(new LimitQuery
{
    Subject = LimitSubject.ApiKey,
    ApiKey = "mz7kq2xa9b",
    Interval = LimitInterval.Daily,
});
var found = rows[0];
await limits.SetAmountAsync(found.Id, Usd.Dollars(25));

// Delete it: the key falls back to the $50 default
await limits.DeleteAsync(found.Id);

User limits

// Default: every user of app 1234 may spend $20 a day
await limits.CreateAsync(new LimitOptions
{
    Subject = LimitSubject.User,
    AppId = "1234",
    Interval = LimitInterval.Daily,
    Amount = Usd.Dollars(20),
});

// A custom limit for one user, replacing the default
await limits.CreateAsync(new LimitOptions
{
    Subject = LimitSubject.User,
    AppId = "1234",
    UserId = "user-42",
    Interval = LimitInterval.Daily,
    Amount = Usd.Dollars(50),
});

Team limits

// Finance across the organization: one row per interval,
// each created or updated
await api.Teams.SetLimitAsync(new TeamLimit
{
    Team = "finance",
    Interval = LimitInterval.Monthly,
    Amount = Usd.Dollars(8_000),
});
await api.Teams.SetLimitAsync(new TeamLimit
{
    Team = "finance",
    Interval = LimitInterval.Weekly,
    Amount = Usd.Dollars(2_500),
});
await api.Teams.SetLimitAsync(new TeamLimit
{
    Team = "finance",
    Interval = LimitInterval.Daily,
    Amount = Usd.Dollars(600),
});

To associate an agent with a team, add the team: finance tag to its agents.yaml file. This is the same for every SDK.

agents.yaml
name: my-agent
mode: text
llm: llm-fast

# associate this agent to the finance team
tags:
  team: finance

Associate sessions to a team

You can attribute a session's usage to a team with the team field when creating it. It is server side only: a team signed into the user's token always wins, then the session's team, then the agent's tag.

var agent = api.Agent("my-agent");
var session = await agent.ChatAsync(new SessionOptions
{
    Team = "finance",
});

Router usage

If you use the router directly, without an agent or session, you can still pass the team to the client constructor as an option. Everything that client does counts under the team.

using var finance = new VisionAgentsClient(new VisionAgentsOptions
{
    Team = "finance",
});
var router = finance.Router("dentist_ai");

Reset limits

You can use the API to reset usage for users and teams.

// Reset the weekly window for one user.
// Leave Intervals out to reset every window
await limits.ResetAsync(new LimitReset
{
    Subject = LimitSubject.User,
    AppId = "1234",
    UserId = "user-42",
    Intervals = [LimitInterval.Weekly],
    Reason = "refunded a broken session",
});

// Only today's window, for team finance
await limits.ResetAsync(new LimitReset
{
    Subject = LimitSubject.Team,
    Team = "finance",
    Intervals = [LimitInterval.Daily],
    Reason = "a batch job ran twice",
});