Claude Skills Guide

Claude Code Tmux Session Management Multi Agent Workflow

When building sophisticated AI-powered development workflows with Claude Code, managing multiple concurrent agent sessions becomes essential. Tmux (terminal multiplexer) provides the infrastructure needed to spawn, monitor, and coordinate multiple Claude Code instances running in parallel. This approach transforms Claude Code from a single interactive assistant into a capable multi-agent orchestration system.

Why Tmux for Claude Code Multi Agent Workflows

Running Claude Code within tmux sessions offers several advantages over simple subprocess spawning. Tmux provides persistent terminal sessions that survive disconnection, window management for organizing agent workspaces, and built-in mechanisms for session-to-session communication. For developers building complex agentic systems, tmux creates a production-ready foundation.

The core pattern involves creating separate tmux sessions for each Claude Code agent, each handling distinct responsibilities. One agent might handle frontend development using the frontend-design skill while another runs test-driven development with the tdd skill. A third session could manage documentation generation via the pdf skill, all running concurrently and communicating through shared files or a coordination script.

Setting Up the Foundation

Before implementing multi-agent workflows, ensure tmux is installed and configure basic settings for agent management. Create a dedicated tmux configuration file optimized for automation:

# ~/.tmux-agent.conf
set -g default-terminal "screen-256color"
set -g history-limit 50000
set -g allow-rename off
set -g set-titles on
set -g set-titles-string "Claude Agent: #S"

# Enable mouse support for manual debugging
set -g mouse on

Launch tmux with this configuration using tmux -f ~/.tmux-agent.conf. The configuration disables automatic window renaming, which keeps agent session names stable for programmatic access.

Spawning Claude Code Agents in Tmux Sessions

The fundamental operation involves creating a new tmux session and running Claude Code within it. Here’s a bash function that spawns a Claude Code agent in print mode:

spawn_claude_agent() {
    local session_name="$1"
    local task_prompt="$2"
    
    tmux new-session -d -s "$session_name" "claude -p \"$task_prompt\""
    
    echo "Agent $session_name spawned"
}

# Example: spawn agents for parallel work
spawn_claude_agent "frontend-agent" "Implement the login page using React components"
spawn_claude_agent "backend-agent" "Create the authentication API endpoints"
spawn_claude_agent "docs-agent" "Generate API documentation from the OpenAPI spec"

This pattern spawns three concurrent agents, each handling different aspects of a project. The -d flag runs sessions detached, allowing all agents to execute simultaneously.

Managing Agent Lifecycle

Beyond spawning, your workflow needs to monitor agent status, retrieve results, and handle failures. Build a management script that provides these capabilities:

# Check if an agent session exists
agent_exists() {
    tmux has-session -t "$1" 2>/dev/null
}

# Get agent exit status
agent_status() {
    if tmux has-session -t "$1" 2>/dev/null; then
        local panes=$(tmux list-panes -t "$1" -F '#{pane_dead_status}' 2>/dev/null)
        if [ -n "$panes" ]; then
            echo "completed:$panes"
        else
            echo "running"
        fi
    else
        echo "not_found"
    fi
}

# Capture agent output (works best with print mode)
agent_output() {
    tmux capture-pane -t "$1" -p
}

# Terminate a running agent
kill_agent() {
    tmux kill-session -t "$1"
}

These functions form the building blocks for sophisticated orchestration logic. Combine them to create workflows that respond to agent completion, failure, or timeout conditions.

Implementing Coordination Patterns

With the foundation in place, implement coordination patterns that determine how agents interact. The fan-out-fan-in pattern with tmux-managed Claude Code agents:

#!/bin/bash
# fan-out-fan-in.sh - Parallel execution with result aggregation

TASK_FILE="tasks.json"
RESULTS_DIR="./agent-results"

mkdir -p "$RESULTS_DIR"

# Parse tasks and spawn agents
jq -c '.[]' "$TASK_FILE" | while read task; do
    agent_id=$(echo "$task" | jq -r '.id')
    prompt=$(echo "$task" | jq -r '.prompt')
    spawn_claude_agent "worker-$agent_id" "$prompt"
done

# Wait for all agents to complete
all_complete=false
while [ "$all_complete" = "false" ]; do
    all_complete=true
    for session in $(tmux list-sessions -F '#{session_name}' | grep "^worker-"); do
        status=$(agent_status "$session")
        if [[ "$status" == "running" ]]; then
            all_complete=false
        fi
    done
    sleep 2
done

# Aggregate results
for session in $(tmux list-sessions -F '#{session_name}' | grep "^worker-"); do
    agent_id="${session#worker-}"
    agent_output "$session" > "$RESULTS_DIR/$agent_id.txt"
done

This pattern suits scenarios where you need multiple Claude Code agents to work on independent tasks simultaneously, then combine their outputs. Use this for parallel code generation, distributed testing, or concurrent documentation generation.

Integrating Claude Skills Into Multi Agent Workflows

The real power emerges when combining tmux orchestration with Claude Code skills. Each agent session can load specific skills tailored to its role:

# Frontend agent with specialized skills
spawn_claude_agent "frontend-pro" "Using frontend-design skill, create a responsive landing page"

# Testing agent with TDD skills
spawn_claude_agent "tester" "Using tdd skill, write unit tests for the user authentication module"

# Documentation agent
spawn_claude_agent "docs" "Using pdf skill, generate a developer guide from the codebase"

The supermemory skill becomes valuable in multi-agent setups, providing persistent context across sessions. Configure agents to use shared memory stores so they can access previous work:

spawn_claude_agent_with_memory() {
    local session_name="$1"
    local task="$2"
    
    # Load context from shared memory
    local context=$(claude -p "Extract project context from ./shared-memory/")
    
    tmux new-session -d -s "$session_name" \
        "claude -p \"Context: $context\n\nTask: $task\""
}

Practical Example: Full Stack Development Workflow

Consider a complete workflow for building a web application:

#!/bin/bash
# fullstack-workflow.sh

# Phase 1: Planning (single agent)
spawn_claude_agent "planner" "Analyze requirements and create SPEC.md for a task management app"

# Wait for plan
while [ "$(agent_status planner)" = "running" ]; do sleep 2; done

# Phase 2: Parallel implementation
spawn_claude_agent "api-dev" "Build REST API with Express, use tdd skill for test-first development"
spawn_claude_agent "ui-dev" "Create React frontend, use frontend-design skill for component design"
spawn_claude_agent "db-dev" "Design PostgreSQL schema and write migration scripts"

# Phase 3: Integration and documentation
spawn_claude_agent "integration" "Integrate frontend with API, fix CORS and authentication issues"
spawn_claude_agent "docs" "Using pdf skill, generate API documentation and deployment guide"

echo "All agents spawned. Monitor with: tmux ls"

Run this script, and tmux manages six concurrent Claude Code sessions, each handling different responsibilities. Monitor progress using tmux list-windows or attach to specific sessions with tmux attach -t session-name.

Best Practices and Common Pitfalls

When building tmux-based multi-agent systems, avoid spawning unlimited sessions. Monitor system resources and cap concurrent agents based on available CPU and memory. Each Claude Code instance consumes resources even in print mode.

Implement proper error handling for agent failures. Network interruptions or API timeouts can cause agents to exit unexpectedly. Your orchestration should detect these conditions and either retry or escalate:

handle_agent_failure() {
    local agent_name="$1"
    local max_retries=3
    local attempt=1
    
    while [ $attempt -le $max_retries ]; do
        if agent_exists "$agent_name"; then
            local status=$(agent_status "$agent_name")
            if [[ "$status" =~ ^completed:[1-9]+$ ]]; then
                return 0  # Success
            elif [[ "$status" == "not_found" ]]; then
                # Respawn agent
                spawn_claude_agent "$agent_name" "$(cat ./pending-tasks/$agent_name)"
            fi
        fi
        sleep 5
        ((attempt++))
    done
    
    echo "Agent $agent_name failed after $max_retries attempts"
    return 1
}

Use descriptive session names that reflect agent purpose. Names like frontend-v2 or api-auth make debugging and monitoring significantly easier than auto-generated IDs.

Extending the System

For more advanced orchestration, integrate with tools like supermemory for persistent agent memory, or connect to external coordination systems through webhooks. The tmux foundation provides reliability and visibility into agent behavior while remaining simple to script and automate.

Building multi-agent workflows with Claude Code and tmux transforms your terminal into a command center for AI-assisted development. Start with simple parallel tasks, then expand to sophisticated coordination patterns as your workflows mature.

Built by theluckystrike — More at zovo.one