|
| 1 | +--- |
| 2 | +slug: building-reliable-agentic-loops-with-laravel-workflow-and-prismphp |
| 3 | +title: "Building Reliable Agentic Loops with Laravel Workflow and PrismPHP" |
| 4 | +authors: |
| 5 | + name: Richard |
| 6 | + title: Core Team |
| 7 | + url: https://github.com/rmcdaniel |
| 8 | + image_url: https://github.com/rmcdaniel.png |
| 9 | +tags: [ai, workflow, agents, agentic] |
| 10 | +--- |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +Laravel Workflow is a powerful tool for orchestrating long-running, stateful workflows in PHP. Paired with [PrismPHP](https://prismphp.com/), it becomes a compelling foundation for building reliable AI agents that not only generate structured data but verify and retry until results meet strict real-world constraints. |
| 15 | + |
| 16 | +In this post, we’ll show how to use Laravel Workflow + Prism to create an agentic loop that: |
| 17 | + |
| 18 | +- Generates structured data using an LLM |
| 19 | +- Validates the result against custom rules |
| 20 | +- Retries automatically until the result passes |
| 21 | + |
| 22 | +You can try this exact workflow right now in your browser with no setup or coding required. Just click the button in the Laravel Workflow [Sample App](https://github.com/laravel-workflow/sample-app) and launch a GitHub Codespace to run it. |
| 23 | + |
| 24 | +### What We’re Building |
| 25 | + |
| 26 | +We’ll create a workflow that asks an LLM to generate a user profile with hobbies. Then we’ll validate that: |
| 27 | + |
| 28 | +- The name is present |
| 29 | +- At least one hobby is defined |
| 30 | +- The name starts with a vowel |
| 31 | + |
| 32 | +If the result fails validation, we loop back to the LLM and regenerate. All of this is durable, asynchronous, and tracked through stateful events. |
| 33 | + |
| 34 | +### Step-by-Step Example |
| 35 | + |
| 36 | +1. Console Command to Trigger the Workflow |
| 37 | +```php |
| 38 | +use App\Workflows\Prism\PrismWorkflow; |
| 39 | +use Illuminate\Console\Command; |
| 40 | +use Workflow\WorkflowStub; |
| 41 | + |
| 42 | +class Prism extends Command |
| 43 | +{ |
| 44 | + protected $signature = 'app:prism'; |
| 45 | + |
| 46 | + protected $description = 'Runs a Prism AI workflow'; |
| 47 | + |
| 48 | + public function handle() |
| 49 | + { |
| 50 | + $workflow = WorkflowStub::make(PrismWorkflow::class); |
| 51 | + $workflow->start(); |
| 52 | + while ($workflow->running()); |
| 53 | + $user = $workflow->output(); |
| 54 | + |
| 55 | + $this->info('Generated User:'); |
| 56 | + $this->info(json_encode($user, JSON_PRETTY_PRINT)); |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +2. Define the Workflow Logic |
| 62 | +```php |
| 63 | +use Workflow\ActivityStub; |
| 64 | +use Workflow\Workflow; |
| 65 | + |
| 66 | +class PrismWorkflow extends Workflow |
| 67 | +{ |
| 68 | + public function execute() |
| 69 | + { |
| 70 | + do { |
| 71 | + $user = yield ActivityStub::make(GenerateUserActivity::class); |
| 72 | + $valid = yield ActivityStub::make(ValidateUserActivity::class, $user); |
| 73 | + } while (!$valid); |
| 74 | + |
| 75 | + return $user; |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +This is a classic agent loop. If validation fails, we prompt again automatically. |
| 81 | + |
| 82 | +3. Generate Structured User Data with PrismPHP |
| 83 | +```php |
| 84 | +use Prism\Prism\Prism; |
| 85 | +use Prism\Prism\Enums\Provider; |
| 86 | +use Prism\Prism\Schema\ArraySchema; |
| 87 | +use Prism\Prism\Schema\ObjectSchema; |
| 88 | +use Prism\Prism\Schema\StringSchema; |
| 89 | +use Workflow\Activity; |
| 90 | + |
| 91 | +class GenerateUserActivity extends Activity |
| 92 | +{ |
| 93 | + public function execute() |
| 94 | + { |
| 95 | + $schema = new ObjectSchema( |
| 96 | + name: 'user', |
| 97 | + description: 'A user profile with their hobbies', |
| 98 | + properties: [ |
| 99 | + new StringSchema('name', 'The user\'s full name'), |
| 100 | + new ArraySchema( |
| 101 | + name: 'hobbies', |
| 102 | + description: 'The user\'s list of hobbies', |
| 103 | + items: new ObjectSchema( |
| 104 | + name: 'hobby', |
| 105 | + description: 'A detailed hobby entry', |
| 106 | + properties: [ |
| 107 | + new StringSchema('name', 'The name of the hobby'), |
| 108 | + new StringSchema('description', 'A brief description of the hobby'), |
| 109 | + ], |
| 110 | + requiredFields: ['name', 'description'] |
| 111 | + ) |
| 112 | + ), |
| 113 | + ], |
| 114 | + requiredFields: ['name', 'hobbies'] |
| 115 | + ); |
| 116 | + |
| 117 | + $response = Prism::structured() |
| 118 | + ->using(Provider::OpenAI, 'gpt-4o') |
| 119 | + ->withSchema($schema) |
| 120 | + ->withPrompt('Use names from many languages and vary first initials.') |
| 121 | + ->asStructured(); |
| 122 | + |
| 123 | + return $response->structured; |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +4. Validate Business Logic |
| 129 | +```php |
| 130 | +use Workflow\Activity; |
| 131 | + |
| 132 | +class ValidateUserActivity extends Activity |
| 133 | +{ |
| 134 | + public function execute($user) |
| 135 | + { |
| 136 | + if (empty($user['name']) || !is_array($user['hobbies']) || count($user['hobbies']) === 0) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + foreach ($user['hobbies'] as $hobby) { |
| 141 | + if (empty($hobby['name']) || empty($hobby['description'])) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Extra Validation: The user's name must start with a vowel. |
| 147 | + if (!in_array(strtoupper($user['name'][0]), ['A', 'E', 'I', 'O', 'U'])) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + return true; |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +### What Makes This Pattern Powerful |
| 157 | + |
| 158 | +This design pattern is what you’d call a reliable agentic loop: |
| 159 | + |
| 160 | +- LLM generation via Prism |
| 161 | +- Validation & retry via Laravel Workflow |
| 162 | +- State persistence for crash recovery or inspection |
| 163 | +- Observability via Waterline |
| 164 | + |
| 165 | +It’s perfect for AI applications where accuracy, safety, and traceability are required. |
| 166 | + |
| 167 | +### Try It Now in Your Browser |
| 168 | + |
| 169 | +We’ve bundled this workflow into the official Laravel Workflow Sample App, which runs in GitHub Codespaces. |
| 170 | + |
| 171 | +To launch it: |
| 172 | +1. Open the sample-app repo |
| 173 | +2. Click the “Code” button → “Codespaces” → “Create codespace on main” |
| 174 | +3. Wait a few seconds for setup |
| 175 | +4. Set your OPENAI_API_KEY |
| 176 | +5. Run: |
| 177 | +```bash |
| 178 | +php artisan migrate |
| 179 | +php artisan queue:work |
| 180 | +``` |
| 181 | +6. In a second terminal: |
| 182 | +```bash |
| 183 | +php artisan app:prism |
| 184 | +``` |
| 185 | + |
| 186 | +You will see the queue working and eventually see the validated output. |
| 187 | + |
| 188 | +### Where to Go From Here |
| 189 | +You can easily adapt this pattern to: |
| 190 | + |
| 191 | +- AI agents for form filling |
| 192 | +- Data scraping and validation |
| 193 | +- Content generation with retry policies |
| 194 | +- Moderation and review queues |
| 195 | + |
| 196 | +Each step remains reliable and traceable thanks to Laravel Workflow’s durable execution model. |
0 commit comments