Introduction
In this tutorial, you'll learn how to create a persistent AI agent that can work proactively and continuously until explicitly stopped. This builds on the concept of AI agents that can maintain state and continue operating, similar to what OpenAI is developing with Codex. We'll create a Python-based AI agent that can monitor a directory for changes, process files automatically, and maintain its state between operations.
Prerequisites
- Python 3.7 or higher installed
- Basic understanding of Python programming
- Knowledge of AI/ML concepts and APIs
- Installed packages:
watchdog,openai,python-dotenv
Step-by-Step Instructions
1. Set Up Your Development Environment
First, create a new Python project directory and set up a virtual environment to isolate your dependencies.
mkdir persistent-ai-agent
cd persistent-ai-agent
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
This ensures you have a clean environment for our AI agent project without affecting your system-wide Python packages.
2. Install Required Dependencies
Install the necessary Python packages for our AI agent to work with file monitoring and OpenAI APIs.
pip install watchdog openai python-dotenv
The watchdog library will help us monitor file system changes, while openai provides access to OpenAI's API for AI processing capabilities.
3. Create Your OpenAI API Configuration
Create a .env file in your project directory to store your API key securely.
OPENAI_API_KEY=your_openai_api_key_here
Never commit your API keys to version control. This approach keeps your credentials secure and separate from your code.
4. Initialize the AI Agent Class
Create a Python file called ai_agent.py and define the core agent structure:
import os
import time
from dotenv import load_dotenv
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import openai
load_dotenv()
class PersistentAIHandler(FileSystemEventHandler):
def __init__(self):
self.running = True
self.processed_files = set()
openai.api_key = os.getenv('OPENAI_API_KEY')
def on_created(self, event):
if not event.is_directory and event.src_path not in self.processed_files:
print(f'Processing new file: {event.src_path}')
self.process_file(event.src_path)
self.processed_files.add(event.src_path)
def process_file(self, file_path):
try:
# Read file content
with open(file_path, 'r') as file:
content = file.read()
# Process with OpenAI API
response = openai.Completion.create(
engine='text-davinci-003',
prompt=f'Analyze the following code and provide a summary:\n\n{content}',
max_tokens=150
)
# Save analysis to a results file
analysis_path = f'{file_path}_analysis.txt'
with open(analysis_path, 'w') as file:
file.write(response.choices[0].text.strip())
print(f'Analysis saved to {analysis_path}')
except Exception as e:
print(f'Error processing file {file_path}: {str(e)}')
def stop(self):
self.running = False
print('AI Agent stopping...')
This class sets up the basic structure of our persistent agent with the ability to monitor file changes and process them using OpenAI's API.
5. Implement the Main Agent Loop
Create the main execution loop that keeps the agent running and monitoring:
import time
from ai_agent import PersistentAIHandler
from watchdog.observers import Observer
if __name__ == '__main__':
# Initialize the handler
handler = PersistentAIHandler()
# Set up file monitoring
observer = Observer()
observer.schedule(handler, path='.', recursive=False)
# Start monitoring
observer.start()
print('AI Agent started. Monitoring directory for changes...')
try:
# Keep the agent running
while handler.running:
time.sleep(1)
except KeyboardInterrupt:
print('Received interrupt signal')
# Clean shutdown
observer.stop()
handler.stop()
observer.join()
print('AI Agent stopped successfully')
This main loop keeps our agent running continuously, monitoring for file changes and processing them as they occur. The while handler.running loop ensures our agent continues until explicitly stopped.
6. Test Your Persistent AI Agent
Create a test file in your project directory to verify the agent works:
print('Hello, World!')
# This is a test file for our AI agent
x = 10
y = 20
result = x + y
print(f'The sum is: {result}')
Save this as test.py and run your agent script. The agent should detect the new file and process it using OpenAI's API, creating an analysis file with the results.
7. Add Graceful Shutdown Handling
Enhance your agent to handle shutdown signals gracefully:
import signal
import sys
# Add this to your main loop
signal.signal(signal.SIGINT, lambda sig, frame: handler.stop())
signal.signal(signal.SIGTERM, lambda sig, frame: handler.stop())
This ensures your agent can be stopped cleanly using Ctrl+C or system termination signals, maintaining proper resource cleanup.
Summary
In this tutorial, you've built a persistent AI agent that monitors file changes and processes them automatically using OpenAI's API. The agent maintains its state between operations and can be stopped gracefully when needed. This demonstrates core concepts of persistent AI agents similar to what OpenAI is developing with Codex, where the system continues working proactively until explicitly put to sleep. The agent uses file system monitoring to detect changes, processes files through AI APIs, and maintains a clean shutdown mechanism.
Key concepts learned include: persistent state management, file system monitoring, API integration, and graceful shutdown handling. This foundation can be extended to create more sophisticated AI agents that perform continuous monitoring and proactive tasks.



