DEV Community

Khaled Hammrouni
Khaled Hammrouni

Posted on

I built a PHP AI agent library with zero dependencies - here's the code

If you've tried to add an LLM agent to a PHP application, you've probably hit the same wall: every serious library is Python. So the "obvious" answer is to bolt a Python microservice behind your PHP app a sidecar that speaks to your agent, your agent speaks to your APIs, and now you're maintaining two runtimes, two deployments, and a network hop you didn't ask for.

I got tired of that. So I built NanoAgent: a PHP library that does agents natively. One composer require. Zero runtime dependencies. No Python. No framework. No DSL.

composer require hammrouni/nanoagent
Enter fullscreen mode Exit fullscreen mode

What it does

  • One config array, any provider. OpenAI, Groq, Anthropic, DeepSeek, OpenRouter or a local model behind any OpenAI-compatible endpoint (Ollama, llama.cpp server). Swap with one line.
  • Tool-first. You map plain PHP functions as tools. Your agent can query your database, hit your internal APIs, read your device telemetry the code you already wrote.
  • MCP support. Register any Model Context Protocol server (Streamable HTTP); its tools behave exactly like local ones, with an allowlist so you control what gets registered.
  • Persistent memory. Conversations survive across requests via pluggable drivers: JSON files, SQLite, MySQL, PostgreSQL or your own.
  • Minimal footprint. One package, no hidden magic. You can audit the whole thing in an afternoon (it's small on purpose).

The 30-second tour

<?php
use NanoAgent\Agent;

$agent = new Agent(llm: [
    'provider' => 'groq',
    'model'    => 'llama-3.3-70b-versatile',
    'api_key'  => getenv('GROQ_API_KEY'),
    // 'base_url' => 'http://localhost:8080/v1',  // …or a local model server
]);

// Your plain PHP function, now a tool:
$agent->tool('weather', fn (string $city) => "It is 22°C in {$city}.");

echo $agent->chat('How warm is it in Marrakech?');
Enter fullscreen mode Exit fullscreen mode

That's it. The agent decides to call weather, your function runs in-process, the result comes back, and the reply is generated. No sidecar, no queue, no second language.

Why PHP, really?

Because a huge amount of production AI isn't greenfield. It's Laravel services talking to legacy databases, internal HTTP APIs, and ten years of business logic. The agent that's useful there is not a shiny framework it's a boring, local library that lives inside the process you already run. NanoAgent is deliberately boring. Boring is the feature.

What I'm not doing

No auto-generated prompts, no "magic" abstractions, no 14 dependencies. If you want a framework, there are great ones. If you want a library you can read end-to-end, point at a hosted API or a model on your own hardware, and ship in an afternoon that's what this is.

Try it

MIT licensed. Issues and PRs welcome.

Full 12-part series on building agents in PHP is linked in my profile this post is the short version.

Top comments (0)