feat: Add a ToolBudgetHook to cap per-tool calls in an Agent run

Abierto
#13,345 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
1/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

The issue names ToolBudgetHook, ConfirmationHook, Agent, and the before_tool hook point, but says the work will be handled internally and is not open to external contributions. If you are authorized to work on it, first read the existing hook and tool call count handling; done means per-tool budgets reject excess calls, including parallel calls, and serialization works.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

handled internally P1

Is your feature request related to a problem? Please describe.
An Agent can keep calling the same tool (e.g. a search tool) far past what the task needs. The only limits today are max_agent_steps and TokenBudgetHook, which both end the whole run rather than capping one tool. The harness evaluator in deepset-ai/haystack-core-integrations#3932 already scores a run as 0 when it goes over a per-tool budget, but there is no way to enforce that budget at runtime.

Describe the solution you'd like
A ToolBudgetHook that runs at before_tool and caps how often each tool can be called in one run.

from haystack.hooks import ToolBudgetHook

agent = Agent(
    chat_generator=OpenAIResponsesChatGenerator(),
    tools=[search, fetch_page, write_report],
    hooks={"before_tool": [ToolBudgetHook(budgets={"search": 5, ("fetch_page", "read_file"): 10, "*": 20})]},
)
  • Budgets are keyed by tool name, a tuple of names sharing one allowance, or "*" for every other tool. This is the same key convention as ConfirmationHook's confirmation_strategies and resolve_tool_budgets in #3932.
  • Counts come from the tool_call_counts already in State. Calls requested in the same step count against the remaining allowance in order, so 3 parallel search calls with 1 remaining run the first and reject the other two.
  • A call over budget is not executed. It gets a tool result like "Tool budget for 'search' is used up (5/5). Continue with the results you already have.", the same way ConfirmationHook handles a rejected call, so the model can adapt and finish.
  • allowed_hook_points = ("before_tool",) and to_dict/from_dict for serialization.
  • Record each rejected call as tags on the hook's span, like CompactionHook does (haystack.agent.hook.compaction.*): which tool went over budget and its count against the allowance, so budget decisions can be audited from the trace.

Describe alternatives you've considered

  • A max_tool_calls parameter on Agent. A hook keeps it opt-in and composable with other hooks.
  • Ending the run with an exit_reason like "tool_budget_exceeded" instead of rejecting the call. Open question whether we want this as an option.

Additional context
Open questions:

  • Should an exhausted tool also be hidden from the model for the rest of the run, so it stops trying and its schema stops costing tokens?

👋 Hello there! This issue will be handled internally and isn't open for external contributions. If you'd like to contribute, please take a look at issues labeled contributions welcome or good first issue. We'd really appreciate it!

Lenguaje dominante
Python
Estrellas
27.2k
Forks
3.5k
Merge medio
1 d 18 h
PR fusionados (30 d)
254

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de deepset-ai/haystack

Todos los issues de deepset-ai/haystack

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.