Skip to content

Gotcha: usage_limits must be in run* function #1987

Closed
@svilupp

Description

@svilupp

Initial Checks

Description

Not a bug, just a learning to share in case others make the same mistake!

Recently, I had a few infinite loop agents despite having set usage_limits -- after some debugging, I realized that I was passing usage_limits key to the Agent, eg, Agent(..., usage_limits=UsageLimits(request_limit=3)).
I had a lot of run() calls, so I suspect I wanted to set it globally.

The correct usage based on the docs is to pass it to each invocation of the run*, eg, run(..., usage_limits=UsageLimits(request_limit=3))

My "wish" would be that providing an unsupported kwarg to the Agent would fail explicitly -- not sure where this unsupported argument got slotted.

See a modification of the docs example that DOES NOT WORK:

Example Code

# modification of https://ai.pydantic.dev/agents/#usage-limits
from typing_extensions import TypedDict

from pydantic_ai import Agent, ModelRetry
from pydantic_ai.exceptions import UsageLimitExceeded
from pydantic_ai.usage import UsageLimits

class NeverOutputType(TypedDict):
    """
    Never ever coerce data to this type.
    """

    never_use_this: str


agent = Agent(
    'openai:gpt-4.1-mini',
    retries=3,
    output_type=NeverOutputType,
    system_prompt='Any time you get a response, call the `infinite_retry_tool` to produce another response.',
    # THIS IS WRONG!!! IT WILL NOT WORK but it fails silently
    usage_limits=UsageLimits(request_limit=3)
)

counter = 0
@agent.tool_plain(retries=5)  
def infinite_retry_tool() -> int:
    global counter
    counter += 1
    print(f"Called infinite_retry_tool {counter} times")
    if counter == 5:
        raise Exception("Too many retries")
    raise ModelRetry('Please try again.')


try:
    result_sync = agent.run_sync(
        'Begin infinite retry loop!'  
    )
except UsageLimitExceeded as e:
    print(e)
    #> The next request would exceed the request_limit of 3

Python, Pydantic AI & LLM client version

0.2.18

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions