Skip to main content

Per-tool budget reminders for Pydantic AI agents

Project description

pydantic-ai-tool-budget

Per-tool budget reminders for Pydantic AI agents.

The Problem

UsageLimits(tool_calls_limit=N) is a silent kill switch. The model has no idea it's running low on tool calls until the hard cap fires UsageLimitExceeded and the entire run dies — often after the model already did useful work.

This package injects per-tool budget reminders directly into the conversation after each tool result, so the model can see how many calls remain and plan accordingly.

See pydantic/pydantic-ai#4359 for the full discussion.

Install

uv add pydantic-ai-tool-budget
# or
pip install pydantic-ai-tool-budget

Requires pydantic-ai-slim>=0.100.0.

Quick Start

from pydantic_ai import Agent
from pydantic_ai_tool_budget import budgeted


def search(query: str) -> str:
    """Search the web."""
    return f"Results for {query}"


def lookup(city: str) -> str:
    """Look up city info."""
    return f"Info about {city}"


agent = Agent(
    'openai:gpt-4o',
    tools=[
        budgeted(search, limit=5),
        budgeted(lookup, limit=3),
        # undecorated tools work normally — no budget tracking
    ],
)

After each tool call, the model sees a message like:

search: 3/5 calls used, 2 remaining.

When a tool's budget is exhausted:

search: 5/5 calls used, 0 remaining. This tool's budget is exhausted.

Options

Threshold — only remind when budget is tight

budgeted(search, limit=10, threshold=3)  # only remind when remaining <= 3

Custom formatter

budgeted(
    search,
    limit=5,
    formatter=lambda name, used, limit: (
        f"Only {limit - used} calls left for {name}. Prioritize."
        if limit - used <= 3
        else None  # suppress when there's plenty of budget
    ),
)

How It Works

budgeted() wraps your tool function using functools.wraps, preserving its name, docstring, and parameter schema. After each call, it returns a ToolReturn with a .content field containing the budget reminder. The framework converts this to a UserPromptPart placed after the tool result in the model request — exactly where the model reads it before deciding what to do next.

This means:

  • Reminders sit in the conversation body, not the system prompt — no prompt cache busting
  • Each tool gets its own budget tracking — the model sees per-tool counts
  • No string mappings — you pass the function directly, so typos are NameErrors
  • Works with sync and async tools, with or without RunContext

API

budgeted(func, *, limit, threshold=None, formatter=None)

Parameter Type Default Description
func Callable required The tool function to wrap
limit int required Maximum calls before budget is exhausted
threshold int | None None Only remind when remaining <= threshold
formatter (name, used, limit) -> str | None None Custom reminder formatter

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_ai_tool_budget-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_tool_budget-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_tool_budget-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_ai_tool_budget-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydantic_ai_tool_budget-0.1.0.tar.gz
Algorithm Hash digest
SHA256 35ffed54cfcd4a23ec16bab7543f2f017d339601242e99692019c29d48612d4d
MD5 60eecc0180efd8eb589d70775b217f94
BLAKE2b-256 b10155f97f9afbfd5621f7010c8d9ecc9ec50309b6b42d283b7a5149789227a2

See more details on using hashes here.

File details

Details for the file pydantic_ai_tool_budget-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_ai_tool_budget-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pydantic_ai_tool_budget-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40eb58207cf346a5c90013ba243a3c10723f19be3fa55db6d147cd9b7d0e6960
MD5 cb7fe4582425c566863e5609a4f5d0e7
BLAKE2b-256 848d7e1e1a9cb79f8d1504c61cc7b0e10fd8583e3cb91e67ad1eef119c599069

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page