Skip to main content
Infrastructure costs can spiral out of control without regular oversight. Idle resources, oversized instances, forgotten test environments, and unattached volumes quietly drain budgets month after month. Most engineering teams know they should review costs regularly, but manual audits are time-consuming and often get deprioritized during busy sprints. What makes cost optimization particularly challenging is the need for consistent, periodic review. One-time audits catch current waste but don’t prevent future problems. You need ongoing monitoring, but dedicating team time to monthly cost reviews is difficult to maintain. The investigation follows predictable patterns: identify underutilized resources, check for orphaned volumes, review instance sizing, and flag opportunities for savings. In this example, we’ll show how to create a scheduled SRE agent using Unpage that automatically generates monthly cost optimization reports, identifying savings opportunities in your AWS infrastructure without requiring manual effort.

Why Schedule This Agent?

Unlike incident response agents that react to alerts, cost optimization benefits from proactive, scheduled analysis:
  • Consistent monitoring: Runs automatically on the same schedule every month
  • No input needed: Gathers data directly from your infrastructure using tools
  • Actionable reports: Identifies specific resources and potential savings
  • No alert fatigue: Generates insights on your schedule, not random alert timing
Scheduled agents are perfect for “background chores” like cost analysis, security audits, and compliance checks that should happen regularly but don’t require immediate response.

Creating A Scheduled Cost Optimization Agent

Let’s create an agent that runs on the 1st of every month, analyzes your AWS infrastructure, and generates a report of cost optimization opportunities. After installing Unpage, create the agent by running:
A YAML file will open in your $EDITOR. Paste the following agent definition:
Let’s break down each section:

Description: What the agent does

The description explains the agent’s purpose. For scheduled agents, this helps you remember what each agent does when reviewing your list of agents.

Schedule: When the agent runs

The schedule section defines when the agent runs automatically. Unpage supports multiple cron formats: Standard 5-field format (minute precision):
Extended 6-field format (second precision):
Convenient aliases:
Common patterns:
  • "0 9 1 * *" - Monthly on the 1st at 9 AM
  • "0 9 * * 1" - Weekly on Mondays at 9 AM
  • "0 0 * * *" - Daily at midnight
  • "0 */6 * * *" - Every 6 hours
  • "*/2 * * * * *" - Every 2 seconds (6-field format)
All schedules use UTC timezone. See the schedule command documentation for more details.

Prompt: What the agent should do

The prompt contains detailed instructions for the agent. Since scheduled agents receive no input payload, the prompt must specify:
  • How to get data: Use graph and metrics tools to discover resources
  • What to analyze: Clear criteria for identifying cost optimization opportunities
  • Output format: Structure for the report
  • What action to take: Create a PagerDuty incident with findings

Tools: What the agent can access

The tools section grants permissions to specific infrastructure tools:
  • Graph tools: Discover and query infrastructure resources
  • Metrics tools: Analyze resource utilization over time
  • AWS tools: Get detailed information about EC2 instances and volumes
  • PagerDuty tools: Create incidents to deliver the report
Use wildcards (aws_*) to grant access to all tools from a plugin. To see all available tools:

Setting Up Your Infrastructure Graph

For the agent to discover AWS resources, you need to build your infrastructure knowledge graph:
The graph should be rebuilt periodically to stay up-to-date. You can run unpage graph build --interval 3600 to rebuild every hour. See the knowledge graph documentation for details.

Running The Scheduler

To start the scheduler and have your agent run automatically on its schedule:
This starts a daemon that:
  1. Loads all agents with schedule configurations
  2. Sets up cron jobs for each scheduled agent
  3. Runs agents automatically according to their schedules
  4. Logs output of each agent run
The scheduler runs in the foreground. Press Ctrl+C to stop it.

Testing Before Scheduling

Before relying on the schedule, test your agent manually:
This runs the agent with no payload (same as the scheduled run) and shows you the output. Use this to verify your agent works before putting it on a schedule.

Example Output

When your scheduled agent runs, it will analyze your infrastructure and create a PagerDuty incident with findings like:
The incident is created with low priority, so it doesn’t wake anyone up but ensures the team reviews the recommendations.

Production Deployment

For production use, run the scheduler as a persistent service:

Using systemd (Linux)

Create /etc/systemd/system/unpage-scheduler.service:
Enable and start:

Using Docker

See the deployment guide for comprehensive production setup instructions.

More Scheduled Agent Ideas

Once you have the scheduler running, consider adding more scheduled agents:

Security Audits

Unused Resource Cleanup

Performance Trend Analysis

Compliance Checks

Best Practices

When creating scheduled agents:
  1. Test thoroughly: Run manually several times before relying on the schedule
  2. Start conservative: Begin with weekly or monthly schedules, not daily
  3. Set clear outputs: Have agents create incidents or send notifications with findings
  4. Monitor execution: Check logs regularly to ensure agents are working as expected
  5. Include timestamps: Have agents log when they started and what period they analyzed
  6. Handle failures gracefully: Design prompts to continue even if some data is unavailable
  7. Document expectations: Note in prompts what data sources are required

Troubleshooting

Agent not running on schedule

Check the scheduler logs:
You should see messages like:

Agent failing during execution

Run manually to see errors:
Enable debug mode for more details:

Schedule not triggering

Verify cron expression at crontab.guru. Remember schedules use UTC timezone.

Conclusion

Scheduled agents enable proactive infrastructure management without manual overhead. By running cost optimization, security audits, and compliance checks automatically, you can maintain infrastructure health while freeing up team time for higher-value work. The cost optimization example shows how scheduled agents gather data, analyze patterns, and deliver actionable insights—all on autopilot. Apply this pattern to any repetitive operational task that benefits from consistent execution.