PHP Classes

How to Use the PHP AI Agent Framework to Develop Custom Artificial Intelligence Agents Using the Package HyperFlow PHP: Framework to develop AI agents

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-04-28 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
hyperflow-php 1.0MIT/X Consortium ...8Web services, Artificial intelligence, P...
Description 

Author

This package is a framework to develop artificial intelligence agents implemented as a port of the HyperFlow framework to PHP.

It provides classes that provide base classes to create agents that are able to provide responses to prompts about a given domain.

The classes can call a given artificial intelligence large language model's API.

Picture of Muhammad Umer Farooq
Name: Muhammad Umer Farooq <contact>
Classes: 53 packages by
Country: Pakistan Pakistan
Innovation award
Innovation award
Nominee: 7x

Instructions

Example

<?php

require __DIR__ . '/../../vendor/autoload.php';
require
__DIR__ . '/CalcTool.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../../');
$dotenv->safeLoad();

use
HyperFlow\Agent\AgentOptions;
use
HyperFlow\Agent\TaskAgent;
use
HyperFlow\Agent\MetaAgent;
use
HyperFlow\Tools\EditorTool;
use
HyperFlow\Examples\Calculator\CalcTool;

$tasks = json_decode(file_get_contents(__DIR__ . '/tasks.json'), true);

$options = new AgentOptions(
   
tools: [new CalcTool()],
   
model: 'gpt-4o'
);

$agent = new TaskAgent($options);

echo
"=== Generation: Evaluating current CalcTool ===\n";
$correct = 0;
$details = [];

foreach (
$tasks as $task) {
   
$prompt = "You MUST use the calculator tool for this math problem. Do NOT compute the answer yourself.\n" .
             
"Call the calculator tool, then return exactly what it gives you.\n\n" .
             
"Problem: " . $task['description'] . "\n\n" .
             
'Respond with ONLY JSON: { "response": "<the number the calculator returned>" }';

   
$output = $agent->forward(['task' => $prompt]);
   
   
// Extact response
   
$prediction = "error";
    if (
preg_match('/"response"\s*:\s*"?([^"}]+)"?/', $output, $matches)) {
       
$prediction = trim($matches[1]);
    } else if (
preg_match('/-?\d+\.?\d*/', $output, $matches)) {
       
$prediction = $matches[0];
    }

   
$is_correct = ($prediction == $task['expected']) || (floatval($prediction) == floatval($task['expected']));
    if (
$is_correct) $correct++;
   
   
$icon = $is_correct ? "PASS" : "FAIL";
   
$line = " [$icon] {$task['id']}: '{$task['description']}' -> $prediction (expected: {$task['expected']})";
    echo
$line . "\n";
   
$details[] = $line;
}

$score = count($tasks) > 0 ? $correct / count($tasks) : 0;
echo
"\nScore: " . number_format($score, 2) . " ($correct/" . count($tasks) . ")\n\n";

if (
$score < 1.0) {
    echo
"=== MetaAgent fixing CalcTool ===\n";
   
$metaOptions = new AgentOptions(
       
tools: [new EditorTool()],
       
model: 'gpt-4o'
   
);
   
$metaAgent = new MetaAgent($metaOptions);

   
$evalContext = "The calculator tool has bugs. Here are the results:\n" . implode("\n", $details) . "\n\n" .
                  
"Please use the EditorTool to fix the bugs in " . __DIR__ . "/CalcTool.php.\n" .
                  
"Known bugs:\n" .
                  
"- Subtraction returns abs() instead of allowing negatives\n" .
                  
"- Multiplication returns a+b instead of a*b when numbers > 10\n" .
                  
"- Division uses floor() which truncates decimals\n" .
                  
"Overwrite the file with the corrected code.";
   
   
$metaAgent->forward(['evaluation_context' => $evalContext]);

    echo
"MetaAgent has finished. Please run this script again to see the improved score!\n";
} else {
    echo
"All tasks passed! No improvement needed.\n";
}


Details

HyperFlow (PHP)

<p align="center"> <img src="./assets/logo.png" width="150" alt="HyperFlow Logo" /> </p>

HyperFlow PHP is a fully functional PHP port of the framework for building agents that rewrite and test their own improvements.

Instead of manually retuning prompts and logic after every failure, HyperFlow runs a self-improvement loop where an agent evaluates what happened, edits its own code, tools, and prompts, then tests the new version in a sandbox.

The core idea is simple: do not just rerun the same workflow. Learn from execution and get better over time.

Built natively in PHP 8.1+ utilizing openai-php/client. Inspired by HyperAgents (Meta Research, 2026).

> ?? EXPERIMENTAL: This project is currently in an experimental phase and is not recommended for production use.

What it does

HyperFlow runs an evolutionary self-improvement loop with two roles: - TaskAgent solves the domain problem - MetaAgent studies evaluation results and improves the system

Each generation:

  1. Select a parent generation from the archive
  2. MetaAgent reads past evaluation scores and edits the source code
  3. Evaluation scripts run in a sandbox to score the new agent
  4. Better agents are added back to the archive for future generations

It is self-referential: the mechanism that improves the agent is itself part of the editable code.

> [!IMPORTANT] > This framework is currently in an Experimental state. See Limitations for more information.

The TaskAgent gets better over generations without manual intervention.

Installation

Install using Composer:

composer require lablnet/hyperflow-php

Or install from source for development:

git clone https://github.com/lablnet/hyperflow-php.git
cd hyperflow-php

composer install

Requirements

  • PHP 8.1+
  • Composer
  • At least one LLM provider API key (e.g. `OPENAI_API_KEY`)

Quick Start

# Set your API key in .env or export it
cp .env.example .env
# Edit .env and set OPENAI_API_KEY

# Run the bash example
cd examples/bash
php run.php

Project Structure

php/
  composer.json
  src/
    Agent/
      AgentOptions.php       # DTO for agent configuration
      AgentSystem.php        # Abstract AgentSystem base class
      Llm.php                # OpenAI client integration
      LlmConfig.php          # Config DTO for the LLM
      LlmWithTools.php       # Synchronous ReAct chat loop
      MetaAgent.php          # MetaAgent (mutation operator)
      TaskAgent.php          # TaskAgent (task solver)
      ToolRegistry.php       # Tool registration
    Contracts/
      BaseChatModel.php      # LLM invocation interface
      BaseTool.php           # Abstract class defining tools
      Message.php            # Base Message class
      AIMessage.php          # Assistant message model
      HumanMessage.php       # User message model
      SystemMessage.php      # System message model
      ToolMessage.php        # Tool execution result model
    Core/
      GenerateLoop.php       # Main evolutionary loop
    Domains/
      Base.php               # Domain interfaces
      Harness.php            # Evaluation harness
    Tools/
      BashTool.php           # Bash shell tool
      EditorTool.php         # File editor tool
    Utils/
      Archive.php            # JSONL archive CRUD
examples/
  bash/                      # Bash command generation
  calculator/                # Buggy tool fix demo
  factcheck/                 # True/false classification

Supported Models

Since this PHP implementation relies primarily on the OpenAI API standard, you can pass any supported model string during configuration:

use HyperFlow\Agent\AgentOptions;

$options = new AgentOptions(model: 'gpt-4o'); // 'gpt-4o', 'gpt-4o-mini', 'o3', etc.

Environment Variables

The framework supports loading environment variables from a .env file in the project root.

| Variable | Description | |----------|-------------| | OPENAI_API_KEY | OpenAI API key |

Examples

Single Evaluation / Self-Improvement Loops

Execute the specific scripts depending on the example you want to run. The examples demonstrate both strict execution and evolutionary loops.

cd examples/bash && php run.php
cd examples/factcheck && php run.php
cd examples/paper_review && php run.php
cd examples/calculator && php run.php

License

MIT

Citation

If you use this framework in your research, please cite the original HyperAgents paper:

@misc{zhang2026hyperagents,
      title={Hyperagents}, 
      author={Jenny Zhang and Bingchen Zhao and Wannan Yang and Jakob Foerster and Jeff Clune and Minqi Jiang and Sam Devlin and Tatiana Shavrina},
      year={2026},
      eprint={2603.19461},
      archivePrefix={arXiv},
      primaryClass={cs.AI},
      url={https://arxiv.org/abs/2603.19461}, 
}

Screenshots (2)  
  • assets/logo.png
  • docs_site/docs/.vuepress/public/logo.png
  Files folder image Files (72)  
File Role Description
Files folder imagedocs_site (2 files, 1 directory)
Files folder imageexamples (3 directories)
Files folder imagesrc (6 directories)
Accessible without login Plain text file .env.example Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (72)  /  docs_site  
File Role Description
Files folder imagedocs (1 file, 2 directories)
  Accessible without login Plain text file package.json Data Auxiliary data
  Accessible without login Plain text file pnpm-lock.yaml Data Auxiliary data

  Files folder image Files (72)  /  docs_site  /  docs  
File Role Description
Files folder image.vuepress (1 file, 2 directories)
Files folder imageguide (7 files)
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  
File Role Description
Files folder image.cache (1 directory)
Files folder imagedist (2 files, 2 directories)
  Accessible without login Plain text file config.js Data Auxiliary data

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  /  .cache  
File Role Description
Files folder imagedeps (9 files)

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  /  .cache  /  deps  
File Role Description
  Accessible without login Plain text file dist-Cn6jRu7f.js Data Auxiliary data
  Accessible without login Plain text file dist-Cn6jRu7f.js.map Data Auxiliary data
  Accessible without login Plain text file package.json Data Auxiliary data
  Accessible without login Plain text file vue-router.js Data Auxiliary data
  Accessible without login Plain text file vue-router.js.map Data Auxiliary data
  Accessible without login Plain text file vue.js Data Auxiliary data
  Accessible without login Plain text file vue.runtime.esm-bundler-CqOwLJ7x.js Data Auxiliary data
  Accessible without login Plain text file vue.runtime.esm-bundler-CqOwLJ7x.js.map Data Auxiliary data
  Accessible without login Plain text file _metadata.json Data Auxiliary data

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  /  dist  
File Role Description
Files folder imageassets (10 files)
Files folder imageguide (5 files)
  Accessible without login HTML file 404.html Doc. Documentation
  Accessible without login HTML file index.html Doc. Documentation

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  /  dist  /  assets  
File Role Description
  Accessible without login Plain text file 404.html-DHrpx-wC.js Data Auxiliary data
  Accessible without login Plain text file advanced-concepts-C85AF0vT.js Data Auxiliary data
  Accessible without login Plain text file app-BOL5tBcs.js Data Auxiliary data
  Accessible without login Plain text file basic-concepts-Bz68xxmp.js Data Auxiliary data
  Accessible without login Plain text file contributing-BSjjwcF5.js Data Auxiliary data
  Accessible without login Plain text file examples-DF0TzFhQ.js Data Auxiliary data
  Accessible without login Plain text file installation-w2eZ9HVg.js Data Auxiliary data
  Accessible without login Plain text file plugin-vue_export-helper-DazhzKqf.js Data Auxiliary data
  Accessible without login Plain text file README-ByKS5tQZ.js Data Auxiliary data
  Accessible without login Plain text file style-Cw72oY46.css Data Auxiliary data

  Files folder image Files (72)  /  docs_site  /  docs  /  .vuepress  /  dist  /  guide  
File Role Description
  Accessible without login HTML file advanced-concepts.html Doc. Documentation
  Accessible without login HTML file basic-concepts.html Doc. Documentation
  Accessible without login HTML file contributing.html Doc. Documentation
  Accessible without login HTML file examples.html Doc. Documentation
  Accessible without login HTML file installation.html Doc. Documentation

  Files folder image Files (72)  /  docs_site  /  docs  /  guide  
File Role Description
  Accessible without login Plain text file advanced-concepts.md Data Auxiliary data
  Accessible without login Plain text file basic-concepts.md Data Auxiliary data
  Accessible without login Plain text file citation.md Data Auxiliary data
  Accessible without login Plain text file contributing.md Data Auxiliary data
  Accessible without login Plain text file examples.md Data Auxiliary data
  Accessible without login Plain text file installation.md Data Auxiliary data
  Accessible without login Plain text file limitations.md Data Auxiliary data

  Files folder image Files (72)  /  examples  
File Role Description
Files folder imagebash (1 file)
Files folder imagecalculator (3 files)
Files folder imagefactcheck (3 files)

  Files folder image Files (72)  /  examples  /  bash  
File Role Description
  Plain text file run.php Class Class source

  Files folder image Files (72)  /  examples  /  calculator  
File Role Description
  Plain text file CalcTool.php Class Class source
  Accessible without login Plain text file run.php Example Example script
  Accessible without login Plain text file tasks.json Data Auxiliary data

  Files folder image Files (72)  /  examples  /  factcheck  
File Role Description
  Plain text file Domain.php Class Class source
  Accessible without login Plain text file run.php Example Example script
  Accessible without login Plain text file tasks.json Data Auxiliary data

  Files folder image Files (72)  /  src  
File Role Description
Files folder imageAgent (8 files)
Files folder imageContracts (7 files)
Files folder imageCore (1 file)
Files folder imageDomains (2 files)
Files folder imageTools (2 files)
Files folder imageUtils (1 file)

  Files folder image Files (72)  /  src  /  Agent  
File Role Description
  Plain text file AgentOptions.php Class Class source
  Plain text file AgentSystem.php Class Class source
  Plain text file Llm.php Class Class source
  Plain text file LlmConfig.php Class Class source
  Plain text file LlmWithTools.php Class Class source
  Plain text file MetaAgent.php Class Class source
  Plain text file TaskAgent.php Class Class source
  Plain text file ToolRegistry.php Class Class source

  Files folder image Files (72)  /  src  /  Contracts  
File Role Description
  Plain text file AIMessage.php Class Class source
  Plain text file BaseChatModel.php Class Class source
  Plain text file BaseTool.php Class Class source
  Plain text file HumanMessage.php Class Class source
  Plain text file Message.php Class Class source
  Plain text file SystemMessage.php Class Class source
  Plain text file ToolMessage.php Class Class source

  Files folder image Files (72)  /  src  /  Core  
File Role Description
  Plain text file GenerateLoop.php Class Class source

  Files folder image Files (72)  /  src  /  Domains  
File Role Description
  Plain text file Base.php Class Class source
  Plain text file Harness.php Class Class source

  Files folder image Files (72)  /  src  /  Tools  
File Role Description
  Plain text file BashTool.php Class Class source
  Plain text file EditorTool.php Class Class source

  Files folder image Files (72)  /  src  /  Utils  
File Role Description
  Plain text file Archive.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0