Skip to content

Add cost-based and time-based agent interruption controls #273

Description

@Hrovatin

Summary

Add cost-based and time-based interruption controls for both the main agent and subagents. This allows users to set maximum cost thresholds or execution time limits and automatically interrupt agents when exceeded, preventing unexpected API bill spikes or runaway processes.

Current Behavior

  • dreb displays real-time cost in the footer, but there's no automatic mechanism to stop execution based on cost
  • Subagents have no timeout or cost limits
  • Users must manually monitor and interrupt expensive or long-running operations
  • There's no way to set per-session or per-agent cost/time budgets

Proposed Behavior

  1. Global settings in ~/.dreb/agent/settings.json or .dreb/settings.json:

    • maxCostPerSession — stop the session if total API cost exceeds this threshold (e.g., "$5.00")
    • maxAgentDurationSeconds — maximum execution time for any single agent run before auto-interrupt
    • Optional: costWarningThreshold — show prominent warning when approaching the limit (e.g., at 80% of max)
  2. Per-subagent limits in tool calls:

    • Allow maxCost and maxDuration parameters to subagent invocations
    • Inherits from global settings if not specified
  3. Behavior on interrupt:

    • Clear message explaining which limit was hit and why
    • Graceful abort with partial results preserved
    • Session continues (user can take action)
    • Cost/timer shown in footer with visual indicator

Acceptance Criteria

  • Settings schema includes maxCostPerSession, maxAgentDurationSeconds, and optional costWarningThreshold
  • Cost tracking persists across tool calls within a single session
  • When cost or time limit is exceeded, agent receives clear error message and stops
  • Subagent tool calls can override global limits via maxCost and maxDuration params
  • Footer displays remaining budget/time (e.g., "Cost: $1.50/$5.00 | Time: 45s/3600s")
  • Documentation updated in settings.md explaining new options
  • Works consistently across all supported providers (Anthropic, OpenAI, etc.)

Technical Notes

Implementation hints:

  • Track cumulative cost from all tool calls and LLM responses
  • Use setTimeout() or process timers for agent duration limits
  • Cost calculation: use provider API response metadata (tokens) to estimate or track actual billed amounts
  • Consider subagent processes spawned by subagent tool — may need inter-process communication for time tracking
  • Provide a hook for extensions to customize interrupt behavior

Relevant files:

  • packages/coding-agent/src/settings.ts — add new settings schema
  • packages/coding-agent/src/agent-loop.ts (or equivalent) — cost/time tracking logic
  • packages/coding-agent/docs/settings.md — documentation
  • packages/agent/src/subagent.ts (or equivalent) — pass limits to spawned agents

Related:

  • Session cost display (footer) — already implemented, can be extended
  • Retry logic (retry setting) — separate concern but could interact with time limits

Labels

  • feature — new feature request
  • enhancement — improves existing behavior
  • agents — affects agent loop or subagent system
  • settings — configuration-related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions