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
-
A
BackgroundRunnermanages daemon threads. -
The agent dispatches long-running commands and continues.
-
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
| Component | Before (s12) | After (s13) |
|---|---|---|
| Execution | Blocking only | Background + notification |
| Concurrency | None | Daemon threads |
| Runtime | Single lane | Task desc vs runtime slot |
Try It
cd learn-claude-code
python agents/s13_background_tasks.py
Run the full test suite in the background and check laterStart a long build, then check its status halfway throughSubmit 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.