---
title: "Monitor Tool: Event Streaming from Background Scripts"
description: "The Monitor tool streams stdout from a background process back to Claude line-by-line, eliminating polling loops for deploy logs, CI status, and file watchers."
tags:
  - claude
  - agent-design
  - observability
aliases:
  - Monitor tool
  - background script monitoring
  - agent event streaming
applies_to: "claude-code@2.x"
last_reviewed: 2026-10-31
status: current
---

# Monitor Tool: Event Streaming from Background Scripts

> Stream stdout from a background process directly to Claude — each output line arrives as a notification, no polling required.

The [Monitor tool](https://code.claude.com/docs/en/tools-reference#monitor-tool), added in Claude Code v2.1.98 (April 9, 2026), lets Claude watch a long-running process and react to its output mid-conversation. Claude writes a small watch script, runs it in the background, and receives each stdout line as it arrives. The conversation continues normally; Claude interjects only when an event lands.

## Push vs poll

Before Monitor, watching a background process meant using `Bash` with `run_in_background: true` and polling for results. Background tasks write output to a file; the agent calls `Read` to check progress. [Source: [Claude Code Tools Reference](https://code.claude.com/docs/en/tools-reference)]

```text
Bash (run_in_background: true) → agent calls Read on output file to check progress
```

Monitor inverts this. It pushes each stdout line to Claude as a notification:

```text
Monitor → each line arrives as a notification → Claude reacts immediately
```

The polling loop disappears. The agent handles other work and responds to events as they arrive.

## Use cases

| Scenario | What to watch |
|----------|--------------|
| Deploy monitoring | Tail deploy logs, flag errors or completion |
| CI status | Watch a `gh run list` poller until a run finishes |
| File watching | Watch a directory for build artifacts |
| Test suite streaming | Stream test runner output, surface failures as they occur |
| Long-running scripts | Any process that emits progress lines over time |

## Starting and stopping a monitor

Ask Claude in natural language — no special syntax required:

```text
Watch the deploy log at /var/log/deploy.log and tell me if any errors appear.
```

```text
Poll the GitHub Actions run for this branch every 30 seconds and report when it finishes.
```

Claude writes the watch script internally and starts it. To stop, ask Claude to cancel the monitor, or end the session. All monitors are session-scoped: they stop when the session exits.

## Permissions and availability

Monitor uses the same [`allow` and `deny` permission rules as Bash](https://code.claude.com/docs/en/permissions#tool-specific-permission-rules). Any pattern you set for Bash applies to Monitor automatically.

On Bedrock, Vertex AI, Foundry, or with `DISABLE_TELEMETRY` or `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` set, Monitor requires Claude Code v2.1.310 or later. [Source: [Claude Code Tools Reference](https://code.claude.com/docs/en/tools-reference#monitor-tool)]

## Monitor vs scheduled polling

Both Monitor and [session scheduling](session-scheduling.md) handle recurring background activity, but they differ in mechanism:

| | Monitor | CronCreate / /loop |
|--|---------|-------------------|
| Trigger | Each stdout line from a process | Timer interval |
| Cadence | Continuous stream | Minimum 1 minute |
| Script lifecycle | Managed by Monitor | Re-runs Claude each fire |
| Best for | Streaming logs, file watchers | Periodic status checks |

Use Monitor when the process already emits events and you want zero-latency reactions. Use scheduling when you want Claude to run a fresh check on a timer.

## When this backfires

Push beats poll when events are rare and meaningful. It loses when any of the following hold:

- Chatty processes flood context, and Monitor will stop them. Every stdout line becomes a notification. Watching a verbose test runner, webpack rebuild, or `pytest -vv` pipes thousands of events into the conversation, and Monitor automatically stops any monitor that produces too many events. Restart it with a tighter filter if that happens. [Source: [Monitor tool description](https://github.com/Piebald-AI/claude-code-system-prompts/blob/main/system-prompts/tool-description-background-monitor-streaming-events.md)] Pipe through `grep` before monitoring, or capture to a file and `Read` on demand.
- Pipe buffering silently delays events. When piping to `grep` or similar filters, use `grep --line-buffered`. Without it, standard block buffering holds output until a ~4 KB chunk fills, which can delay events by minutes on low-traffic streams. [Source: [Monitor tool description](https://github.com/Piebald-AI/claude-code-system-prompts/blob/main/system-prompts/tool-description-background-monitor-streaming-events.md)]
- Stderr and exit codes are invisible. Monitor forwards stdout only. A script that logs errors to stderr, crashes silently, or communicates via a non-zero exit status will look healthy to Monitor. Use `Bash` with captured output for anything where failure is silent.
- Outcome filters must match every terminal state. When you watch a job for a result, the filter has to match both success and failure signals. Otherwise a failed run produces silence, which Claude reads as "still running". [Source: [Monitor tool description](https://github.com/Piebald-AI/claude-code-system-prompts/blob/main/system-prompts/tool-description-background-monitor-streaming-events.md)]
- Low-cadence checks do not need streaming. If the thing you watch changes every few minutes (a slow CI run, a nightly build), scheduled polling with `CronCreate` or `/loop` is just as responsive, does not hold a background process open, and survives across idle windows.
- Session-scoped lifetime. Monitors die when the Claude session exits. For batch jobs that need to outlive a conversation, or monitoring across reboots, use an external supervisor (systemd, cron, CI) instead.
- Builds before v2.1.310 lack Monitor on Bedrock, Vertex AI, Foundry, and with telemetry or nonessential traffic off. [Source: [Claude Code Tools Reference](https://code.claude.com/docs/en/tools-reference#monitor-tool)]

## Example

Monitor a Docker Compose deployment and surface errors the moment they appear:

```text
Run `docker compose up --build` in the background and monitor its output.
Flag any lines containing "error" or "failed" immediately, and tell me when
all services report "healthy".
```

Claude starts `docker compose up --build`, monitors its stdout, and interjects on error lines or when the health check passes — without blocking the conversation or requiring manual polling.

## Key Takeaways

- Monitor pushes each stdout line to Claude as a notification; `Bash run_in_background` requires the agent to poll manually
- Session-scoped: monitors stop when the session exits
- Uses the same permission rules as Bash — no separate configuration required. Hook `if` conditions are the exception: they name one tool each, so Monitor needs [its own rule](../../patterns/agent-design/shell-surface-hook-matcher-coverage.md)
- On Bedrock, Vertex AI, Foundry, or with telemetry or nonessential traffic disabled, Monitor needs v2.1.310 or later

## Related

- [Plugin Background Monitors](plugin-background-monitors.md)
- [Wait Primitive Plus Shell-Sleep Deny](../../patterns/agent-design/wait-primitive-shell-sleep-deny.md) — why the 2.1.310 release paired Monitor with a sleep block
- [Session Scheduling](session-scheduling.md)
- [MCP Tasks in Claude Code: Background Calls and When to Wait](mcp-tasks-background.md) — the other way a long job runs in the background
- [Hooks & Lifecycle](hooks-lifecycle.md)
- [/batch & Worktrees](batch-worktrees.md)
- [Observability](../../observability/index.md)
