Code Agents With AgentCtrl
The AgentCtrl facade provides a unified interface for invoking CLI-based code agents that can execute code, modify files, and perform complex multi-step tasks. Each agent runs as an external process, and the facade handles process management, output parsing, and response structuring.
This guide is specifically for external CLI code agents. Native Cognesy\Agents runtime integration uses a separate Laravel config namespace under instructor.agents and is documented separately in Native Agents.
Setup
Before usingAgentCtrl, install the CLI agent you want to run and make sure its executable is available in PATH. The Laravel package does not install or authenticate these tools for you.
| Agent | Required binary | Setup note |
|---|---|---|
| Claude Code | claude | Install Claude Code separately and sign in with its normal workflow |
| Codex | codex | Install the Codex CLI and ensure codex resolves on the server running Laravel |
| OpenCode | opencode | Install OpenCode and ensure opencode resolves on the server running Laravel |
config/instructor.php under agent_ctrl for timeout, working directory, sandbox driver, and per-agent model overrides.
Supported Agents
| Agent | Description | Use Case |
|---|---|---|
| Claude Code | Anthropic’s Claude agent with code execution | General coding tasks, refactoring, file modifications |
| Codex | OpenAI’s Codex agent | Code generation and completion |
| OpenCode | Multi-model code agent | Research and coding with model flexibility |
Quick Start
Agent Selection
Claude Code
Best for general coding tasks with Anthropic’s Claude models. Supports sandbox isolation, session resumption, and streaming output.Codex
Best for OpenAI-powered code generation.OpenCode
Best for multi-model flexibility. Specify the model using theprovider/model format.
Dynamic Selection
Select agent type at runtime based on configuration or business logic.Configuration
Builder Methods
All agents support the same set of builder methods for configuration. UsewithConfig() when you want one typed object for the shared options, then layer agent-specific methods on top as needed. Builder methods override any defaults set in the Laravel config file.
AgentCtrlConfig::fromArray() also accepts the Laravel config-style keys used in config/instructor.php, so directory and sandbox are mapped automatically.
Laravel Configuration
Configure defaults inconfig/instructor.php. The facade automatically reads these values and applies them when you create a builder. Builder methods then override any defaults for that specific call.
claude_code.timeout) take precedence over the global defaults (e.g., agent_ctrl.timeout).
Environment Variables
Streaming
Process output in real-time with streaming callbacks. The three callback types fire at different points during execution.Response Object
TheAgentResponse object contains the agent’s output along with metadata about the execution.
Session Management
Resume previous sessions for continued work. This is useful for multi-turn interactions where the agent needs context from a prior execution.Error Handling
Always checkisSuccess() and handle failures gracefully. Agent executions can fail due to timeouts, sandbox errors, or issues in the generated code.
Testing
UseAgentCtrl::fake() for testing without actual agent execution. See the Testing guide for full documentation of AgentCtrlFake.
Real-World Examples
Code Generation Service
Queued Code Generation
For long-running agent tasks, dispatch them to a queue so the user does not have to wait.Interactive Code Review
Sandbox Drivers
Control the isolation level of agent execution. The sandbox driver determines whether the agent runs directly on the host or inside a container.| Driver | Description | Use Case |
|---|---|---|
host | Direct execution (no isolation) | Development, trusted environments |
docker | Docker container isolation | Production, untrusted code |
podman | Podman container isolation | Rootless containers |
firejail | Linux sandbox | Lightweight isolation |
bubblewrap | Minimal sandbox | CI/CD environments |
Best Practices
- Set Timeouts — Always set appropriate timeouts for your use case. Complex code generation can take several minutes.
- Use Sandbox Isolation — In production, use Docker or another container-based sandbox driver to prevent agents from making unintended changes.
- Handle Errors — Check
isSuccess()and handle failures gracefully. Agents can fail for many reasons, including API limits, invalid code, and sandbox restrictions. - Log Sessions — Store session IDs for debugging and continuation. They let you resume work and trace agent behavior.
- Test with Fakes — Use
AgentCtrl::fake()in tests to avoid API calls and process execution. - Queue Long Tasks — Use Laravel queues for time-consuming code generation to keep web responses fast.