📦 Marketplace⭐ GitHub
First in PHPmonkeyslegion-apex@1.0.1363 tests705 assertions

The first PHP framework with an AI orchestration engine built in.

Not a wrapper. A complete AI infrastructure layer with multi-provider routing, declarative pipelines, guardrails, agent crews, and cost management. What takes five packages in Python and custom code in Node.js, MonkeysLegion Apex does in one.

$composer require monkeyscloud/monkeyslegion-apex
View on GitHub →

The problem Apex solves.

Building an AI product in PHP used to mean one of two things: call OpenAI's HTTP API directly and rebuild everything yourself — retries, token counting, cost tracking, PII filtering, fallback providers, tool calling schemas, streaming — or shell out to a Python microservice and pay the latency and operational tax forever.

Apex removes that choice. It's the abstraction layer PHP has been missing — one that treats AI models as first-class infrastructure, not an HTTP endpoint.

Architecture layers.

Concentric rings — each layer is useful on its own, each can be swapped via interface.

Pipelines · Crews · MCP · HTTP Controllers← Orchestration
Router · Fallback · Middleware · Guardrails · Budget← Infrastructure
Memory · Tools · Schemas · Streaming · Embeddings← Primitives
Anthropic · OpenAI · Google · Vertex · Ollama · Fake← Providers

Fifteen capabilities. One package.

Everything an AI-native PHP application needs — first-party, tested together, versioned together.

🔌

Provider Abstraction

Swap Claude, GPT, Gemini, or Ollama in one line. All implement ProviderInterface.

📐

Structured Output

Extract type-safe PHP objects from LLM responses with Schema attributes.

🔧

Tool Calling

Register PHP methods as LLM-callable tools with #[Tool] attributes.

🔗

Declarative Pipelines

Compose AI workflows as chainable, traceable steps. 12 built-in types.

🤖

Multi-Agent Crews

Orchestrate agents in Sequential, Parallel, Hierarchical, or Conversational mode.

🛡️

Guardrails Engine

6 composable validators with Block, Redact, Warn, Truncate, Replace, Retry.

🧭

Smart Model Router

Auto-pick the best model per-request. 4 strategies + custom routing rules.

💰

Cost & Budget

Track every request, aggregate by model, enforce per-scope spend limits.

🧠

6 Memory Strategies

Conversation, Sliding Window, Summary, Vector, Persistent, Agent Memory.

🔍

Embeddings & Search

In-memory vector store with cosine, Euclidean, and dot-product similarity.

📡

MCP Protocol

First-class MCP server + client. The only PHP framework with this today.

Middleware Stack

Rate limit, retry, cache, guardrails, cost budget, telemetry, fallback.

📺

Streaming & SSE

Real-time chunk iteration and Server-Sent Events for web UIs.

🧪

FakeProvider Testing

Zero-API-call testing with queued responses, failures, and call inspection.

⌨️

CLI Integration

Interactive REPL, model switching, cost reports — registered automatically.

Ready to see the code?

Dive into the detailed API for each of the fifteen capabilities below.

Explore API ↓GitHub →
Provider Layer

1. Provider abstraction.

Swap providers without changing a line of application code. All providers implement ProviderInterface.

Claude (Opus 4, Sonnet 4, Haiku 4)
GPT (4.1, 4.1-mini, 4.1-nano, o3, o4-mini)
Gemini (2.5 Pro, 2.5 Flash, AI Studio + Vertex)
Ollama (any local model)
Provider swap
php
use MonkeysLegion\Apex\AI;
use MonkeysLegion\Apex\Provider\Anthropic\AnthropicProvider;
use MonkeysLegion\Apex\Provider\OpenAI\OpenAIProvider;
use MonkeysLegion\Apex\Provider\Google\GoogleProvider;

// Pick one — the rest of your code doesn't change
$provider = new AnthropicProvider(
    apiKey: $_ENV['ANTHROPIC_API_KEY'],
    model: 'claude-sonnet-4',
);

$ai = new AI($provider);
$response = $ai->generate('Explain PHP 8.4 property hooks');
Schema extraction
php
final class SentimentResult extends Schema
{
    public function __construct(
        #[Description('Detected sentiment')]
        #[Constrain(enum: ['positive', 'negative', 'neutral'])]
        public readonly string $sentiment,

        #[Description('Confidence 0–1')]
        #[Constrain(min: 0.0, max: 1.0)]
        public readonly float $confidence,

        #[Description('Key phrases')]
        #[ArrayOf('string')]
        public readonly array $phrases = [],
    ) {}
}

$result = $ai->extract(SentimentResult::class, 'Amazing!');
echo $result->sentiment;   // 'positive'
Primitives Layer

2. Structured output with schemas.

Extract type-safe PHP objects from LLM responses. No more json_decode() + manual validation. Nested schemas, automatic retry with validation feedback, and JSON Schema export are all built in.

Primitives Layer

3. Tool calling with attributes.

Register PHP methods as LLM-callable tools using #[Tool] and #[ToolParam] attributes. Apex compiles the schema per-provider automatically. For autonomous multi-step agents, use MultiStepRunner.

Tool definition
php
use MonkeysLegion\Apex\Tool\Attribute\{Tool, ToolParam};

final class WeatherTools
{
    #[Tool(name: 'get_weather', description: 'Get current weather')]
    public function getWeather(
        #[ToolParam(description: 'City name', required: true)]
        string $city,
        #[ToolParam(enum: ['celsius', 'fahrenheit'])]
        string $unit = 'celsius',
    ): array {
        return ['city' => $city, 'temp' => 22, 'unit' => $unit];
    }
}

$response = $ai->generate(
    "What's the weather in Tokyo?",
    options: ['tools' => [new WeatherTools()]],
);
Pipeline composition
php
use MonkeysLegion\Apex\Pipeline\Pipeline;
use MonkeysLegion\Apex\Pipeline\Step\{
    GenerateStep, SummarizeStep, TranslateStep, GuardStep
};

$result = Pipeline::create('research-and-translate')
    ->pipe(new GuardStep($guard, isInput: true))
    ->pipe(new GenerateStep($ai, system: 'Research thoroughly'))
    ->pipe(new SummarizeStep($ai, maxWords: 200))
    ->pipe(new TranslateStep($ai, 'Spanish'))
    ->pipe(new GuardStep($guard, isInput: false))
    ->run('Quantum computing in healthcare');

echo $result->output;        // Final translated result
echo count($result->trace);  // Step-by-step trace
Orchestration Layer

4. Declarative pipelines.

Compose complex AI workflows as chainable, traceable pipelines. Twelve built-in step types with full control flow.

Generate
Extract
Classify
Summarize
Translate
Guard
Conditional
Loop
Parallel
Transform
HumanInLoop
Route
Orchestration Layer

5. Multi-agent crews.

Orchestrate multiple AI agents in four coordination modes.

SequentialParallelHierarchicalConversational
Agent crew
php
use MonkeysLegion\Apex\Agent\{Agent, Crew};
use MonkeysLegion\Apex\Enum\AgentProcess;

$crew = new Crew('content-team', [
    new Agent('researcher', 'Research with academic rigor.', $ai),
    new Agent('writer', 'Write engaging, clear content.', $ai),
    new Agent('editor', 'Edit for grammar and tone.', $ai),
], AgentProcess::Sequential);

$results = $crew->run('Blog post on PHP 8.4 property hooks');
// researcher → writer → editor → final output
Guard pipeline
php
$pipeline = GuardPipeline::create()
    ->add(new PromptInjectionValidator(), GuardAction::Block)
    ->add(new PIIDetectorValidator(),     GuardAction::Redact)
    ->add(new ToxicityValidator(),        GuardAction::Warn)
    ->add(new WordCountValidator(max: 500), GuardAction::Truncate);

$result = $pipeline->run($text);
Infrastructure Layer

6. Guardrails engine.

Protect inputs and outputs with six composable validators and six action modes.

PII Detection
Prompt Injection
Toxicity Filter
Regex Rules
Word Count
Custom Callable

One package. Zero stitching.

Every ⚠️ or ❌ in the alternatives column is a decision your team has to make. Apex removes those decisions from your backlog.

$composer require monkeyscloud/monkeyslegion-apex

Infrastructure built in.

🧭 Smart Model Router

Auto-pick the best model per-request based on input complexity. Fall back to other providers when one fails.

php
$router = ModelRouter::create()
    ->tier('fast',     ['claude-haiku-4', 'gpt-4.1-nano', 'gemini-2.5-flash'])
    ->tier('balanced', ['claude-sonnet-4', 'gpt-4.1', 'gemini-2.5-pro'])
    ->tier('power',    ['claude-opus-4', 'o3'])
    ->strategy(RouterStrategy::CostOptimized);
CostOptimizedQualityFirstLatencyFirstRoundRobin

💰 Cost & Budget

Track every request, aggregate by model or period, and enforce spend limits per scope.

php
$tracker = new CostTracker(new PricingRegistry());
$ai = new AI($provider, costTracker: $tracker);

$budget = new BudgetManager();
$budget->setBudget('tenant:acme', 100.00);
$budget->charge('tenant:acme', 'claude-sonnet-4', $usage);

$report = CostReport::generate($tracker->all());

Six memory strategies.

Each designed for a different context management pattern.

StrategyUse case
ConversationMemoryFull transcript, unbounded
SlidingWindowMemoryLast N messages or tokens — whichever hits first
SummaryMemoryAuto-summarize older messages to stay under token limits
VectorMemoryRetrieve relevant past messages via embedding similarity
PersistentMemoryPSR-16-backed — survives across HTTP requests
AgentMemoryIsolated per-agent memory with system-prompt injection

🔍 Embeddings & Vector Search

php
$vectors = $ai->embed(['PHP 8.4 Guide', 'AI in PHP']);

$store = new InMemoryStore();
$store->add('doc-1', $vectors[0]->values, ['title' => 'PHP 8.4']);

$query = $ai->embed(['property hooks'])[0];
$results = $store->search($query->values, topK: 3);

Cosine, Euclidean, and dot-product similarity. Plug any external vector DB via interface.

📡 MCP (Model Context Protocol)

php
// Expose tools to Claude Desktop, IDEs, etc.
$server = new MCPServer();
$server->tool('search_docs', 'Search docs', $schema,
    fn($args) => searchDocs($args['query']));

// Connect to external MCP servers
$client = new MCPClient('http://localhost:8080/mcp');
$tools  = $client->listTools();

The only PHP framework with first-class MCP server and client.

Infrastructure Layer

12. Middleware stack.

Onion-model pipeline for cross-cutting concerns.

RateLimitMiddleware
RetryMiddleware
CacheMiddleware
InputGuardMiddleware
OutputGuardMiddleware
CostBudgetMiddleware
TelemetryMiddleware
FallbackMiddleware
Middleware pipeline
php
$pipeline = (new MiddlewarePipeline())
    ->push(new RateLimitMiddleware(maxRequests: 60))
    ->push(new RetryMiddleware(maxRetries: 3))
    ->push(new CacheMiddleware($cache, ttl: 3600))
    ->push(new InputGuardMiddleware($guard))
    ->push(new OutputGuardMiddleware($guard))
    ->push(new CostBudgetMiddleware($tracker, maxBudget: 100.0))
    ->push(new TelemetryMiddleware($logger))
    ->push(new FallbackMiddleware($backupProvider));

And more.

📺

Streaming & SSE

php
$stream = $ai->stream('Write a story');
foreach ($stream as $chunk) {
    echo $chunk->delta;
    flush();
}

// SSE for web UIs
(new AIStreamResponse($stream))->send();
🧪

FakeProvider Testing

php
$fake = FakeProvider::create()
    ->respondWith('First response')
    ->failWith(new ProviderException('Rate limited'))
    ->respondWith('Retry success');

$ai = new AI($fake);
echo $fake->calledTimes(); // 1
⌨️

CLI Integration

bash
php ml ai:chat
php ml ai:chat --model=gpt-4.1
php ml ai:costs
php ml ai:costs --format=json

Commands registered automatically when monkeyslegion-cli is installed.

Requirements

PHP 8.4+ext-curlext-jsonext-mbstringpsr/log ^3.0psr/simple-cache ^3.0

Optional: monkeyslegion-cli · monkeyslegion-telemetry · monkeyslegion-cache ^2.0 · ext-pcntl + ext-sockets

Start building with Apex.

One package, one version, one test suite, one team behind it.

$composer require monkeyscloud/monkeyslegion-apex
Star on GitHub ⭐Packagist →Framework home →