Run Autonomous • AI Agent SDK

run_autonomous: Run an autonomous task execution loop.

run_autonomous

Method

</div>

This is a method of the Agent class in the agent module.

Run an autonomous task execution loop.

This method executes a task autonomously, using the agent’s tools and capabilities to complete the task. It handles:

  • Progressive escalation based on task complexity
  • Doom loop detection and recovery
  • Iteration limits and timeouts
  • Completion detection (keyword-based or promise-based)
  • Optional context clearing between iterations

Signature

python
def run_autonomous(prompt: str, max_iterations: Optional[int], timeout_seconds: Optional[float], completion_promise: Optional[str], clear_context: bool) -> Any

Parameters

ParamField str

The task to execute

ParamField Optional[int]

Override max iterations (default from config)

ParamField Optional[float]

Timeout in seconds (default: no timeout)

ParamField Optional[str]

Optional string that signals completion when wrapped in &lt;promise&gt;TEXT&lt;/promise&gt; tags in the response

ParamField bool

Whether to clear chat history between iterations (forces agent to rely on external state like files)

Returns

ResponseField Any

AutonomyResult with success status, output, and metadata

Exceptions

ValueError

If autonomy is not enabled

Usage

python
agent = Agent(instructions="...", autonomy=True)
    result = agent.run_autonomous(
        "Refactor the auth module",
        completion_promise="DONE",
        clear_context=True
    )
    if result.success:
        print(result.output)

Uses

  • ValueError
  • track
  • logging.debug
  • time_module.time
  • isoformat
  • datetime.now
  • get_recommended_stage
  • execute_sync_callback
  • is_set
  • AutonomyResult

Source

View on GitHub

praisonaiagents/agent/agent.py at line 4706