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

# Agentic Autonomous Workflow

> Learn how to create AI agents that can autonomously monitor, act, and adapt based on environment feedback.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
flowchart LR
    Human[Human] <--> LLM[LLM Call]
    LLM -->|ACTION| Environment[Environment]
    Environment -->|FEEDBACK| LLM
    LLM --> Stop[Stop]
    
    style Human fill:#8B0000,color:#fff
    style LLM fill:#2E8B57,color:#fff
    style Environment fill:#8B0000,color:#fff
    style Stop fill:#333,color:#fff
```

An agent-based workflow where LLMs act autonomously within a loop, interacting with their environment and receiving feedback to refine their actions and decisions.

## Quick Start

<Steps>
  <Step title="Install Package">
    First, install the PraisonAI Agents package:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    pip install praisonaiagents
    ```
  </Step>

  <Step title="Set API Key">
    Set your OpenAI API key as an environment variable in your terminal:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    export OPENAI_API_KEY=your_api_key_here
    ```
  </Step>

  <Step title="Create a file">
    Create a new file `app.py` with the basic setup:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent, AgentFlow
    from praisonaiagents import route, repeat

    # Create monitor agent that analyzes environment state
    monitor_agent = Agent(
        name="Monitor",
        role="Environment Monitor",
        goal="Monitor and analyze system state",
        instructions="Analyze the current state. Respond with ONLY 'normal', 'critical', or 'optimal'."
    )

    # Create action agents for different states
    maintenance_agent = Agent(
        name="Maintenance",
        role="System Maintainer",
        goal="Maintain normal system operations",
        instructions="Perform routine maintenance tasks. Report what was done."
    )

    emergency_agent = Agent(
        name="Emergency",
        role="Emergency Responder",
        goal="Handle critical situations",
        instructions="Address the critical issue immediately. Provide resolution steps."
    )

    optimization_agent = Agent(
        name="Optimizer",
        role="System Optimizer",
        goal="Optimize system performance",
        instructions="The system is optimal. Suggest further improvements."
    )

    # Create feedback agent
    feedback_agent = Agent(
        name="Feedback",
        role="Feedback Processor",
        goal="Process and learn from feedback",
        instructions="Analyze the action taken and provide feedback for improvement."
    )

    # Check if optimal state reached
    def is_optimal(ctx) -> bool:
        return "optimal" in ctx.previous_result.lower()

    # Create autonomous workflow
    workflow = AgentFlow(
        steps=[
            repeat(
                monitor_agent,  # Monitor keeps checking state
                until=is_optimal,
                max_iterations=3
            ),
            route({
                "normal": [maintenance_agent],
                "critical": [emergency_agent],
                "optimal": [optimization_agent]
            }),
            feedback_agent
        ]
    )

    result = workflow.start("Monitor the AI system health")
    print(f"Result: {result['output'][:500]}...")
    ```
  </Step>

  <Step title="Start Workflow">
    Type this in your terminal to run your workflow:

    ```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    python app.py
    ```
  </Step>
</Steps>

<Note>
  **Requirements**

  * Python 3.10 or higher
  * OpenAI API key. Generate OpenAI API key [here](https://platform.openai.com/api-keys). Use Other models using [this guide](/models).
  * Basic understanding of Python
</Note>

## Understanding Autonomous Workflow

<Card title="What is Autonomous Workflow?" icon="question">
  Autonomous Workflow enables:

  * Continuous environment monitoring
  * Automated decision-making and action execution
  * Real-time feedback processing
  * Self-adapting behavior based on outcomes
</Card>

## Features

<CardGroup cols={2}>
  <Card title="Environment Monitoring" icon="eye">
    Continuously monitor and analyze environment state.
  </Card>

  <Card title="Adaptive Actions" icon="gears">
    Execute context-aware actions based on state analysis.
  </Card>

  <Card title="Feedback Processing" icon="rotate">
    Process and learn from action outcomes.
  </Card>

  <Card title="Self-Optimization" icon="arrow-trend-up">
    Improve performance through continuous learning.
  </Card>
</CardGroup>

## Configuration Options

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Create a monitor agent
monitor = Agent(
    name="Environment Monitor",
    role="State analyzer",
    goal="Monitor and analyze state",
    tools=[get_environment_state],  # Environment monitoring tools
      # Enable detailed logging
)

# Create an action agent
action = Agent(
    name="Action Executor",
    role="Action performer",
    goal="Execute appropriate actions",
    tools=[perform_action]  # Action execution tools
)

# Create monitoring task
monitor_task = Task(
    name="monitor_environment",
    description="Monitor environment state",
    agent=monitor,
    is_start=True,
    task_type="decision",
    condition={
        "normal": ["execute_action"],
        "critical": ["execute_action"],
        "optimal": "exit"
    }
)

# Create feedback loop task
feedback_task = Task(
    name="process_feedback",
    description="Process and adapt",
    agent=feedback_agent,
    next_tasks=["monitor_environment"],  # Create feedback loop
    context=[monitor_task, action_task]  # Access history
)
```

## Troubleshooting

<CardGroup cols={2}>
  <Card title="Monitoring Issues" icon="triangle-exclamation">
    If monitoring fails:

    * Check environment access
    * Verify state detection
    * Enable verbose mode for debugging
  </Card>

  <Card title="Adaptation Flow" icon="diagram-project">
    If adaptation is incorrect:

    * Review feedback processing
    * Check action outcomes
    * Verify learning loop
  </Card>
</CardGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="AutoAgents" icon="robot" href="./autoagents">
    Learn about automatically created and managed AI agents
  </Card>

  <Card title="Mini Agents" icon="microchip" href="./mini">
    Explore lightweight, focused AI agents
  </Card>
</CardGroup>

<Note>
  For optimal results, ensure your environment monitoring is reliable and your feedback processing logic is properly configured for effective adaptation.
</Note>
