Skip to content

Dollar budgets: count unpriced calls and sub-agents, fail closed (#222) - #227

Merged
antiv merged 1 commit into
mainfrom
ccr-af51431d-ob19a4
Oct 12, 2026
Merged

antiv merged 1 commit into
mainfrom
ccr-af51431d-ob19a4

Conversation

@antiv

@antiv antiv commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Closes #222.

A dollar budget could quietly never trigger, for three reasons:

  • calls on a model with no price added nothing to the spend;
  • a database error while summing counted as $0;
  • an agent's budget left out what its sub-agents spent.

Changes

  • TokenUsageService.get_spend_since replaces get_cost_since.
    • It returns (dollars, unpriced calls), or None on a database error.
    • For an agent scope it can include the agent's sub-agents, found by following agents_config.parent_agents. get_agent_tokens_since can do the same.
  • rate_limit_config gets two new columns, block_unpriced and include_sub_agents (migration V043 for SQLite, PostgreSQL and MySQL). Both are off by default.
    • block_unpriced: unpriced calls in the period count as over the dollar budget.
    • include_sub_agents: an agent's daily token limit and its dollar budgets also cover its sub-agents. The API forces it to false for any scope other than agent, and refuses non-boolean values with a 400.
  • A block dollar budget now refuses the request when the spend can't be read. Before, it allowed it.
  • Rate Limits page:
    • The form has both switches.
    • Usage vs Limits warns when the scope had unpriced calls.
  • Dollar budget alerts say how many calls had no price, and skip a round whose spend can't be read.

Docs

  • Updated: docs/user/rate-limits.md, docs/user/alerts.md, docs/dev/alerts.md, the generated docs/reference/database.md, and CHANGELOG.md.
  • gen_docs.py drift also lists three guides. They still hold because none of them describes the changed parts:
    • docs/user/usage-and-audit.md covers the Usage page, not dollar spend per scope.
    • docs/dev/database-and-migrations.md covers the migration mechanics, which didn't change.
    • docs/dev/template-format.md doesn't touch rate limits.

Testing

  • The full suite passes (python -m unittest discover -s shared/test): 1483 tests, 1 skipped.
  • test_usd_budgets.py has new cases for unpriced calls, the sub-agent tree, fail-closed on a database error, and validation of the switches.
  • python scripts/gen_docs.py --check passes.

Generated by Claude Code

A dollar budget could quietly never trigger: calls on a model with no
price added nothing to the spend, a database error while summing counted
as $0, and an agent's budget left out its sub-agents' spend.

- TokenUsageService.get_spend_since returns (dollars, unpriced calls), or
  None on a database error; it replaces get_cost_since. Agent scopes can
  include the agent's sub-agents (parent_agents), as can
  get_agent_tokens_since.
- rate_limit_config gets block_unpriced and include_sub_agents (V043, off
  by default). block_unpriced treats unpriced calls in the period as over
  the dollar budget; include_sub_agents widens an agent's daily token limit
  and dollar budgets to its sub-agents.
- A block dollar budget refuses the request when its spend can't be read.
- Rate Limits page: both switches in the form; Usage vs Limits warns when
  the scope had unpriced calls.
- Dollar budget alerts say how many calls had no price, and skip a round
  whose spend can't be read.
@antiv
antiv merged commit 9fd6091 into main Oct 12, 2026
2 checks passed
@antiv
antiv deleted the ccr-af51431d-ob19a4 branch October 12, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dollar budgets ignore unpriced calls and sub-agents' spend

2 participants