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.
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.
1. Provider abstraction.
Swap providers without changing a line of application code. All providers implement ProviderInterface.
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.
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.
4. Declarative pipelines.
Compose complex AI workflows as chainable, traceable pipelines. Twelve built-in step types with full control flow.
5. Multi-agent crews.
Orchestrate multiple AI agents in four coordination modes.
6. Guardrails engine.
Protect inputs and outputs with six composable validators and six action modes.
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.
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.
💰 Cost & Budget
Track every request, aggregate by model or period, and enforce spend limits per scope.
Six memory strategies.
Each designed for a different context management pattern.
🔍 Embeddings & Vector Search
Cosine, Euclidean, and dot-product similarity. Plug any external vector DB via interface.
📡 MCP (Model Context Protocol)
The only PHP framework with first-class MCP server and client.
12. Middleware stack.
Onion-model pipeline for cross-cutting concerns.
And more.
Streaming & SSE
FakeProvider Testing
CLI Integration
Commands registered automatically when monkeyslegion-cli is installed.
Requirements
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.