Skip to main content

Overview

This example demonstrates the simplest use of AgentCtrl to execute a prompt against a CLI-based code agent. It shows basic prompt execution with response handling and statistics display.

Example

<?php
require 'examples/boot.php';

use Cognesy\AgentCtrl\AgentCtrl;
use Cognesy\AgentCtrl\Enum\AgentType;

/**
 * Unified Agent - Basic Example
 *
 * Demonstrates the simplest use of AgentCtrl to execute a prompt
 * against a CLI-based code agent.
 */

print("╔════════════════════════════════════════════════════════════════╗\n");
print("║              Unified Agent - Basic Usage Demo                  ║\n");
print("╚════════════════════════════════════════════════════════════════╝\n\n");

// Simple prompt execution
print("Sending prompt to OpenCode agent...\n\n");

$response = AgentCtrl::make(AgentType::OpenCode)
    ->execute('Explain the SOLID principles in software design. List each principle with a one-line explanation.');

if ($response->isSuccess()) {
    print("RESPONSE:\n");
    print(str_repeat("─", 68) . "\n");
    print($response->text() . "\n");
    print(str_repeat("─", 68) . "\n\n");

    print("STATS:\n");
    print("  Agent: {$response->agentType->value}\n");

    if ($response->sessionId) {
        print("  Session: {$response->sessionId}\n");
    }
    if ($response->usage) {
        print("  Tokens: {$response->usage->input} input, {$response->usage->output} output\n");
    }
    if ($response->cost) {
        print("  Cost: $" . number_format($response->cost, 4) . "\n");
    }
} else {
    print("ERROR: Request failed with exit code {$response->exitCode}\n");
}
?>

Key Features

  • Simple API: Single method call to execute prompts
  • Response Handling: Comprehensive success/error handling
  • Statistics: Detailed usage metrics and cost information
  • Agent Types: Support for different CLI agents (OpenCode, Claude Code, Codex)