Skip to main content

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 using AgentCtrl, 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. After the binary is available, configure Laravel defaults in config/instructor.php under agent_ctrl for timeout, working directory, sandbox driver, and per-agent model overrides.

Supported Agents

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 the provider/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. Use withConfig() 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 in config/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.
Agent-specific settings (e.g., 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

The AgentResponse 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 check isSuccess() and handle failures gracefully. Agent executions can fail due to timeouts, sandbox errors, or issues in the generated code.

Testing

Use AgentCtrl::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.

Best Practices

  1. Set Timeouts — Always set appropriate timeouts for your use case. Complex code generation can take several minutes.
  2. Use Sandbox Isolation — In production, use Docker or another container-based sandbox driver to prevent agents from making unintended changes.
  3. Handle Errors — Check isSuccess() and handle failures gracefully. Agents can fail for many reasons, including API limits, invalid code, and sandbox restrictions.
  4. Log Sessions — Store session IDs for debugging and continuation. They let you resume work and trace agent behavior.
  5. Test with Fakes — Use AgentCtrl::fake() in tests to avoid API calls and process execution.
  6. Queue Long Tasks — Use Laravel queues for time-consuming code generation to keep web responses fast.