Skip to main content

Introduction

Agent-Ctrl’s builder API is split into two layers: a shared set of methods that every builder supports, and agent-specific methods that expose the unique capabilities of each CLI tool. This design lets you write agent-agnostic code for common configuration while still accessing the full feature set of each agent when needed. All configuration methods return static, so they can be chained fluently in any order before calling execute() or executeStreaming().

Shared Options

The following methods are defined in the AgentBridgeBuilder interface and implemented by every bridge builder. They work identically regardless of which agent you are using.

withConfig(AgentCtrlConfig $config): static

Apply a typed config object containing the shared builder options:
This is the preferred way to pass shared builder defaults around your own application code. The object covers:
  • model
  • timeout
  • workingDirectory
  • sandboxDriver

withModel(string $model): static

Set the model the agent should use. The accepted model name format depends on the agent:
If not specified, each agent uses its own default model.

withTimeout(int $seconds): static

Set the maximum execution time in seconds. The default is 120 seconds. The minimum accepted value is 1 second — values below 1 are clamped.
When the timeout is reached, the sandbox executor kills the process. The response will contain whatever output was produced before the timeout and will have a non-zero exit code.

inDirectory(string $path): static

Set the working directory for the agent. The bridge validates that the directory exists before execution and throws an InvalidArgumentException if it does not.
Always use absolute paths. The bridge changes the PHP process’s current working directory for the duration of the execution and restores it afterward.

withSandboxDriver(SandboxDriver $driver): static

Set the sandbox driver for process isolation. The default is SandboxDriver::Host, which runs the CLI binary directly on the host system.
Available drivers: Host, Docker, Podman, Firejail, Bubblewrap.

Streaming Callbacks

Four callback methods are shared across all builders. See the Streaming documentation for full details.
  • onText(callable $handler): static — Receive incremental text output
  • onToolUse(callable $handler): static — Receive normalized tool call events
  • onComplete(callable $handler): static — Receive the final AgentResponse
  • onError(callable $handler): static — Receive streamed error events

wiretap(callable $handler): static

Connect an event observer to the builder’s internal event system. This is primarily used with the AgentCtrlConsoleLogger for development-time debugging:

build(): AgentBridge

Build the configured bridge without executing a prompt. This is an advanced method for scenarios where you need to call the bridge’s execute() or executeStreaming() methods directly:

Claude Code Options

The ClaudeCodeBridgeBuilder adds the following methods on top of the shared options.

withSystemPrompt(string|\Stringable $prompt): static

Replace the agent’s default system prompt entirely with a custom one:

appendSystemPrompt(string|\Stringable $prompt): static

Add instructions on top of the default system prompt without replacing it. This preserves Claude Code’s built-in behavior while layering in project-specific context:
You can use both withSystemPrompt() and appendSystemPrompt() together — withSystemPrompt() sets the base and appendSystemPrompt() appends to it. Both accept Stringable objects (e.g. xprompt Prompt classes), which are cast to string at the boundary.

withMaxTurns(int $turns): static

Limit the number of agentic turns. Each turn represents one cycle where the agent reads context, reasons, and takes an action. The minimum is 1.
Without a turn limit, Claude Code continues working until it decides the task is complete or the timeout is reached. For simple tasks, 5-10 turns is often sufficient. Complex refactoring may need 20-50 turns.

withPermissionMode(PermissionMode $mode): static

Control how the agent handles tool permission requests during headless execution. The default is BypassPermissions because Agent-Ctrl runs headlessly and cannot respond to interactive permission prompts.

verbose(bool $enabled = true): static

Enable or disable verbose output. Verbose mode is required for proper stream-JSON parsing and is enabled by default. You generally do not need to change this setting.

continueSession(): static

Continue the most recent Claude Code session. See Session Management.

resumeSession(string $sessionId): static

Resume a specific Claude Code session by its ID. See Session Management.

withAdditionalDirs(array $paths): static

Grant the agent access to additional directories beyond the working directory:

Complete Claude Code Example

Codex Options

The CodexBridgeBuilder adds the following methods on top of the shared options.

withSandbox(SandboxMode $mode): static

Set the Codex sandbox mode, which controls filesystem and network access:

disableSandbox(): static

Shorthand for withSandbox(SandboxMode::DangerFullAccess). Provides full filesystem and network access:

fullAuto(bool $enabled = true): static

Enable full-auto mode, which combines workspace-write sandbox access with automatic on-failure approval. This is enabled by default for headless execution:

dangerouslyBypass(bool $enabled = true): static

Skip all approval prompts and sandbox restrictions. This is the most permissive mode and should be used with caution:

skipGitRepoCheck(bool $enabled = true): static

Allow the agent to run outside a Git repository. By default, Codex requires the working directory to be inside a Git repository:

withImages(array $imagePaths): static

Attach image files to the prompt for visual analysis:

continueSession(): static

Continue the most recent Codex session. See Session Management.

resumeSession(string $sessionId): static

Resume a specific Codex session by its thread ID. See Session Management.

withAdditionalDirs(array $paths): static

Add additional writable directories for the Codex agent:

Complete Codex Example

OpenCode Options

The OpenCodeBridgeBuilder adds the following methods on top of the shared options.

withAgent(string $agentName): static

Select a named agent within OpenCode (e.g., 'coder', 'task'):

withFiles(array $filePaths): static

Attach specific files to the prompt for the agent to reference:

withTitle(string $title): static

Set a descriptive title for the session, which appears in OpenCode’s session listing:

shareSession(): static

Mark the session for sharing after completion, making it accessible to other users or tools:

continueSession(): static

Continue the most recent OpenCode session. See Session Management.

resumeSession(string $sessionId): static

Resume a specific OpenCode session by its ID. See Session Management.

Complete OpenCode Example

Pi Options

The PiBridgeBuilder adds the following methods on top of the shared options.

withProvider(string $provider): static

Set the provider explicitly (e.g., 'anthropic', 'openai', 'google'):

withThinking(ThinkingLevel $level): static

Set the thinking level, which controls how much reasoning the agent does:

withSystemPrompt(string|\Stringable $prompt): static

Replace the agent’s default system prompt:

appendSystemPrompt(string|\Stringable $prompt): static

Add instructions on top of the default system prompt:

withTools(array $tools): static

Enable specific built-in tools:

noTools(): static

Disable all built-in tools.

withFiles(array $filePaths): static

Attach files to the prompt:

withExtensions(array $extensions): static

Load extensions from paths or sources.

noExtensions(): static

Disable extension discovery.

withSkills(array $skills): static

Load skills from paths.

noSkills(): static

Disable skill discovery.

withApiKey(string $apiKey): static

Override the API key for this execution.

ephemeral(): static

Run in ephemeral mode — the session is not saved.

withSessionDir(string $dir): static

Set a custom session storage directory.

verbose(bool $enabled = true): static

Enable verbose output.

continueSession(): static

Continue the most recent Pi session. See Session Management.

resumeSession(string $sessionId): static

Resume a specific Pi session by its ID. See Session Management.

Complete Pi Example

Gemini Options

The GeminiBridgeBuilder adds the following methods on top of the shared options.

withApprovalMode(ApprovalMode $mode): static

Set the approval mode for tool execution:

yolo(): static

Shorthand for withApprovalMode(ApprovalMode::Yolo):

planMode(): static

Shorthand for withApprovalMode(ApprovalMode::Plan):

withSandbox(bool $enabled = true): static

Enable Gemini’s sandbox mode for additional isolation.

withIncludeDirectories(array $paths): static

Add additional workspace directories:

withExtensions(array $extensions): static

Use specific extensions.

withAllowedTools(array $tools): static

Restrict which tools the agent can use:

withAllowedMcpServers(array $names): static

Set allowed MCP server names.

withPolicy(array $paths): static

Add policy files or directories.

debug(bool $enabled = true): static

Enable debug output for troubleshooting CLI behavior.

continueSession(): static

Continue the most recent Gemini session (resumes 'latest' internally). See Session Management.

resumeSession(string $sessionId): static

Resume a specific Gemini session by its ID or index. See Session Management.

Complete Gemini Example