PTY-for-AI
A pattern for controlling AI CLI tools at the terminal level, enabling cross-LLM orchestration without provider-specific APIs.The Problem
Every AI coding CLI (Claude Code, Codex, Gemini CLI, Aider) communicates with its agents through structured I/O — JSON pipes, API calls, or protocol messages. This creates walls:- Subagents can only call tools, not run interactive terminal sessions
- One AI cannot control another AI at the terminal level
- Multi-agent teams are locked to a single provider
- Cross-LLM collaboration requires per-pair plugins (N² problem)
The Pattern
Instead of structured I/O between agents, use PTY sessions — real terminal instances that any CLI can run in.How It Works
1. Daemon manages PTY sessions
A persistent daemon (launchd/systemd) owns PTY sessions. Sessions survive terminal closures, reboots, and SSH disconnects.2. Headless server bridges to network
A headless server connects PTY sessions to a gateway, enabling remote access without a visible terminal window.3. CLI commands control sessions
4. Any CLI is a “worker”
Why PTY, Not Pipe
| Structured I/O (pipe) | PTY-for-AI | |
|---|---|---|
| Protocol | JSON/NDJSON | Terminal I/O |
| Interactive | No | Yes |
| Target | Same LLM only | Any CLI |
| Cross-LLM | Plugin per pair (N²) | One interface (N) |
| Provider lock-in | Yes | No |
| Heterogeneous teams | No | Yes |
| Cost routing | Model selection only | LLM-level routing |
| Failover | Not possible | Switch PTY session |
| Offline | Not possible | Local LLM in PTY |
Session Plugins
PTY sessions are configured through daemon-external plugins. The daemon binary is never modified — plugins transform session parameters before spawn.| Plugin | What it does | How |
|---|---|---|
| worktree | Git-isolated workspace | Changes cwd to worktree path |
| sandbox | OS-level process isolation | Wraps shell with sandbox-exec |
| scratchpad | Shared knowledge directory | Creates shared dir in workspace |
| env | Environment injection | Sets env vars on session |
| timeout | Session lifetime limit | Wraps shell with timeout |
Architecture
cwd and shell fields.
Plugins work by transforming these fields before the daemon sees them.
No daemon modification needed — ever.
Cross-Machine
PTY-for-AI extends across machines through gateway relay or P2P:Comparison with Existing Approaches
Claude Code Teams
Spawns Claude CLI instances in tmux/iTerm2 panes. Limited to Claude only. Requires visible terminal. Single machine.Codex Plugin for Claude Code
Subprocess JSON-RPC to Codex server. One direction (Codex → Claude). Requires both installed. Single machine.claude-squad
Tmux session manager for multiple AI CLIs. Requires tmux. No headless. No remote. No daemon persistence.Warp
Terminal replacement with multi-agent UI. Requires Warp app. No headless. No cross-machine. No daemon.PTY-for-AI (NIIA)
Daemon-managed headless PTY sessions. Any CLI. Any LLM. Cross-machine. Persistent. Plugin-extensible.The Declarative Layer: connector.json
PTY-for-AI is the execution layer. connector.json is the declarative spec that sits on top. connector.json describes which agents run, in what topology (pipeline, dialogue, meeting, mesh, recursive), on which machines, with what isolation. The runtime reads connector.json and translates it into PTY daemon calls.Design Principles
- PTY, not pipe — terminal-level control, not structured I/O
- Daemon, not process — persistent, survives everything
- Headless, not visual — no terminal window needed
- Plugins, not patches — extend without modifying daemon
- LLM-agnostic — if it runs in a terminal, it’s controllable
- Infrastructure, not prompts — orchestration in daemon, not in system prompts
PTY-for-AI is an open spec by OpenCLIs. Runtime implementation by NIIA (Monolex). Spec: pty4ai.com