Skip to main content
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

1

Install Package

First, install the PraisonAI Agents package:
pip install praisonaiagents
2

Set API Key

Set your OpenAI API key as an environment variable in your terminal:
export OPENAI_API_KEY=your_api_key_here
3

Create a file

Create a new file app.py with the basic setup:
from praisonaiagents import Agent, Workflow
from praisonaiagents.workflows 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 = Workflow(
    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]}...")
4

Start Workflow

Type this in your terminal to run your workflow:
python app.py
Requirements
  • Python 3.10 or higher
  • OpenAI API key. Generate OpenAI API key here. Use Other models using this guide.
  • Basic understanding of Python

Understanding Autonomous Workflow

What is Autonomous Workflow?

Autonomous Workflow enables:
  • Continuous environment monitoring
  • Automated decision-making and action execution
  • Real-time feedback processing
  • Self-adapting behavior based on outcomes

Features

Environment Monitoring

Continuously monitor and analyze environment state.

Adaptive Actions

Execute context-aware actions based on state analysis.

Feedback Processing

Process and learn from action outcomes.

Self-Optimization

Improve performance through continuous learning.

Configuration Options

# Create a monitor agent
monitor = Agent(
    name="Environment Monitor",
    role="State analyzer",
    goal="Monitor and analyze state",
    tools=[get_environment_state],  # Environment monitoring tools
    verbose=True  # 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

Monitoring Issues

If monitoring fails:
  • Check environment access
  • Verify state detection
  • Enable verbose mode for debugging

Adaptation Flow

If adaptation is incorrect:
  • Review feedback processing
  • Check action outcomes
  • Verify learning loop

Next Steps

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