Skip to content

Apply delegation_budget's rates, timeout and policy to delegated calls - #753

Open
yorzi wants to merge 2 commits into
activeagents:mainfrom
yorzi:fix/delegation-agent-wide-budget-enforcement
Open

yorzi wants to merge 2 commits into
activeagents:mainfrom
yorzi:fix/delegation-agent-wide-budget-enforcement

Conversation

@yorzi

@yorzi yorzi commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What this does

delegation_budget accepts every budget key, and the docs and Budget comments show it with rates:, but Delegation::Runner read only the agent-wide budget's limits. Everything else came from the delegation's own budget:

  • rates: set agent-wide priced nothing: Pricing.cost_for got budget.rates (the delegation's, usually nil), so with no registered price the agent-wide max_cost never fired.
  • timeout: set agent-wide was never enforced; with_timeout used budget.timeout only.
  • on_exceeded: :raise set agent-wide was ignored. A call refused at a shared limit, a timeout, and a delegated pause for input all followed budget.policy, which is :stop unless the delegation sets its own.

On main, with delegation_budget max_cost: 0.5, rates: { input: 1_000_000, output: 1_000_000 }, every delegation records a cost of 0.0 and keeps running; with delegation_budget max_calls: 1, on_exceeded: :raise the second call returns { error: "budget_exceeded" } instead of raising.

The runner now:

  • prices a call with the delegation's rates, or the agent-wide ones when it has none, and records that cost on both ledgers;
  • runs a call under the shorter of the two timeouts, and reports that timeout in the result and the error;
  • uses the delegation's on_exceeded, or the agent-wide policy when it sets none, for refusals, timeouts and paused sub-agents.

docs/actions/delegation.md says so under Budgets.

Decision for the maintainer

A delegation's own on_exceeded and rates win over the agent-wide ones, even when the limit that tripped is agent-wide, the same way delegate_to ..., budget: already overrides the callee's contract budget. The alternative is to take the policy from whichever budget was violated. That changes nothing for the common case (only one of them sets a policy), and I'm glad to switch if you prefer it.

Tests

test/features/delegation_test.rb and test/features/input_requests_test.rb:

  • the agent-wide rate prices both ledgers and trips the shared max_cost;
  • an exhausted agent-wide budget raises under agent-wide :raise;
  • a delegation cannot extend the agent-wide timeout (0.05 s wins over 0.5 s, in the result and its message);
  • a shorter delegation timeout raises under the agent-wide :raise;
  • a delegated pause raises under the agent-wide :raise;
  • a delegation's own rates and on_exceeded still override the agent-wide ones (this one passes before the change too; it guards the precedence).

The first five fail on main and pass with the change.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the contributing guide
  • This fits activeagent's stated focus
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my use case

Related issue

Fixes #749

Quality check

  • The project's hooks and linters pass (bin/rubocop on the changed files)
  • The new tests fail before this change and pass after it
  • The full test suite passes (run in full, Ruby 3.3.5, gemfiles/rails8.gemfile: 4732 runs, 0 failures, 11 errors, 66 skips. The 11 errors are all in actionagent/test/cli_contract_test.rb and are the same on unmodified main in my environment (NoMethodError: undefined method id' for an instance of Hashin rack-session under WebMock'sto_rack`), so they are unrelated to this change)
  • I updated documentation where needed (docs/actions/delegation.md, CHANGELOG entry)
  • I didn't edit generated files by hand

Ownership

  • I understand every line of this change and can answer questions about it

API changes

  • Breaking change
  • New public methods or classes
  • Changed method signatures
  • No API changes

Behavior change to note: an app that set delegation_budget on_exceeded: :raise or timeout: and relied on them doing nothing will now see them apply.

yorzi added 2 commits December 3, 2026 22:24
The runner priced calls and selected timeouts and refusal policies
from the individual delegation alone. Shared limits could therefore
count no cost or answer with the wrong policy.

This branch has not been deployed

No deployments
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.

Delegation ignores delegation_budget's rates, timeout and on_exceeded

1 participant