Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Build the budget standings statement once, not on every read (CHOO-3667)

Abel Dantasbbf1238d2a27
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..8ac53dd7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,9 @@ version of their own to them without also giving them a release of their own.
group whose subgroup would move up next to a namesake is refused too, and
the dashboard now shows why. Names can no longer contain `/` or be blank.
Existing duplicates are left as they are and can be reached by id.
+- **Reading an agent's budget standings no longer builds their query each
+ time.** The standings are read twice for every message an agent posts, and
+ building the query cost more than running it; it is now built once.
#### Fixed
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
diff --git a/core/switch_core/db/stores/budget_store.py b/core/switch_core/db/stores/budget_store.py
index 12cb9aaba..bbbae55d4 100644
--- a/core/switch_core/db/stores/budget_store.py
+++ b/core/switch_core/db/stores/budget_store.py
@@ -1,7 +1,19 @@
from dataclasses import dataclass
from datetime import datetime
-
-from sqlalchemy import BigInteger, ColumnElement, and_, cast, delete, func, or_, select
+from typing import Any
+
+from sqlalchemy import (
+ BigInteger,
+ ColumnElement,
+ Select,
+ and_,
+ bindparam,
+ cast,
+ delete,
+ func,
+ or_,
+ select,
+)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased
@@ -47,6 +59,73 @@ def _period_start() -> ColumnElement[datetime]:
)
+def _standings(*, covering_agent: bool) -> Select[Any]:
+ """The statement `BudgetStore.standings` runs: every budget of the tenant
+ named by the `tenant_id` parameter, or with `covering_agent` only the
+ budgets covering the agent named by `agent_id`.
+
+ Built once, at import: it is run for every message an agent posts and
+ every row decided for an agent, and building it, a correlated subquery
+ and a dozen comparisons, cost more than running it."""
+ period_start = _period_start()
+ agent_clients = aliased(Agent)
+ spent = (
+ select(func.coalesce(func.sum(TenantUsage.amount), 0))
+ .where(
+ TenantUsage.tenant_id == UsageBudget.tenant_id,
+ TenantUsage.metric == UsageBudget.metric,
+ TenantUsage.bucket_start >= period_start,
+ or_(UsageBudget.model == "", TenantUsage.model == UsageBudget.model),
+ or_(
+ and_(
+ UsageBudget.agent_id.is_(None),
+ TenantUsage.client_id.in_(
+ select(agent_clients.client_id).where(
+ agent_clients.tenant_id == UsageBudget.tenant_id
+ )
+ ),
+ ),
+ TenantUsage.client_id == Agent.client_id,
+ ),
+ )
+ .correlate(UsageBudget, Agent)
+ .scalar_subquery()
+ )
+ statement = (
+ select(
+ UsageBudget,
+ Agent.name,
+ spent,
+ period_start + func.make_interval(0, 0, 0, 0, UsageBudget.period_hours),
+ )
+ .outerjoin(
+ Agent,
+ and_(
+ Agent.tenant_id == UsageBudget.tenant_id,
+ Agent.id == UsageBudget.agent_id,
+ ),
+ )
+ .where(UsageBudget.tenant_id == bindparam("tenant_id"))
+ .order_by(
+ UsageBudget.agent_id.nulls_first(),
+ UsageBudget.metric,
+ UsageBudget.model,
+ )
+ )
+ if covering_agent:
+ statement = statement.where(
+ or_(
+ UsageBudget.agent_id.is_(None),
+ UsageBudget.agent_id == bindparam("agent_id"),
+ )
+ )
+ return statement
+
+
+_EVERY_BUDGET = _standings(covering_agent=False)
+_BUDGETS_COVERING_AGENT = _standings(covering_agent=True)
+
+
class BudgetStore:
async def standings(
self, session: AsyncSession, *, tenant_id: str, agent_id: str | None
@@ -62,56 +141,12 @@ class BudgetStore:
Names the tenant explicitly as well as relying on the bound session,
for the reason `UsageStore.totals` gives.
"""
- period_start = _period_start()
- agent_clients = aliased(Agent)
- spent = (
- select(func.coalesce(func.sum(TenantUsage.amount), 0))
- .where(
- TenantUsage.tenant_id == UsageBudget.tenant_id,
- TenantUsage.metric == UsageBudget.metric,
- TenantUsage.bucket_start >= period_start,
- or_(UsageBudget.model == "", TenantUsage.model == UsageBudget.model),
- or_(
- and_(
- UsageBudget.agent_id.is_(None),
- TenantUsage.client_id.in_(
- select(agent_clients.client_id).where(
- agent_clients.tenant_id == UsageBudget.tenant_id
- )
- ),
- ),
- TenantUsage.client_id == Agent.client_id,
- ),
- )
- .correlate(UsageBudget, Agent)
- .scalar_subquery()
- )
- statement = (
- select(
- UsageBudget,
- Agent.name,
- spent,
- period_start + func.make_interval(0, 0, 0, 0, UsageBudget.period_hours),
- )
- .outerjoin(
- Agent,
- and_(
- Agent.tenant_id == UsageBudget.tenant_id,
- Agent.id == UsageBudget.agent_id,
- ),
- )
- .where(UsageBudget.tenant_id == tenant_id)
- .order_by(
- UsageBudget.agent_id.nulls_first(),
- UsageBudget.metric,
- UsageBudget.model,
- )
- )
- if agent_id is not None:
- statement = statement.where(
- or_(UsageBudget.agent_id.is_(None), UsageBudget.agent_id == agent_id)
+ if agent_id is None:
+ rows = await session.execute(_EVERY_BUDGET, {"tenant_id": tenant_id})
+ else:
+ rows = await session.execute(
+ _BUDGETS_COVERING_AGENT, {"tenant_id": tenant_id, "agent_id": agent_id}
)
- rows = await session.execute(statement)
return [
BudgetStanding(
id=budget.id,
diff --git a/core/tests/switch_core/db/stores/test_budget_store.py b/core/tests/switch_core/db/stores/test_budget_store.py
index da4d5528e..148a3cc08 100644
--- a/core/tests/switch_core/db/stores/test_budget_store.py
+++ b/core/tests/switch_core/db/stores/test_budget_store.py
@@ -23,6 +23,7 @@ from switch_core.db.models import (
UsageMetric,
User,
)
+from switch_core.db.stores import budget_store
from switch_core.db.stores.budget_store import BudgetNotFound, BudgetStore
@@ -200,6 +201,31 @@ class TestSpend:
assert [s.agent_id for s in covering] == [None, mine.id]
+ async def test_reading_standings_builds_no_statement(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ monkeypatch: pytest.MonkeyPatch,
+ ) -> None:
+ """Every message an agent posts reads the standings, and building
+ their statement cost more than running it, so it is built once."""
+ async with session_factory() as session:
+ mine = await _agent(session, "mine")
+ await _budget(session, agent_id=mine.id)
+ await session.commit()
+
+ def built(*_args: object, **_kwargs: object) -> None:
+ raise AssertionError("the standings statement was built again")
+
+ monkeypatch.setattr(budget_store, "select", built)
+ monkeypatch.setattr(budget_store, "_period_start", built)
+ async with session_factory() as session:
+ every = await _standings(session, None)
+ covering = await _standings(session, mine.id)
+
+ assert (
+ [s.agent_id for s in every] == [s.agent_id for s in covering] == [mine.id]
+ )
+
class TestChanges:
async def test_a_second_budget_on_the_same_thing_is_refused(