Skip to main content

Budget evaluator for agent-control -- cumulative LLM cost and token tracking

Project description

agent-control-evaluator-budget

Budget evaluator for agent-control that tracks cumulative LLM token and cost usage per scope and time window.

Install

pip install "agent-control-evaluators[budget]"

Fallback direct wheel install:

pip install agent-control-evaluator-budget

For local development:

uv pip install -e evaluators/contrib/budget

Quickstart

from agent_control_evaluator_budget.budget import (
    BudgetEvaluatorConfig,
    BudgetLimitRule,
    ModelPricing,
)

config = BudgetEvaluatorConfig(
    budget_id="support-daily",
    limits=[
        BudgetLimitRule(
            scope={"agent": "support"},
            group_by="user_id",
            window_seconds=86_400,
            limit=500,
            limit_unit="usd_cents",
        ),
        BudgetLimitRule(
            scope={"agent": "support"},
            group_by="user_id",
            window_seconds=86_400,
            limit=50_000,
            limit_unit="tokens",
        ),
    ],
    pricing={
        "gpt-4.1-mini": ModelPricing(input_per_1k=0.04, output_per_1k=0.16),
    },
    model_path="model",
    metadata_paths={
        "agent": "metadata.agent",
        "user_id": "metadata.user_id",
    },
    unknown_model_behavior="block",
)

The evaluator reads token usage from standard fields such as usage.input_tokens and usage.output_tokens. Configure token_path only when your event shape uses a custom location.

Scope and group_by

Each BudgetLimitRule has a static scope and an optional group_by field.

scope filters which events a rule applies to. A rule with scope={"agent": "support"} only applies when extracted metadata contains agent="support". An empty scope is global.

group_by creates independent buckets per extracted metadata value. The common per-user pattern is:

BudgetLimitRule(
    scope={"agent": "support"},
    group_by="user_id",
    window_seconds=86_400,
    limit=500,
    limit_unit="usd_cents",
)

With metadata_paths={"user_id": "metadata.user_id"}, each user gets a separate daily budget inside the support scope.

Budget pools

budget_id identifies the accumulated budget pool.

Evaluators with the same budget_id share accumulated spend and token totals across all evaluator instances. Each evaluator still evaluates using its own configured rules -- the shared state is the bucket (the rolling sum), not the rule set. Evaluators with different budget_id values are fully isolated.

Use stable names such as support-daily, billing-global, or tenant-acme-monthly. Avoid generating a new budget_id per request unless each request should have an isolated budget.

Pricing

ModelPricing stores cost rates in cents per 1K tokens:

ModelPricing(input_per_1k=0.04, output_per_1k=0.16)

input_per_1k is applied to input tokens. output_per_1k is applied to output tokens.

Pricing and model_path are required when any rule uses limit_unit="usd_cents". Token-only rules can omit both. If an event uses a model that is not in the pricing table and a cost rule exists, unknown_model_behavior="block" fails closed. Use "warn" to log a warning and treat the cost as 0.

Dual Ceiling Pattern

Use two evaluators when cost and token ceilings need independent control records or different budget_id pools:

cost_config = BudgetEvaluatorConfig(
    budget_id="support-cost-daily",
    limits=[
        BudgetLimitRule(
            scope={"agent": "support"},
            group_by="user_id",
            window_seconds=86_400,
            limit=500,
            limit_unit="usd_cents",
        )
    ],
    pricing={
        "gpt-4.1-mini": ModelPricing(input_per_1k=0.04, output_per_1k=0.16),
    },
    model_path="model",
    metadata_paths={"agent": "metadata.agent", "user_id": "metadata.user_id"},
)

token_config = BudgetEvaluatorConfig(
    budget_id="support-token-daily",
    limits=[
        BudgetLimitRule(
            scope={"agent": "support"},
            group_by="user_id",
            window_seconds=86_400,
            limit=50_000,
            limit_unit="tokens",
        )
    ],
    metadata_paths={"agent": "metadata.agent", "user_id": "metadata.user_id"},
)

This pattern lets cost and token budgets reset, alert, and roll out independently. A single evaluator can also contain both rules when one shared pool and one control result are sufficient.

Limitations

InMemoryBudgetStore is single-process only. State is lost on restart and is not shared across workers or pods.

Use a distributed store for production deployments that run multiple processes, multiple workers, or multiple pods.

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

agent_control_evaluator_budget-8.1.0.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

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

agent_control_evaluator_budget-8.1.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file agent_control_evaluator_budget-8.1.0.tar.gz.

File metadata

File hashes

Hashes for agent_control_evaluator_budget-8.1.0.tar.gz
Algorithm Hash digest
SHA256 2787d64eb446a67dcb20b5ea62f91f464be93a99fb9b9d67f715b7a346dec6bb
MD5 7fd10ae044fc42447f9de795238956dd
BLAKE2b-256 2e25a6f0e0fd96bcb3939af5646f749504c6cb0ee6ababce1ea5e5a1de1b39c9

See more details on using hashes here.

File details

Details for the file agent_control_evaluator_budget-8.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_control_evaluator_budget-8.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5dee4844810ae0f194ff20180745584a4cdd64e0612b099c394d88a440c56b6d
MD5 020e17865703d73e2b485df738ec7ea7
BLAKE2b-256 164dec1b0a4301f1744c2e5fc2a4ef1cfe2474a0d6f7d5fa6f412186e39cbd8b

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