Video 21.4: Foreground vs Background Execution
Course: Claude Code - Parallel Agent Development | Section: 21. Subagents for Parallel Work | Length: 4 minutes | Presenter: Daniel Treasure
Opening Hook
Here's a powerful workflow pattern: you're refactoring code in one subagent while running tests in another, completely in parallel. Both are happening without blocking you. The secret? Understanding the difference between foreground and background subagent execution. One blocks your conversation (foreground), the other runs concurrently without interrupting you (background). In this video, you'll learn which to use when, and why background mode is secretly where subagents shine.
Key Talking Points
What to say:
Foreground Execution (Default) - Blocks: You wait for the subagent to finish before continuing - Permissions: Asks you for approval on sensitive actions - Interactivity: You can ask follow-up questions, request changes mid-work - Good for: Tasks where you need to guide, validate, or iterate - Feedback: You see results immediately and can course-correct - Example: "Refactor this function" — you want to see it, maybe ask for adjustments - How to trigger: Just ask normally, subagent runs in foreground by default
Background Execution (Ctrl+B) - Non-blocking: Subagent runs concurrently, you keep working - Permissions: Pre-approved (you can't approve/deny mid-execution) - Interactivity: Limited — agent can't ask you questions, must be self-directed - Good for: Long-running tasks, tests, builds, anything you don't need to monitor - Feedback: Results appear when ready, but you've been productive meanwhile - Example: "Run the full test suite" — it runs while you continue coding - How to trigger: Use Ctrl+B when starting the subagent request
Permission Modes Comparison - Foreground: Permission prompts pass to you (you approve/deny) - Background: Permissions are pre-approved based on agent configuration - This means background agents need clear tool restrictions and safety guardrails
Failure Handling: The Critical Difference - Foreground: Failures interrupt you. You see the error and can decide what to do. - Background: Failures don't block your work. Agent reports what went wrong in its summary. - You can check on background agents anytime, but they won't interrupt if something goes sideways.
When to Use Foreground 1. Complex, interactive tasks (refactoring, architecture design) 2. Tasks where feedback improves the output 3. Anything requiring approval or decision-making mid-process 4. First-time tasks (you want to see what the agent does) 5. Security-sensitive work (you want to approve permissions explicitly)
When to Use Background 1. Tests, builds, linting (long-running, self-contained) 2. Documentation generation (doesn't need your input) 3. Code analysis or scanning (fire and forget) 4. Multiple parallel agents (you can't approve 3 permissions simultaneously in foreground) 5. Tasks where blocking you is worse than delayed results
The Sweet Spot: Parallel Background Pattern - Start 3-5 Bash agents in background (all running tests simultaneously) - Continue refactoring or implementing features in foreground - Check back when tests complete - Results are ready when you need them, but you weren't blocked - This is where subagents deliver the biggest productivity boost
What to show on screen:
- Keyboard shortcut visual: Show Ctrl+B prominently
- Create a visual reminder: "Ctrl+B = Background"
-
Show the shortcut working in real-time
-
Foreground execution demo: Start a simple task in foreground
- Type a request normally
- Show the conversation blocking while the subagent works
- Show results appearing in the chat
-
Show you can ask follow-up questions
-
Background execution demo: Start the same or similar task with Ctrl+B
- Show you typing the request with Ctrl+B active
- Show the agent starting in the background
- Continue working (type something else, navigate code, etc.)
-
Show the agent's results appearing separately in a sidebar or panel
-
Parallel background agents: Show 3+ background agents running
- Each one executing independently
- No blocking, no permission prompts
-
Multiple progress indicators or status panel
-
Failure scenario in foreground: Intentionally trigger an agent error
- Show the error appearing immediately
-
Show you being able to respond and retry
-
Failure scenario in background: Trigger a similar error in background
- Show the error logged, but you keep working
- Show you checking the results later
Demo Plan
Setup (30 seconds): - Open Claude Code with a real project that has tests - Have code you want to refactor ready - Have a test command ready to run - Show the Claude Code UI (prompt area, results area)
Foreground Demo: Refactoring (1 minute) 1. Say: "Let's start with foreground. I'll ask a subagent to refactor a function." 2. Type a clear refactoring request (no Ctrl+B) 3. Show the conversation blocking while the subagent works 4. When it returns results, review them on screen 5. Say: "Notice I had to wait. That's foreground—I'm blocked until it finishes." 6. Ask a follow-up: "Can you also add error handling?" (show interactivity) 7. Mention: "Foreground is great when you need to guide the work."
Background Demo: Testing (1 minute) 1. Say: "Now let's run tests in the background while I keep working." 2. Type a test request, but trigger it with Ctrl+B 3. Show the command going to background 4. Continue working on something else: "While that's running, let me refactor this other function" 5. Show you actively coding, typing, navigating—all while the background test runs 6. After ~30 seconds or when test completes, show the results appearing (sidebar, notification, or results panel) 7. Mention: "Background didn't block me. I was productive the whole time."
Parallel Background Agents (1.5 minutes) 1. Say: "The real power is parallel background execution." 2. Trigger 3-4 background tasks simultaneously: - "npm test" in Bash agent (background) - "npx eslint ." in another Bash agent (background) - "Generate API documentation" in General-Purpose (background) - "Analyze code for security issues" in custom agent (background) 3. Show all 4 starting without blocking 4. Show a status panel or progress view (if UI has this) 5. Continue working in the foreground (refactoring, implementing features) 6. As results come in, show them being collected 7. Conclusion: "All 4 ran concurrently. No blocking. This is productivity."
Permission Difference (30 seconds) 1. Say: "There's one critical difference: permissions." 2. Show a foreground agent asking for permission to do something 3. Show you approving it (pass-through) 4. Mention: "In background, those permissions are pre-approved. The agent must be trustworthy." 5. This is why background is best for well-tested, simple agents
Checking Background Results (30 seconds) 1. Show how to check on background agents mid-execution (if supported) 2. Show final results when ready 3. Mention: "You can always check back. Background doesn't mean fire-and-forget."
Code Examples & Commands
Foreground Execution (Default)
# Natural language (no special syntax)
"Refactor this authentication module to use OAuth 2.0"
# Subagent runs in foreground, blocks conversation
# You wait for results, can ask follow-ups
Background Execution (Ctrl+B)
# Press Ctrl+B before hitting Enter, then type request
[Ctrl+B pressed]
"Run the full test suite and report results"
# Subagent runs in background, doesn't block
# Results appear when ready
Triggering Multiple Background Agents
# Start them all in quick succession with Ctrl+B
[Ctrl+B] "npm test" # Background Bash agent 1
[Ctrl+B] "npx eslint ." # Background Bash agent 2
[Ctrl+B] "Generate API docs for all exported functions" # Background General-Purpose
[Ctrl+B] "Analyze for SQL injection vulnerabilities" # Background custom agent
# All 4 run in parallel. Continue working in foreground.
Explicit Agent Selection in Background
# Foreground: Explicit agent with results
"Use the Explore agent to find all database queries"
# Background: Same request, non-blocking
[Ctrl+B] "Use the Explore agent to find all database queries"
Pre-approving Permissions for Background Agents
# In your AGENT.md, use permissionMode for background safety
---
name: test-runner
description: Runs tests and reports results
tools: [Bash]
model: haiku
permissionMode: plan # Read-only for safety in background mode
---
Run test commands and summarize results clearly.
Gotchas & Tips
Gotcha: "Ctrl+B doesn't work in my editor" - Ctrl+B is the standard shortcut, but it might be overridden by your IDE - Check if your IDE has Ctrl+B mapped elsewhere - Alternative: There may be a UI button or menu option for background execution - Ask: Is Ctrl+B available in your Claude Code instance?
Gotcha: "Background agent is stuck. How do I stop it?" - Background agents run to completion. You can't interrupt them mid-execution. - Make sure your background tasks have reasonable timeouts - For long-running tests, use explicit timeout configs
Gotcha: "I lost track of my background agents. What are they doing?" - Look for a status panel or background job indicator in the UI - Background doesn't mean invisible. Results should be logged somewhere. - Check recent results/output to see what's been completed
Tip: Default to background for routine work - Tests, linting, builds → background - Documentation generation → background - Code analysis → background - Only use foreground when you need interactivity
Tip: Use foreground for exploration - First time implementing a pattern → foreground (learn what agent does) - After you trust the pattern → background (set it and forget it)
Tip: Combine foreground + background strategically - Foreground: You're doing important work (refactoring, design) - Background: Routine tasks (tests, linting, analysis) - Example: You refactor code in foreground while background runs full test suite
Tip: Group background agents by category - If you run 5 agents, label them mentally: "testing batch", "analysis batch", "doc batch" - Makes it easier to understand overall progress
Tip: Set clear expectations for background agents - Give them specific, self-contained tasks - Make sure they don't need you to approve mid-execution - Example: "Run tests" is good. "Implement feature X" is not (needs interaction)
Lead-out
You now understand when to run subagents in the foreground versus the background. In the final video of this section, we'll put it all together and explore the most powerful parallel research patterns—using multiple subagents simultaneously to investigate, analyze, and document at scale. You'll see the techniques in action.
Reference URLs
- Foreground vs Background Execution
- Background Agent Best Practices
- Permission Modes and Safety
- Parallel Execution Patterns
Prep Reading
- Docs: "Execution Modes" — understand the behavioral differences
- Examples: Review background-safe agent configurations
- Benchmark: Time a task in foreground vs. background (show the difference)
- Think: Which of your current workflows could use background agents?
Notes for Daniel
This video is about unlocking productivity. The key insight is: "While this is running, you can do something else." Make that feel revolutionary.
The Ctrl+B shortcut needs to be muscle memory for viewers. Show it multiple times, use it repeatedly in demos. Make it second nature.
The parallel agents demo is the "wow moment." Show 4 agents starting quickly in sequence, all running without blocking. This demonstrates the power of background mode.
Live-screen demonstration is critical here. Viewers need to see: - Foreground blocking and how it feels - Background starting and you continuing to work - Multiple background agents on the status panel - Results appearing as agents complete
Talk through failure handling clearly. Background agents failing doesn't crash your work—this is a safety feature, not a limitation.
Tone: You're showing a superpower. "This is how to get multiple things done at once without context-switching."
Consider showing side-by-side timer: foreground (you waiting) vs. background (you working). Visual proof of time saved.