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.

import { Client, USD } from "@stream-io/calypso";

const api = new Client();
const limits = api.spendLimits;

Organization limits

// The organization's ceiling
await limits.create({
  subject: "organization",
  interval: "monthly",
  amount: USD.dollars(20_000),
});

API key limits

// Default: every key of app 1234 may spend $50 a day
await limits.create({
  subject: "api_key",
  appId: "1234",
  interval: "daily",
  amount: USD.dollars(50),
});

// One key gets its own limit, replacing the default
await limits.create({
  subject: "api_key",
  appId: "1234",
  apiKey: "mz7kq2xa9b",
  interval: "daily",
  amount: USD.dollars(10),
  note: "CI key",
});

// Later: raise it. Find the row first if you lack its id
const [found] = await limits.list({
  subject: "api_key",
  apiKey: "mz7kq2xa9b",
  interval: "daily",
});
await limits.setAmount(found!.id, USD.dollars(25));

// Delete it: the key falls back to the $50 default
await limits.delete(found!.id);

User limits

// Default: every user of app 1234 may spend $20 a day
await limits.create({
  subject: "user",
  appId: "1234",
  interval: "daily",
  amount: USD.dollars(20),
});

// A custom limit for one user, replacing the default
await limits.create({
  subject: "user",
  appId: "1234",
  userId: "user-42",
  interval: "daily",
  amount: USD.dollars(50),
});

Team limits

// Finance across the organization: one row per interval,
// each created or updated
const teams = api.teams;
await teams.setLimit("finance", {
  interval: "monthly",
  amount: USD.dollars(8_000),
});
await teams.setLimit("finance", {
  interval: "weekly",
  amount: USD.dollars(2_500),
});
await teams.setLimit("finance", {
  interval: "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.

const agent = api.agent("my-agent");
const session = await 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.

const finance = new Client({ team: "finance" });
const router = finance.router({ config: "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.reset({
  subject: "user",
  appId: "1234",
  userId: "user-42",
  intervals: ["weekly"],
  reason: "refunded a broken session",
});

// Only today's window, for team finance
await limits.reset({
  subject: "team",
  team: "finance",
  intervals: ["daily"],
  reason: "a batch job ran twice",
});