> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Autonomous • AI Agent SDK

> run_autonomous: Run an autonomous task execution loop.

# run\_autonomous

<div className="flex items-center gap-2">
  <Badge color="purple">Method</Badge>
</div>

> This is a method of the [**Agent**](../classes/Agent) class in the [**agent**](../modules/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 theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
def run_autonomous(prompt: str, max_iterations: Optional[int], timeout_seconds: Optional[float], completion_promise: Optional[str], clear_context: bool) -> Any
```

## Parameters

<ParamField query="prompt" type="str" required={true}>
  The task to execute
</ParamField>

<ParamField query="max_iterations" type="Optional" required={false}>
  Override max iterations (default from config)
</ParamField>

<ParamField query="timeout_seconds" type="Optional" required={false}>
  Timeout in seconds (default: no timeout)
</ParamField>

<ParamField query="completion_promise" type="Optional" required={false}>
  Optional string that signals completion when  wrapped in \<promise>TEXT\</promise> tags in the response
</ParamField>

<ParamField query="clear_context" type="bool" required={false} default="False">
  Whether to clear chat history between iterations (forces agent to rely on external state like files)
</ParamField>

### Returns

<ResponseField name="Returns" type="Any">
  AutonomyResult with success status, output, and metadata
</ResponseField>

### Exceptions

<AccordionGroup>
  <Accordion title="ValueError">
    If autonomy is not enabled
  </Accordion>
</AccordionGroup>

## Usage

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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`
* `time_module.time`
* `isoformat`
* `datetime.now`
* `get_recommended_stage`
* `AutonomyResult`
* `chat`
* `clear_history`

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-agents/praisonaiagents/agent/agent.py#L1904">
  `praisonaiagents/agent/agent.py` at line 1904
</Card>
