# How to Stop Your PC Sleeping During Long AI Agent Runs

> Source: <https://dev.to/codexam/how-to-stop-your-pc-sleeping-during-long-ai-agent-runs-23jc>
> Published: 2026-11-12 21:46:37+00:00

By Subham Maity · November 13, 2026

Download from: [https://github.com/Subham-Maity/agent-power-guard](https://github.com/Subham-Maity/agent-power-guard)

**Quick answer:** Use a sleep guard that checks whether work is still running, not a timer. Agent Power Guard, a free open-source Rust tool for Windows, Linux and macOS, keeps your PC awake while Claude Code, Cursor or a long build runs, then lets it sleep shortly after the work ends.

Agent Power Guard keeps a PC awake only while real work is running, which fixes the two usual failures: sleeping mid-task and staying awake all night.

Long agent runs make this problem obvious. A tool like Claude Code can work for an hour without a single keystroke, and the operating system sees an empty desk.

An operating system measures one thing: how long since your last keystroke or mouse move. A coding agent writes files, runs tests and calls APIs without touching either, so the OS assumes you left.

A program can tell Windows otherwise. Microsoft’s [SetThreadExecutionState documentation](https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate) says the program must keep calling the function to hold the system awake, because a call without `ES_CONTINUOUS` only resets the idle timer once. If the tool never makes that call, the machine sleeps on schedule.

People reach for three fixes, and each fails differently:

`cargo build`, `npm install` or a fine-tuning run.
Windows can list those requests with a built-in command, documented in the [powercfg reference](https://learn.microsoft.com/windows-hardware/design/device-experiences/powercfg-command-line-options):

```
# What this shows: the Windows command that lists apps and drivers blocking sleep
powercfg /requests
```

The command needs an elevated prompt, and it says nothing about whether real work is running. In practice, this means you need something that watches the work itself, not the clock.

**Key Takeaway:** PCs sleep during AI agent runs because the OS measures keyboard and mouse idle time, not work. A fixed timer, a keep-awake toggle and a stuck power request each fail in a different way.

Agent Power Guard replaces the timer with a watchdog that samples the machine every second. It tracks 11 conditions: you typing, an agent compiling, a call in progress, a movie playing and a model running on the GPU. The PC suspends only when every enabled condition is quiet.

The tool treats different activity differently. Calls in Zoom, Teams, Meet and Discord stay protected even when you are muted. A podcast can turn the screen off without killing the audio. An idle VS Code window does not block sleep, but a `cargo` worker it spawned does.

The part I like most is the spike filter. A one-second burst counts as one sample out of 15, and a metric needs 8 of 15 seconds over its threshold to count as busy. It clears again at 2 of 15 or fewer, so it never flaps.

In practice, this means a compile that alternates between full load and idle keeps the PC awake, while a one-second indexer burst does not.

When something does block sleep, the app tells you exactly what:

```
{ "condition": 5, "name": "Disk I/O", "who": "cargo.exe", "value": "14.2 MB/s", "since_secs": 18 }
```

A Block History tab then answers the question “what kept my PC awake last night?” I prefer a tool that names the blocker over one that just refuses to sleep.

**Key Takeaway:** Agent Power Guard checks 11 conditions every second and uses an 8-of-15-second duty-cycle filter, so real work holds the PC awake and one-second spikes do not.

The lowest-effort path is a process wrapper. It ties a sleep lease to the command you run, so nothing depends on the agent remembering to release it:

```
# What this shows: wrapping long tasks so the PC stays awake until they exit
agent-power-guard run -- claude
agent-power-guard run -- cargo test --workspace
agent-power-guard run --max-mins 120 --reason "LoRA fine-tuning" -- python train.py
```

If the task crashes, the lease is released within 1 to 2 seconds. The PC then follows its normal countdown and sleeps.

The second path lets the agent decide. `agent-power-guard mcp` is a stdio Model Context Protocol server, which means the client launches it as a subprocess and exchanges JSON-RPC messages over stdin and stdout, as the [MCP stdio transport specification](https://modelcontextprotocol.io/specification/latest/basic/transports/stdio) describes. It exposes four tools: `keep_awake`, `release`, `status` and `postpone`.

Claude Code users can register it with one line: `claude mcp add agent-power-guard -- agent-power-guard mcp`. Cursor and Antigravity accept the same config entry, and the [agent integration guide](https://github.com/Subham-Maity/agent-power-guard/blob/main/docs/agents.md) shows each one.

A hung agent cannot hold the machine forever. If a lease tied to a process sits near 0% CPU and disk, Agent Power Guard flags it as idle and alerts you before the optional expiry. I would start with the wrapper and add MCP only when you want the agent to request time itself.

**Key Takeaway:** The run wrapper needs zero configuration and releases the lease when the process exits, while the MCP server lets Claude Code, Cursor and Antigravity request and release leases themselves.

The RAM cost is small. Agent Power Guard uses under 15 MB in normal use and about 5 MB once trimmed to the system tray, while a typical Electron utility commonly uses 100 MB or more. I chose Rust and egui for that reason, because a tool that guards power should not waste it.

The privacy cost is zero by default. The app sends no telemetry, and outbound requests go only to notification destinations you configure and, if you enable it, GitHub Releases. The local API binds to 127.0.0.1 and requires a 256-bit token.

Under the hood, Best Performance mode takes native wake locks. Windows uses `SetThreadExecutionState`, macOS uses `caffeinate`, and Linux uses a systemd inhibitor lock, which the [systemd inhibitor lock documentation](https://www.freedesktop.org/wiki/Software/systemd/inhibit) describes.

The trust cost is real, and I would rather you hear it from me. Release binaries are unsigned, so Windows Defender, SmartScreen or Bitdefender may warn about them. Adding an antivirus exclusion for the install folder fixes it, and `SHA256SUMS.txt` on each release lets you verify your download.

**Key Takeaway:** Agent Power Guard costs under 15 MB of RAM and sends no telemetry, but its unsigned binaries can trigger antivirus warnings.

If you want it, the source and full README are on [GitHub](https://github.com/Subham-Maity/agent-power-guard). It is MIT-licensed, and issues and pull requests are welcome.

**Key Takeaway:** The 15s Test preset shows the full sleep cycle in under a minute, so you can trust the 3-minute default before leaving a real task running.

`agent-power-guard run --`, or register the MCP server so Claude Code, Cursor or Antigravity can request leases.
**How do I keep my computer awake while Claude Code runs?**

Start Claude Code through the Agent Power Guard run wrapper by typing agent-power-guard run -- claude. The wrapper holds a sleep lease tied to the process, so the PC stays awake while Claude Code works. If the process crashes, the lease is released within 1 to 2 seconds and the normal countdown resumes.

**Is Agent Power Guard free and open source?**

Agent Power Guard is free and released under the MIT license. The full source is on GitHub, and CI builds every pull request on Windows x64, Linux x86_64 and macOS Apple Silicon. Good first contributions include new agent profiles, ignore-list defaults for noisy apps, Linux and macOS probe improvements, translations and packaging manifests.

**Which operating systems does Agent Power Guard support?**

Agent Power Guard supports Windows 10 and 11, Linux on X11 and Wayland, and macOS on Apple Silicon and Intel. Each release ships a Windows MSI and portable EXE, a Linux tar.gz and deb package, and macOS archives plus a universal app bundle. Package manifests exist for winget, Scoop, Chocolatey, Homebrew and the AUR.

**Will Agent Power Guard sleep my PC during a video call?**

Calls in Zoom, Teams, Meet and Discord stay protected even when you are muted, because the tool checks microphone and camera use. Creative apps such as Premiere, Resolve, Audacity and OBS block sleep only while real audio or camera capture is happening. A recording app sitting in standby does not keep the PC awake.

**Can Agent Power Guard handle laptops on battery?**

Agent Power Guard detects whether a laptop is on AC power or battery. On battery it switches to a shorter idle timeout, 60 seconds by default, and a tighter CPU limit of 1.0 percent. An emergency low-battery sleep also forces sleep or hibernate below a configurable cutoff, which defaults to 15 percent.

**How do I see what kept my PC awake overnight?**

Open the Block History tab. It ranks blockers by cumulative hold time, shows a Last Night summary for the overnight 12-hour window, and lists each event with its start time, duration and primary condition. You can export the history as CSV or JSON in one click, and the data stays in a local file.

If Agent Power Guard saves you a night of lost work, a [star on GitHub](https://github.com/Subham-Maity/agent-power-guard) helps other developers find it. You can also [download a release](https://github.com/Subham-Maity/agent-power-guard/releases) or [report an issue](https://github.com/Subham-Maity/agent-power-guard/issues).
