Requirements
- PHP 8.2 or later
- At least one supported CLI-based code agent installed and authenticated:
- Claude Code — the
claudebinary (Anthropic CLI) - OpenAI Codex — the
codexbinary (npm install -g @openai/codex) - OpenCode — the
opencodebinary (curl -fsSL https://get.opencode.dev | bash) - Pi — the
pibinary (from pi-mono) - Gemini — the
geminibinary (npm install -g @google/gemini-cli)
- Claude Code — the
PATH visible to your PHP process. Run the binary interactively at least once to complete any first-run authentication flows before using it through Agent-Ctrl.
Installation
Install the package via Composer:cognesy/sandbox package for process execution and isolation, which is pulled in automatically.
Your First Request
The simplest way to use Agent-Ctrl is to pick an agent, pass a prompt, and read the result:execute() method runs the agent synchronously, waits for it to finish, and returns an AgentResponse object containing the text output, exit code, session ID, and any tool calls the agent made.
Choosing a Different Agent
Each supported agent has its own factory method on theAgentCtrl facade:
withConfig(), withModel(), withTimeout(), inDirectory(), execute(), executeStreaming(), etc.), so you can switch agents without restructuring your code.
Selecting the Agent at Runtime
When the agent type is determined by configuration or user input rather than being hard-coded, use theAgentType enum with AgentCtrl::make():
AgentType enum has five cases:
| Case | Value | Builder |
|---|---|---|
AgentType::ClaudeCode | 'claude-code' | ClaudeCodeBridgeBuilder |
AgentType::Codex | 'codex' | CodexBridgeBuilder |
AgentType::OpenCode | 'opencode' | OpenCodeBridgeBuilder |
AgentType::Pi | 'pi' | PiBridgeBuilder |
AgentType::Gemini | 'gemini' | GeminiBridgeBuilder |
Common Configuration
Every builder — regardless of the agent type — supports the same set of core configuration methods. These methods are defined in theAgentBridgeBuilder interface and implemented by the AbstractBridgeBuilder base class.
withConfig(AgentCtrlConfig $config): static
Apply a typed config object for the shared builder options:
AgentCtrlConfig::fromArray() accepts Laravel-style aliases:
directory->workingDirectorysandbox->sandboxDriver
Model Selection
Specify which model the agent should use. The format depends on the agent: Claude Code uses Anthropic model names, Codex uses OpenAI model names, and OpenCode uses provider-prefixed IDs:Execution Timeout
Set the maximum time (in seconds) the agent is allowed to run. The default is 120 seconds. If the timeout is exceeded, the process is killed and the response will have a non-zero exit code:Working Directory
Set the directory the agent should operate in. The bridge validates that the directory exists before changing into it and restores the original working directory after execution:Note: Always use absolute paths. The bridge changes the PHP process’s current working directory for the duration of the execution. If your PHP process handles concurrent requests (e.g., Swoole or RoadRunner), be aware that this affects the entire process.
Sandbox Driver
By default, Agent-Ctrl runs the CLI binary directly on the host. You can switch to a containerized sandbox driver for additional process isolation:| Driver | Description |
|---|---|
SandboxDriver::Host | Run directly on the host (default) |
SandboxDriver::Docker | Run inside a Docker container |
SandboxDriver::Podman | Run inside a Podman container |
SandboxDriver::Firejail | Run inside a Firejail sandbox (Linux) |
SandboxDriver::Bubblewrap | Run inside a Bubblewrap sandbox (Linux) |
Checking the Result
Theexecute() method returns an AgentResponse. Always check isSuccess() before using the text output, because a completed execution with a non-zero exit code does not throw an exception:
AgentResponse.
Next Steps
- Streaming — Receive real-time updates while the agent is working
- Session Management — Continue or resume agent sessions
- Agent Options — Explore shared and agent-specific configuration
- Claude Code Bridge, Codex Bridge, OpenCode Bridge, Pi Bridge, Gemini Bridge — Deep dives into each agent’s unique capabilities