Search by

adachsoft / ai-agent-budget-monitor

Arkadiusz Adach

Per-agent budget monitoring for adachsoft/ai-agent with interchangeable usage sources and transient context warnings.

Package info

gitlab.com/a.adach/ai-agent-budget-monitor

Issues

pkg:composer/adachsoft/ai-agent-budget-monitor

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.1 2026-11-27 22:01 UTC

This package is auto-updated.

Last update: 2026-11-27 21:01:18 UTC


README

A framework-independent package for adding per-agent budget warnings to the context sent to an AI model. When one or more configured thresholds are reached, the package adds a single runtime notice for the most advanced threshold to the transformed context. The notice is not written to conversation history.

The package depends on adachsoft/ai-agent for its conversation-context strategy SPI. Applications provide the agent-extension resolver and budget usage providers; the package does not depend on ai-agent-run-lib or an agent-configuration library.

Features

  • Configure multiple warning thresholds and select the most advanced reached threshold.
  • Plug in application-specific budget usage providers by source name.
  • Add a transient warning to the transformed context without modifying conversation history.
  • Customize the warning role, placement, and template, including optional conversation activity statistics.

Installation

composer require adachsoft/ai-agent-budget-monitor

Agent configuration

Configure the extension on an agent:

{
  "extensions": {
    "budget_monitor": {
      "source": "steps",
      "warnings": [
        {
          "at_used": 10000,
          "message_template": "Work used {used} of {limit} {unit}. Goal: {instruction}"
        },
        {
          "remaining": 5000,
          "message_template": "Remaining activity: {stats}"
        },
        {
          "percent": 90
        }
      ]
    }
  }
}

source selects a registered BudgetUsageProviderInterface. Each item in warnings must contain exactly one threshold: at_used (a non-negative integer), remaining (a non-negative integer), or percent (a number from 0 to 100). Its optional message_template defaults to the package's standard warning template. If multiple thresholds are reached, only the message for the threshold with the greatest consumed-budget level is added.

For backward compatibility, warn_remaining, warn_percent, and the top-level message_template remain supported and form one additional warning. At least one warning from warnings or the legacy threshold settings is required. Warning templates support {remaining}, {used}, {limit}, {unit}, {instruction}, and {stats}. {instruction} expands to text from the first user message in the full conversation, truncated to 800 Unicode characters. {stats} expands to a deterministic, one-line Polish summary of tool calls, repeated identical calls, and textual tool-result size; activity statistics are calculated only when that placeholder is used.

The message role defaults to user and can be set to system; its position defaults to end and can be set to start.

Requirements

  • PHP
  • Composer
  • adachsoft/ai-agent

Usage

Application integration

Implement AgentExtensionsResolverInterface to return the leading agent's extensions array for a conversation, or an empty array when the conversation is unknown. Implement one or more BudgetUsageProviderInterface providers to obtain current usage by conversation ID.

Decorate the application's existing context strategy and register providers by source name:

use AdachSoft\AiAgentBudgetMonitor\BudgetMonitoringContextStrategyDecorator;
use AdachSoft\AiAgentBudgetMonitor\BudgetUsageProviderRegistry;

$budgetAwareStrategy = new BudgetMonitoringContextStrategyDecorator(
    innerStrategy: $contextStrategy,
    agentExtensionsResolver: $agentExtensionsResolver,
    budgetUsageProviderRegistry: new BudgetUsageProviderRegistry([
        'steps' => $stepsBudgetUsageProvider,
    ]),
);

The decorator delegates the strategy ID to the inner strategy, runs its transformation first, then appends or prepends a new message only to the returned collection when the threshold is reached. Invalid extension settings, unknown sources, and inactive budgets leave the inner result unchanged.