OpperatorAgent base class and implement lifecycle methods that control behavior during startup, operation, and shutdown.
Minimal Agent
Lifecycle Methods
Your agent goes through several lifecycle stages. Understanding these helps you initialize resources at the right time and clean up properly.initialize()
Setup phase called beforestart(). Use this for:
- Loading configuration
- Setting up initial state
- Configuring the agent description
Don’t start background threads or long-running operations here. Save those for
start().start()
Activation phase called afterinitialize(). Use this for:
- Registering commands
- Opening connections
- Starting background tasks
main_loop()
Override for custom event loop (optional). Default waits for shutdown signal.Most agents don’t need a custom
main_loop(). Commands and event handlers usually provide enough control.on_shutdown()
Graceful cleanup when SIGTERM or SIGINT (Ctrl+C) received.Should complete quickly (< 5 seconds). Not guaranteed to finish before termination.
cleanup()
Final cleanup. Always runs before exit, even on crash.CRITICAL: Always call
super().cleanup() or async executors won’t shut down.For persisting agent state across restarts, see State Management.
Agent Properties
| Property | Type | Description |
|---|---|---|
self.running | bool | Agent should continue operating. True when running, False on shutdown. |
self.config | dict | Configuration from agents.yaml. Available after load_config(). |
self.name | str | Agent identifier |
self.version | str | Agent version |
Logging
DEBUG, INFO, WARNING, ERROR, FATAL
Core Agent Methods
set_system_prompt(prompt)
Set or update the system prompt that guides the LLM’s behavior when interacting with your agent. For advanced system prompt patterns and dynamic updates, see System Prompts.get_working_directory() / get_invocation_directory()
Access the agent’s working directory or the user’s invocation directory:get_working_directory()- Returns where the agent process is runningget_invocation_directory()- Returns where the user ranopfrom (may beNoneif not available)
op was invoked. Agents can fetch this via get_invocation_directory() and receive on_invocation_directory_changed() events when it changes.
set_description(description)
Set a human-readable description of what your agent does.get_secret(name, timeout=5.0)
Securely retrieve secrets without hardcoding them in your agent.Never log secrets.
report_progress(call_id, percentage, message, metadata)
Report progress for async commands withasync_enabled=True.
register_section(section_id, title, content) / update_section(section_id, content)
Add live sidebar sections for displaying dynamic status information.See Custom sidebars for comprehensive sidebar customization options.
Signal Handling
| Signal | Trigger | Handler | Platform | Notes |
|---|---|---|---|---|
SIGTERM | kill <pid> | on_shutdown() | All | Graceful shutdown |
SIGINT | Ctrl+C | on_shutdown() | All | Same as SIGTERM |
SIGHUP | kill -HUP <pid> | on_config_update() | Unix/Linux | Config reload |
SIGUSR1 | kill -USR1 <pid> | on_status() | Unix/Linux | Health check |
Event Handlers
Beyond the core lifecycle methods, agents can respond to additional events: Protocol Events (sent by Opperator daemon):on_new_conversation()- New conversation createdon_conversation_switched()- User switched conversationson_conversation_deleted()- Conversation deletedon_agent_activated()- Agent becomes activeon_agent_deactivated()- Another agent becomes activeon_invocation_directory_changed()- User’s working directory changed
on_config_update()- SIGHUP received, config reloadedon_status()- SIGUSR1 received, health check requested
See Lifecycle events for detailed usage and examples.