carmelosantana / php-agents
PHP 8.4+ agent framework — interfaces, tools, and providers for building AI agents
Requires
- php: ^8.4
- ext-mbstring: *
- psr/log: ^3.0
- symfony/http-client: ^7.0 || ^8.0
Requires (Dev)
- pestphp/pest: ^4.0
- phpstan/phpstan: ^2.0
Suggests
- hkulekci/qdrant: Vector similarity search for persistent agent memory
- yethee/tiktoken-php: Accurate token counting for OpenAI models (cl100k_base, o200k_base)
Provides
None
Conflicts
None
Replaces
None
- dev-main
- v0.19.0
- v0.18.1
- v0.18.0
- v0.17.0
- v0.16.1
- v0.16.0
- v0.15.2
- v0.15.1
- v0.15.0
- v0.14.1
- v0.14.0
- v0.13.2
- v0.13.1
- v0.13.0
- v0.12.4
- v0.12.3
- v0.12.2
- v0.12.1
- v0.12.0
- v0.11.0
- v0.10.3
- v0.10.2
- v0.10.1
- v0.10.0
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.3
- 0.7.1
- 0.7.0
- 0.6.1
- 0.6.0
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.1
- v0.1.0
- dev-feat/usage-listener
- dev-release/0.18.1
- dev-feat/streaming-events
- dev-claude/php-agents-cleanup-9849e9
- dev-fix/pentest-hardening
- dev-fix/anthropic-api-gaps
- dev-fix/provider-request-limits
- dev-fix/gemini-structured-properties
- dev-feat_mcp-client
- dev-feat_claude-cli
- dev-feat_llama-cpp
- dev-chore_adds-json-string-support
- dev-feat_param-validation-metadata
- dev-feat_extend-system-prompt
- dev-chore_refactor-model-def
This package is auto-updated.
Last update: 2026-11-24 19:58:28 UTC
README
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
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
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
AI attempts to teach itself to |
Building with php-agents? Lets us know on Discord or open a PR to add your project to the list!
License
MIT