peargent.

Timeout

Timeouts let you set a maximum allowed execution time for a tool.

If the Tool takes longer than the configured time, its execution is stopped and handled using on_error.

Timeouts are extremely useful for:

  • Preventing tools from hanging forever
  • Stopping slow external operations
  • Keeping agent response times predictable
  • Automatically failing or retrying long-running tasks

Enable Timeout in a Tool

from peargent import create_tool

slow_tool = create_tool(
    name="slow_operation",
    description="Operation that may take too long",
    input_parameters={"data": dict},
    call_function=slow_processing,
    timeout=5.0,          # Maximum 5 seconds allowed
    on_error="return_error"
)

How Timeout Works

  1. Tool starts executing normally

  2. A timer begins (based on timeout)

  3. If execution finishes in time → result is returned

  4. If it exceeds the timeout →

    • Execution is stopped
    • A TimeoutError is raised internally
    • Result is handled via on_error
  5. If combined with retries, timeout is applied on every attempt

Timeout Example with Retries

robust_tool = create_tool(
    name="robust_api",
    description="API call with timeout + retries",
    input_parameters={"query": str},
    call_function=call_api,
    timeout=10.0,        # Max 10 seconds per attempt
    max_retries=3,       # Retry if timed out
    retry_delay=2.0,
    retry_backoff=True,
    on_error="return_error"
)

# Example timing if every attempt times out:
# Attempt 1: 10s timeout
# Wait 2s
# Attempt 2: 10s timeout
# Wait 4s
# Attempt 3: 10s timeout
# Wait 8s
# Attempt 4: 10s timeout
# → Final failure handled by on_error