Tmux for AI coding agents: sessions and reconnection
October 16, 2026 · Oyren AI Team (AI-written)

Recommendation: Run each remote AI coding agent inside a dedicated, task-named
tmuxsession (tmux new-session -s <task-name>) so closing a browser tab or dropping an SSH connection never kills the active CLI process. Treattmuxas protection against network disconnects, and rely ongit pushplus workspace snapshots to protect against machine stops.
Choose or create a workspace, then select the agents and repositories for the workflow.
Terminal-based coding agents such as Claude Code, Codex CLI, OpenCode, and the Google Antigravity CLI (agy) run as interactive foreground processes. When an agent is launched directly inside a raw SSH session or browser terminal tab, a brief Wi-Fi drop or closed window sends SIGHUP to the shell and interrupts the run midway through file edits or test suites. Wrapping the task in a named tmux session decouples the running process on the Linux host from the viewing client.
Session, window, and pane architecture for agent tasks
The tmux manual separates the background server (which owns pseudo-terminals on the execution machine) from the attached client (which renders the screen over SSH or a web socket). Naming each session after a single task and repository checkout makes reconnection deterministic without memorizing numeric session IDs or crowded split-pane grids.
The Mind Map above shows a three-window layout inside one task session, agent-form-fix. The left card is the terminal client, a browser terminal tab or SSH client that attaches and detaches without stopping anything inside the session. The center card is the tmux task session itself (-s agent-form-fix), bound to one repository checkout and one feature branch on the Linux host. Window 0 runs the interactive coding agent (claude, codex, opencode, or agy) in a full-width pane so diffs and tool calls stay readable; window 1 runs test suites and typechecks (pnpm test, pnpm typecheck) on demand without scrolling the agent conversation off screen; window 2 holds the long-running preview server or watcher on its own port.
Keeping one task per named session prevents log confusion. When ten unrelated agents share a single multi-pane window, identifying which checkout or port belongs to which pane slows down review and increases the risk of stopping the wrong process.
Creating, detaching, and reattaching a named session
From the task's assigned repository directory (or isolated Git worktree) on the execution host, a single command creates and attaches to a named tmux session:
tmux new-session -s agent-form-fix
The flags and arguments above set up the task container:
new-session: Starts a new session on the localtmuxserver (launching the server automatically if none is running) and attaches the current terminal to Window0.-s agent-form-fix: Assigns the human-readable session nameagent-form-fixso later reconnect commands can target the task by name rather than index0or1.
Once inside the session, start the coding agent CLI. To leave the session running in the background, press the default detach key sequence Ctrl-b followed by d. From any new SSH connection or browser terminal tab on the same host, list the active sessions and reattach by name:
tmux list-sessions
tmux attach-session -t agent-form-fix
Each command above handles a step of the reconnect flow:
tmux list-sessions: Prints every active session on the host along with its window count, creation time, and attachment status.tmux attach-session -t agent-form-fix: Reconnects the current terminal client to the target session (-t) namedagent-form-fix, restoring the exact screen state of the running agent.
Before starting a multi-hour refactor on a new host, a ten-second rehearsal (create a test session, detach with Ctrl-b d, close the terminal tab, run tmux attach-session -t from a fresh tab) confirms that the host keeps background processes alive when clients disconnect.
What tmux preserves versus what requires Git or snapshots
A common operational mistake is assuming that because tmux survives a closed browser tab, it also survives a stopped virtual machine or replaces version control. Separating client disconnects from host lifecycle events keeps work recoverable at every layer.
The Mind Map above divides task state into two groups. The left branch is what the tmux server preserves while the Linux host stays powered on: the running agent PID, the pseudo-terminal's environment variables, the working directory and the pane scrollback buffers, all across network drops. The right branch is what tmux alone does not preserve: if the host stops, reboots or is replaced, the in-memory tmux server ends with it, so unpushed commits, uncommitted working-tree edits and task summaries need durable storage outside the terminal server.
| Layer of task state | What happens on client disconnect | What happens if the host stops | Durable record to maintain |
|---|---|---|---|
| Layer of task stateRunning agent process | What happens on client disconnectContinues running inside tmux | What happens if the host stopsProcess terminates with the OS | Durable record to maintainTask prompt and checkpoint notes |
| Layer of task stateTerminal scrollback | What happens on client disconnectPreserved in tmux pane memory | What happens if the host stopsCleared when RAM resets | Durable record to maintainExported failure excerpt in PR or note |
| Layer of task stateWorking-tree file edits | What happens on client disconnectUntouched on host disk | What happens if the host stopsSurvives only if disk is persistent | Durable record to maintainCommitted changes on a named branch |
| Layer of task stateLocal Git commits | What happens on client disconnectUntouched in local .git | What happens if the host stopsUnreachable while machine is off | Durable record to maintainRemote branch updated via git push |
| Layer of task stateAgent internal checkpoints | What happens on client disconnectPreserved in agent session store | What happens if the host stopsScoped to tracked edits only | Durable record to maintainFull Git commits (Claude Code checkpointing) |
When tmux attach-session -t <name> fails to find a session, checking the layers in order prevents launching a duplicate agent on top of an already-running checkout:
hostnameandpwdconfirm that the terminal reached the same machine and workspace.tmux list-sessionsshows whether the session carries a different name or whether thetmuxserver table is empty after a machine restart.git statusandgit log -n 3in the task checkout show what the previous session committed before it ended.
Capturing pane output without leaking secrets
When a test suite or build fails inside a tmux window, copying only the relevant failure lines into a pull request or handoff note is cleaner and safer than dumping an entire terminal log. The capture-pane subcommand reads lines directly from a pane's scrollback buffer:
tmux capture-pane -pt agent-form-fix:1.0 -S -80
Each parameter in the command above controls what gets extracted:
capture-pane: Reads the visible screen and scrollback history of a target pane without interrupting the process running inside it.-p: Prints the captured text directly to standard output (stdout) instead of storing it only in an internaltmuxpaste buffer.-t agent-form-fix:1.0: Targets sessionagent-form-fix, window1(the verification window), pane0.-S -80: Starts capturing80lines above the top of the currently visible pane (-Ssets the starting line offset), capturing the error trace without pulling thousands of lines of package installation noise.
Before pasting captured output into a shared issue or pull request, inspect the snippet to ensure no API tokens, session cookies, or environment variable dumps (env, printenv) were printed in the scrollback window.
Parallel agent limits and session cleanup
tmux creates dozens of sessions with little overhead, so terminal multiplexing is never the bottleneck for parallel coding agents. The limits come from the host. Two coding agents editing the same working directory overwrite each other's uncommitted files and lock .git/index, so every concurrent tmux task session runs in its own repository clone or isolated Git worktree. Concurrent language servers, typecheckers and test runners also compete for CPU cores and RAM, as measured in the guide on running 30 AI coding agents on one machine.
Once a task's pull request is merged or handed off, verify that git status is clean, confirm the branch is pushed to origin, stop any background preview server in Window 2, and close the session with tmux kill-session -t agent-form-fix so stale processes do not hold ports or memory.
In Oyren Codespaces, the browser terminal connects to a persistent Linux environment where tmux keeps agent sessions alive whenever the browser tab is closed or switched to another device. Stopping the Codespace halts the underlying virtual machine and ends running tmux processes while preserving disk state according to the workspace snapshot settings. The same holds on an approved Mac or 64-bit Raspberry Pi running Docker and the private GitHub kit in the request-only device beta: tmux on that machine keeps the agents running for as long as the machine stays on.
Multi-CLI agent orchestration in tmux
Modern engineering workflows frequently combine multiple specialized agent CLIs across parallel tasks rather than relying on one model family. In a typical multi-agent setup, one tmux session runs Claude Code for architectural refactoring, a second session runs Codex CLI for targeted unit test generation, and a third runs the Antigravity CLI (agy) for autonomous multi-step subagent execution.
Because each CLI has its own authentication lifecycle, starting agents inside persistent tmux sessions avoids repetitive login prompts:
- Claude Code uses web-based OAuth (Claude Code sign-in guide), preserving its token in local configuration.
- OpenAI Codex CLI supports ChatGPT subscription authentication or API keys (Codex sign-in guide).
- Antigravity CLI (
agy) connects directly to project workspaces with pre-authenticated environment credentials in cloud runtimes (Antigravity sign-in guide). - OpenCode provides terminal-based model switching with local and hosted providers (OpenCode sign-in guide).
Comparing runtime characteristics, token costs, and context handling across these tools in an agent comparison table helps teams decide which agent to launch for a given task.
Frequently asked questions
Does detaching a tmux client stop the coding agent?
No. Detaching with Ctrl-b d (or losing an SSH or browser connection) only disconnects the display client. The tmux server process and any coding agent or test runner inside the session continue executing on the host.
Does a tmux session survive if the cloud machine stops or reboots?
No. The tmux server lives in the operating system's memory. If the virtual machine stops or restarts, all running tmux sessions end. Pushing commits with git push and saving workspace snapshots ensure the work survives machine stops.
Why use one named session per task instead of putting multiple agents in one window?
Dedicated task sessions (tmux new-session -s <task>) give each agent a full-width terminal, keep test and dev-server windows grouped with their matching branch, and allow closing one finished task without disturbing other active agents.
Choose or create a workspace, then select the agents and repositories for the workflow.
Open Codespace setup