---
name: tool-call-budget-guard
description: "Cap an agent's tool calls with per-tool budgets and graceful degradation when exhausted — so runaway agents don't blow through API quotas."
title: Tool-Call Budget Guard
category: agents-workflows
difficulty: intermediate
author: admin
icon: 🛑
input: structured-data
output: structured-json
phase: pre
domain: ops
tags: agent-safety,budget-control,tool-wrapper,cost-management,middleware,graceful-degradation,api-quota,runaway-prevention,tool-call-limit,structured-errors,system-message-injection,spend-logging
best_for:
  - cost-conscious agentic applications with expensive external APIs
  - preventing runaway tool-use loops in long-running agents
  - multi-step workflows with tiered tool costs
---

## Description

Middleware that enforces per-tool and per-run budgets on an agent. When a budget is near exhaustion, it warns the agent in the tool response. When exhausted, it denies the call with a structured error the agent can reason about — instead of silently failing or consuming beyond the limit.

## Why it works

Agents loop. Loops hit API limits. The default failure mode is 'keep trying, accumulate costs, fail late.' Injecting a budget signal into the tool response lets the agent adapt its plan (skip a step, batch, use a cheaper tool) rather than retry until quota is gone.

## How it works

1. Wrap each tool with a budget check: pre-call decrement, post-call refund on error. 2. Expose the remaining budget to the agent via a system message appended to the tool response: 'budget: 3/10 remaining.' 3. When a budget drops below a threshold, send a softer signal; when it hits zero, return a structured error the agent reads and reasons about. 4. Emit a per-run spend log so you can retrospectively tune budgets.
