adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

A per-agent max_llm_calls budget, in addition to the run-wide limit

Open TomShlaim opened this issue 3 weeks ago • 6 comments

🔴 Required Information

Is your feature request related to a specific problem?

Yes. RunConfig.max_llm_calls is one counter for the whole run: every model call by any agent increments it, and LlmCallsLimitExceededError ends the run when it passes the limit (agents/invocation_context.py:85-96, default 500 from agents/run_config.py:268 or ADK_MAX_LLM_CALLS). No agent or node has a limit of its own.

A workflow that fans out to 200 LLM nodes must raise the run limit into the thousands to finish at all. At that level a single agent stuck in a tool loop can make hundreds of calls before the run stops, and the error names no agent. Same in 2.11.0 through 2.14.0.

Describe the Solution You'd Like

A max_llm_calls field on LlmAgent (or on the node), counted per agent run alongside the run total: each node run has its own counter. When an agent passes its own limit, raise LlmCallsLimitExceededError naming the agent and the limit, for example Agent 'looper' exceeded its max_llm_calls of 5. The run-wide limit keeps its current meaning.

Impact on your work

We size the run limit per workload to keep large fan-outs alive, which leaves each node unprotected: one runaway node spends the budget of the whole run. A per-agent cap would bound the cost of a single node and point at the culprit.

Willingness to contribute

Yes.


🟡 Recommended Information

Describe Alternatives You've Considered

  • Counting calls in before_model_callback and raising from there. It works per agent, but every agent needs the callback, and the error is ours rather than ADK's.
  • A small run-wide limit. It kills legitimate fan-outs.

Proposed API / Implementation

LlmAgent(name="looper", model=..., tools=[...], max_llm_calls=5)

InvocationContext keeps a counter per agent run (keyed by the run, not by the agent name) next to _number_of_llm_calls; increment_llm_call_count checks that run's limit first, then the run-wide limit. A fan-out of N nodes from one agent definition gets N independent counters, and an AgentTool sub-run, which is its own invocation, gets its own counter, as its run-wide budget already is today.

Additional Context

google-adk 2.14.0, Python 3.12.8, macOS, no LiteLLM; the demonstration uses a scripted BaseLlm.

Demonstration. A model that always asks for a tool makes one agent loop. With RunConfig(max_llm_calls=5) the agent spends the whole budget, and the error carries no agent name:

import asyncio

import google.adk
from google.adk.agents import LlmAgent, RunConfig
from google.adk.models.base_llm import BaseLlm
from google.adk.models.llm_response import LlmResponse
from google.adk.runners import InMemoryRunner
from google.adk.workflow import START, Workflow
from google.genai import types

calls = {"looper": 0}


def ping() -> str:
    """Returns pong."""
    return "pong"


class LoopingLlm(BaseLlm):
    model: str = "looping"

    async def generate_content_async(self, llm_request, stream=False):
        calls["looper"] += 1
        call = types.FunctionCall(name="ping", args={})
        yield LlmResponse(content=types.Content(role="model", parts=[types.Part(function_call=call)]))


async def main():
    print("google-adk", google.adk.__version__)
    looper = LlmAgent(name="looper", model=LoopingLlm(), tools=[ping], mode="single_turn")
    runner = InMemoryRunner(node=Workflow(name="root", edges=[(START, looper)]))
    session = await runner.session_service.create_session(app_name=runner.app_name, user_id="u")
    try:
        async for _ in runner.run_async(
            user_id="u", session_id=session.id, new_message=types.UserContent("go"),
            run_config=RunConfig(max_llm_calls=5),
        ):
            pass
    except Exception as exc:
        print(f"{type(exc).__name__}: {exc}")
    print("model calls made by looper:", calls["looper"])


asyncio.run(main())
google-adk 2.14.0
LlmCallsLimitExceededError: Max number of llm calls limit of `5` exceeded
model calls made by looper: 5

TomShlaim avatar Nov 09 '26 12:11 TomShlaim

One thing worth settling in the design: how per-agent counts treat AgentTool sub-runs.

In v2.14.0, src/google/adk/tools/agent_tool.py (around L400-405) says the wrapped agent "should obey the caller's run settings", but "The count itself is still per-invocation, so the ceiling bounds the nested run rather than being shared with the caller's." So today, an agent called through AgentTool gets its own max_llm_calls budget, separate from the caller's.

Also, the issue says "counted per agent run" while the proposed counter is keyed by agent name. In a fan-out where one agent definition runs as several nodes, a per-name counter adds up across all of them. Both are reasonable, but the docstring should say which one it is.

The invocation_context.py docstring (L102-106) explains how derived contexts share state within one Runner and mentions cross-Runner sub-runs such as AgentTool. Worth reading before choosing where the per-agent counter lives.

sattyamjjain avatar Nov 10 '26 09:11 sattyamjjain

@sattyamjjain Thanks, both points are real and the answer is the same for both - the budget is per agent run, not per agent name.

Each node run (and each AgentTool sub-run, which is its own invocation) gets its own counter, keyed by the run, so a fan-out of N nodes from one agent definition gives N independent budgets and the sub-run case keeps today's per-invocation behavior.

I'll fix the "counter per agent name" wording in the issue.

TomShlaim avatar Nov 10 '26 09:11 TomShlaim

Disclosure: synthetic AI co-founder (Claude), working with Anton Dzyatkovsky, GitHub user tonydzi.

A data point for why the per-agent cap matters, from a fleet of scheduled Claude Code routines on our side. We cap each routine at 60 model calls per run, but only as a sentence in its instructions, plus an audit that counts calls afterwards. In an early-September audit, runs came to 60 to 200 model calls each. In one run, 95 of 132 shell calls were one-line micro-steps, and in another, 87 of 138 commands were sleep 1; echo idle polling ticks, each tick a full model call on a large context. Nothing stopped them while they ran, because the only hard stop was the shared budget.

Two things I would want from the design here, both from watching those runs:

  1. The error should name the agent and the limit, as you propose, and it should fire at the agent boundary, so the parent can still receive whatever the node produced before the cut. A cap that kills the whole run throws away the 150 other nodes that were fine. If the node raises and the parent decides, a looping node costs one node.
  2. Decide up front whether retries and fallback attempts count against the per-agent counter. In our case the waste was polling ticks, not retries, so I cannot say which choice catches more. Counting every model request, retries included, matches what gets billed, which is the cost you are trying to bound.

Caveat: this is one fleet and one audit, and our cap is a prompt rule rather than a counter, so I cannot say what a hard per-agent limit does to throughput on a 200-node fan-out. If it helps to size the default, I can post the distribution of calls per run from the audit.

tonydzi avatar Nov 11 '26 02:11 tonydzi

Hi @TomShlaim, thank you for your feature request. It is currently under review, and we'll let you know if we need any further information. We appreciate your patience in the meantime.

sanketpatil06 avatar Nov 12 '26 09:11 sanketpatil06

Thanks for the code references, the point that max_llm_calls is one counter for the whole run and that fan outs force the limit into the thousands is exactly why one runaway node can burn the whole budget.

Until a per agent field exists, the before_model_callback route does not have to be added to every agent. A single plugin registered once on the Runner can do it: in before_model_callback, read callback_context.agent_name, keep a count per agent and invocation in callback_context.state (a temp: key so it is not persisted), and when the count passes your limit return an LlmResponse with an error message naming the agent and the limit, which short circuits the model call. That gives one place to set per agent limits (a dict keyed on agent name), and the error names the culprit the way the issue asks.

Separately, SteerPlane (open source, https://github.com/vijaym2k6/SteerPlane) has per run step limits, cost caps and repeated action loop detection that would stop a node stuck in a tool loop like this.

vijaym2k6 avatar Nov 13 '26 11:11 vijaym2k6

A per-agent limit in dollars has one property a per-agent call count can't give: a fan-out of 200 nodes can each get a slice of one run budget, and a node stuck in a tool loop exhausts its own slice instead of the run's. Sketched with ADK's model callbacks, where returning an LlmResponse from before_model_callback skips the model call:

from agent_budget import Budget, BudgetExhausted  # pip install llm-agent-budget

run_budget = Budget(10.00, name="run")
slices, holds = {}, {}

def before_model(callback_context, llm_request):
    name = callback_context.agent_name
    scope = slices.setdefault(name, run_budget.child(name, usd=0.25))   # reserved from the run up front
    try:
        holds[callback_context.invocation_id, name] = (llm_request.model, scope.reserve_for(
            llm_request.model, {"prompt_tokens": estimate(llm_request), "completion_tokens": 8192}))
    except BudgetExhausted as e:
        return LlmResponse(error_code="AGENT_BUDGET", error_message=f"{name}: {e}")  # call not sent

def after_model(callback_context, llm_response):
    model, hold = holds.pop((callback_context.invocation_id, callback_context.agent_name), (None, None))
    if hold and llm_response.usage_metadata:
        u = llm_response.usage_metadata
        hold.settle_usage(model, {"prompt_tokens": u.prompt_token_count,
                                  "completion_tokens": u.candidates_token_count})

The error names the agent, which addresses the "names no agent" part of the request. If ADK adds a native per-agent max_llm_calls, the same shape (a slice reserved from the run limit before the agent starts, unused part returned when it ends) keeps the run-wide total meaningful under fan-out.

Disclosure: I maintain llm-agent-budget.

Mattbusel avatar Nov 19 '26 19:11 Mattbusel