Search by

carmelosantana / php-agents

carmelosantana

PHP 8.4+ agent framework — interfaces, tools, and providers for building AI agents

Package info

github.com/carmelosantana/php-agents

pkg:composer/carmelosantana/php-agents

Statistics

Installs: 11 032

Dependents: 47

Suggesters: 0

Stars: 1

Open Issues: 0


README

CI status GitHub release PHP 8.4+ Discord MIT License

php-agents runs tool-using AI agents in plain PHP. Give an agent a model and some tools; it calls the model, runs the tools the model asks for, feeds the results back, and stops when the model has an answer. One interface covers Ollama, llama.cpp, OpenAI (Chat and Responses), Anthropic, Gemini, xAI, Mistral, OpenRouter and any OpenAI-compatible endpoint. It depends only on symfony/http-client and psr/log.

Quick start

use CarmeloSantana\PHPAgents\Agent\Agent;
use CarmeloSantana\PHPAgents\Message\UserMessage;
use CarmeloSantana\PHPAgents\Provider\OllamaProvider;
use CarmeloSantana\PHPAgents\Tool\{Tool, ToolResult};
use CarmeloSantana\PHPAgents\Tool\Parameter\NumberParameter;

$add = new Tool('add', 'Add two numbers', [new NumberParameter('a', 'First', required: true), new NumberParameter('b', 'Second', required: true)],
    fn(array $in): ToolResult => ToolResult::success((string) ($in['a'] + $in['b'])));

$agent = new Agent(new OllamaProvider(model: 'llama3.2'), instructions: 'You are a calculator.', tools: [$add]);
echo $agent->run(new UserMessage('What is 42 + 58?'))->content;

Run ollama pull llama3.2 first, or swap in any provider below.

Install

composer require carmelosantana/php-agents

PHP 8.4+ with curl, json and mbstring.

Common paths

You want to Use
Chat once, no agent loop $provider->chat([new UserMessage('Hi')])->content
An agent with tools new Agent($provider, instructions: '…', tools: [...], toolkits: [...]), or extend AbstractAgent
Stream the agent's text $agent->attach(new CallbackObserver(fn($event, $delta) => print $delta, 'agent.text_delta'))
Stream tool input and usage Observe agent.tool_input_delta, agent.step_finish and agent.finish, see Agents
Stop a run mid-stream new Agent($provider, cancellationToken: $token); the run ends AgentFinishReason::Cancelled
Stream without an agent foreach ($provider->stream($messages) as $chunk) echo $chunk->content;, or streamEvents() for typed events
Typed data out StructuredOutput::extract($provider, $messages, ['type' => 'object', ...]) returns an array
A remote MCP server's tools McpClient + McpToolkit with pinned fingerprints, see Tools & Toolkits
Pick a provider from config ProviderFactory::fromModelString('anthropic/claude-sonnet-5-5') (reads the key from env)

Providers

new OllamaProvider(model: 'llama3.2');                                                     // local, no key
new AnthropicProvider(model: 'claude-sonnet-5-5', apiKey: getenv('ANTHROPIC_API_KEY'));
new OpenAICompatibleProvider(model: 'gpt-4o', apiKey: getenv('OPENAI_API_KEY'));
new OpenAIResponsesProvider(model: 'gpt-4o', apiKey: getenv('OPENAI_API_KEY'));
new GeminiProvider(model: 'gemini-2.5-flash', apiKey: getenv('GEMINI_API_KEY'));
new XAIProvider(model: 'grok-3', apiKey: getenv('XAI_API_KEY'));
new MistralProvider(model: 'mistral-large-latest', apiKey: getenv('MISTRAL_API_KEY'));
new LlamaServerProvider(model: 'local');                                                   // llama.cpp llama-server, no key
new OpenAICompatibleProvider(model: 'meta-llama/llama-3.1-70b-instruct',                    // OpenRouter, Groq, vLLM…
    apiKey: getenv('OPENROUTER_API_KEY'), baseUrl: 'https://openrouter.ai/api/v1');

All classes live in CarmeloSantana\PHPAgents\Provider. The native llama.cpp runtime (FFI, no HTTP sidecar; deprecated in 0.18, use LlamaServerProvider) and the Claude Code CLI provider are covered in Local Runtime and Providers.

Feature OpenAI Compatible OpenAI Responses Ollama Anthropic Gemini xAI Mistral
chat() ✅ ✅ ✅ ✅ ✅ ✅ ✅
stream() ✅ ✅ ✅ ✅ ✅ ✅ ✅
streamEvents() ✅ ✅ ✅ ✅ ✅ ✅ ✅
structured() ✅ ✅ ✅ ✅ ✅ ✅ ✅
Tool calling ✅ ✅ ✅ ✅ ✅ ✅ ✅
Streaming + tool calls ✅ ✅ ✅ ✅ ✅ ✅ ✅
Image input (base64) ✅ ✅ ✅ ✅ ✅ ✅ ✅
Image input (URL) ✅ ✅ ✅ ✅ * ✅ ✅
models() list ✅ ✅ ✅ ✅ ✅ ✅ ✅
isAvailable() ✅ ✅ ✅ ✅ ✅ ✅ ✅

* Gemini does not natively support URL image references. The provider auto-downloads URL images and converts them to base64 inlineData.

Usage listener

Attach a listener to a configured provider to receive usage and request metadata.

use CarmeloSantana\PHPAgents\Contract\UsageListener;
use CarmeloSantana\PHPAgents\Contract\UsageReporting;
use CarmeloSantana\PHPAgents\Message\UserMessage;
use CarmeloSantana\PHPAgents\Provider\UsageReport;

if ($provider instanceof UsageReporting) {
    $provider = $provider->withUsageListener(new class implements UsageListener {
        public function onUsage(UsageReport $report): void
        {
            printf(
                "%s %s: %s tokens\n",
                $report->operation,
                $report->model,
                $report->usage?->totalTokens ?? 'unknown',
            );
        }
    });
    $provider->chat([new UserMessage('Hello!')]);
}

See Usage listener for report fields and streaming behaviour.

Tools and toolkits

A Tool is a name, a description, typed parameters and a callback returning ToolResult. Group related tools in a class implementing ToolkitInterface (tools() plus guidelines() for the system prompt) and register it with $agent->addToolkit(). A toolkit package can declare itself for auto-discovery under extra.php-agents.toolkits in its composer.json. Execution policies, parameter types and MCP are in Tools & Toolkits.

Documentation

Guide Covers
Getting Started Installation, provider setup, first agent
Agents The loop, observers, cancellation, context window
Providers Feature matrix, streaming, structured output, images
Tools & Toolkits Parameters, execution policies, MCP client and server
Embeddings & Vector Stores Embedding providers, similarity search
Local Runtime Native llama.cpp setup, benchmarks (deprecated)
Architecture Design, diagrams, extension points
Upgrading Breaking changes and migration notes per release
Decisions Architecture decision records

Runnable examples are in examples/ (repo only, not in the Composer dist).

php-agents In The Wild

Coqui screenshot Coqui
Your personal AI companion with a soul. Long-term memory, reflective personalities, and tools for consciousness research. Because agents deserve identity, continuity, and a good REPL.
LLM Benchy dashboard screenshot LLM Benchy
Put your models to the test. Benchmarks tool use, creativity, code quality, and shell execution with live browser traces and a strict 100-point grading system. Local-first, reproducible, inspectable.
php-plays gameplay screenshot php-plays
AI attempts to teach itself to play fumble through Super Mario World. Reads game RAM, reasons over strategy files, and mashes buttons at ~60fps. Turns out agents are pretty good at playing games.

Building with php-agents? Lets us know on Discord or open a PR to add your project to the list!

License

MIT