Skip to main content

time-budget-hook-haystack

A Haystack Agent hook that stops a run once it has been running for a set number of seconds.

max_agent_steps caps how many steps an Agent takes, and Haystack's TokenBudgetHook caps how many tokens it spends. Neither one limits how long a run takes, and that's usually the limit that matters when an Agent sits behind an API with a request timeout, or calls slow tools. TimeBudgetHook adds that limit.

Installation

pip install time-budget-hook-haystack

Requires haystack-ai>=3.2.0.

Usage

from haystack.components.agents import Agent
from haystack.components.generators.chat import OpenAIChatGenerator

from haystack_integrations.hooks.time_budget import TimeBudgetHook

agent = Agent(
    chat_generator=OpenAIChatGenerator(),  # needs OPENAI_API_KEY
    tools=[web_search],
    hooks={"before_llm": [TimeBudgetHook(max_seconds=60)]},
)

result = agent.run(messages=[...])
result["exit_reason"]
# >> "time_budget_exceeded" if the run took longer than 60 seconds

For a version that runs offline, with no API key, see examples/agent_with_time_budget.py.

Parameters

Parameter Description
max_seconds Maximum wall-clock time, in seconds, a run may take before it's stopped. Must be positive.
add_final_message If True, adds an assistant message saying the Agent stopped because the time budget was exceeded. Defaults to False.

How it works

  • The hook runs at the before_llm hook point. The first time it sees a run, it starts that run's clock.
  • Before each later LLM call, it checks the elapsed time. Once the budget is used up, it sets the Agent's stop_run state key, and the run ends with exit_reason == "time_budget_exceeded" (also exported as EXIT_REASON).
  • The check happens between steps, so an LLM call or tool call that's already running is never cut off. A run can go over the budget by up to one step.
  • To also count time spent in your before_run hooks, register the same hook instance under before_run as well. The clock then starts there.
  • One hook instance can be shared by concurrent runs, sync or async. Each run gets its own clock.
  • The hook serializes with Agent.to_dict() / Agent.from_dict() like Haystack's built-in hooks.

Combine it with other budgets as needed:

from haystack.hooks.budget import TokenBudgetHook

hooks = {"before_llm": [TimeBudgetHook(max_seconds=60), TokenBudgetHook(max_total_tokens=100_000)]}

Development

This project uses Hatch.

hatch run test:all   # run the tests
hatch run fmt        # format and lint

Publishing

Pushing a v* tag (for example v0.1.0) runs .github/workflows/release.yml, which publishes to PyPI through Trusted Publishing. No API token is needed.

License

Apache-2.0. See LICENSE.

Metadata

Release files for time-budget-hook-haystack 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for time-budget-hook-haystack 0.1.0
File Size Uploaded Provenance Actions
time_budget_hook_haystack-0.1.0.tar.gz 12.6 kB With provenance Details for time_budget_hook_haystack-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for time-budget-hook-haystack 0.1.0
File Interpreter ABI Platform Actions
time_budget_hook_haystack-0.1.0-py3-none-any.whl Python 3 none any Details for time_budget_hook_haystack-0.1.0-py3-none-any.whl

Total release size: 21.9 kB

Release files / time_budget_hook_haystack-0.1.0.tar.gz

Download URL time_budget_hook_haystack-0.1.0.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0adafb897922b1dde03ec050d3042608b09619b2d8e05883303ebb7f854228e9
BLAKE2b-256 checksum
How to use checksums
617f7906460966a58a3b7648de6e006336ef6729946ce2ed58a89047e65d4503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Release files / time_budget_hook_haystack-0.1.0-py3-none-any.whl

Download URL time_budget_hook_haystack-0.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e89a4eda6c659b62e151c68160c955e2d501c0183fe7044315dadb93c1a888f1
BLAKE2b-256 checksum
How to use checksums
7cae7f901c5f6dfe2b4055bc9a38ae652bb5836d60a8faba68a5729e1f56bb77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Security for time-budget-hook-haystack 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for time_budget_hook_haystack-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page