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.

require "getstream/vision_agents"

api = GetStream::VisionAgents::Client.new
limits = api.spend_limits

Organization limits

# The organization's ceiling
limits.create(:organization, interval: :monthly, amount: 20_000)

API key limits

# Default: every key of app 1234 may spend $50 a day
limits.create(
  :api_key,
  app_id: "1234",
  interval: :daily,
  amount: 50,
)

# One key gets its own limit, replacing the default
limits.create(
  :api_key,
  app_id: "1234",
  api_key: "mz7kq2xa9b",
  interval: :daily,
  amount: 10,
  note: "CI key",
)

# Later: raise it. Find the row first if you lack its id
found = limits.list(
  subject: :api_key,
  api_key: "mz7kq2xa9b",
  interval: :daily,
).first
limits.set_amount(found["id"], 25)

# Delete it: the key falls back to the $50 default
limits.delete(found["id"])

User limits

# Default: every user of app 1234 may spend $20 a day
limits.create(
  :user,
  app_id: "1234",
  interval: :daily,
  amount: 20,
)

# A custom limit for one user, replacing the default
limits.create(
  :user,
  app_id: "1234",
  user_id: "user-42",
  interval: :daily,
  amount: 50,
)

Team limits

# Finance across the organization: one row per interval,
# each created or updated
teams = api.teams
teams.set_limit("finance", interval: :monthly, amount: 8_000)
teams.set_limit("finance", interval: :weekly, amount: 2_500)
teams.set_limit("finance", interval: :daily, amount: 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.

agent = api.agent("my-agent")
session = agent.sessions.create(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.

finance = GetStream::VisionAgents::Client.new(team: "finance")
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
limits.reset(
  :user,
  app_id: "1234",
  user_id: "user-42",
  intervals: [:weekly],
  reason: "refunded a broken session",
)

# Only today's window, for team finance
limits.reset(
  :team,
  team: "finance",
  intervals: [:daily],
  reason: "a batch job ran twice",
)