Agentic Autonomous Workflow
Learn how to create AI agents that can autonomously monitor, act, and adapt based on environment feedback.
from praisonaiagents import Agent
agent = Agent(name="planner", instructions="Act autonomously until the task is done.")
agent.start("Monitor the queue and clear stuck jobs.")The user sets a goal; the agent loops on environment feedback until it stops or hits a completion signal.
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
- Install Package
First, install the PraisonAI Agents package:
bash pip install praisonaiagents - Set API Key
Set your OpenAI API key as an environment variable in your terminal:
bash export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}" - Create a file
Create a new file
app.pywith the basic setup:python 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]}...") - Start Workflow
Type this in your terminal to run your workflow:
bash python app.py
How It Works
| Phase | What happens |
|---|---|
| 1. Monitor | The monitor agent reads the current environment state |
| 2. Act | route() dispatches the matching action agent |
| 3. Adapt | Feedback feeds back into the loop via context= |
| 4. Stop | The loop ends when the until condition is met |
Choosing an Autonomy Level
Decide how much the workflow runs without a human in the loop.
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
Continuously monitor and analyze environment state.
Adaptive ActionsExecute context-aware actions based on state analysis.
Feedback ProcessingProcess and learn from action outcomes.
Self-OptimizationImprove 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
# 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
If monitoring fails: – Check environment access – Verify state detection – Enable verbose mode for debugging
Adaptation FlowIf adaptation is incorrect: – Review feedback processing – Check action outcomes – Verify learning loop
Best Practices
Keep monitor instructions deterministic
The monitor agent should return only discrete states (normal, critical, optimal). Vague prose breaks route() matching and causes wrong action agents to run.
Cap repeat iterations
Always set max_iterations on repeat() loops. Without a ceiling, a misconfigured until condition can spin indefinitely and burn tokens.
Cap the spend on unattended workflows
For run_autonomous flows, set max_budget_usd and max_tokens so an unattended run can’t burn unlimited money or tokens. See Autonomy Budget.
Wire feedback into the loop
Pass prior task outputs via context=[...] on the feedback step so the agent learns from outcomes rather than re-analysing from scratch each cycle.
Start with verbose logging
Enable verbose output while tuning routes and exit conditions. Once the workflow stabilises, reduce logging for production runs.
Next Steps
Learn about automatically created and managed AI agents
Mini AgentsExplore lightweight, focused AI agents