Run slow operations in the background; the agent keeps thinking

What You’ll Learn

  • Why the agent shouldn’t block on slow operations
  • How to implement background execution with notification
  • How to separate task description from runtime slot

The Problem

The agent runs a test suite that takes 5 minutes. Meanwhile, the agent is frozen — can’t plan the next step, can’t fix the previous bug. Time is wasted.

The Solution

Background execution: spawn slow commands in separate threads, notify the agent when they finish.

Main loop                      Background runner
+------------------+           +------------------+
| waiting for      |           |                  |
| test results...  |  spawn    | pytest           |
|                  |---------->|                  |
| [meanwhile,      |           | running...       |
|  plan next step] |           |                  |
|                  |  notify   | done (42 passed) |
| result arrives   |<----------|                  |
+------------------+           +------------------+

How It Works

  1. A BackgroundRunner manages daemon threads.

  2. The agent dispatches long-running commands and continues.

  3. Results are injected back via a notification queue.

class BackgroundRunner:
    def __init__(self):
        self.jobs = {}

    def submit(self, job_id: str, command: str):
        thread = threading.Thread(
            target=self._run, args=(job_id, command),
            daemon=True)
        self.jobs[job_id] = {"thread": thread, "result": None}
        thread.start()

    def check(self, job_id: str) -> str:
        job = self.jobs.get(job_id)
        if job and job["result"] is not None:
            return job["result"]
        return "still running..."

What Changed From s12

ComponentBefore (s12)After (s13)
ExecutionBlocking onlyBackground + notification
ConcurrencyNoneDaemon threads
RuntimeSingle laneTask desc vs runtime slot

Try It

cd learn-claude-code
python agents/s13_background_tasks.py
  1. Run the full test suite in the background and check later
  2. Start a long build, then check its status halfway through
  3. Submit 3 background jobs and list their progress

Key Takeaway

Background execution is a runtime lane, not a second main loop. The agent dispatches, continues working, and receives results asynchronously.