Skip to content
Leonidov.DEV

LLM in PHP: 5 Simple Reasons to Drop Python

Engineering
LLM in PHP: 5 Simple Reasons to Drop Python

The integration has two shapes. With a sidecar, a PHP site talks to a Python service through a queue, and the release gains three moving parts: the PHP image, the Python image and the model file. In review that shape keeps producing the same bill: a second deploy, a second log file and a health endpoint nobody remembers to monitor.

Code: FerryAI, MIT licensed.

The LLM in PHP approach removes the middle. FerryAI loads llama.cpp and ONNX Runtime through FFI, so the model call happens in the process that already serves the request. Five reasons decide the choice, and one case where Python stays.

1. One deploy instead of two

The strongest argument is not performance. It is the shape of the release.

With a sidecar, a feature ships when PHP, the Python image, the model file and the service config all agree. Any one of them out of step produces a version that half works, and the person deploying has to remember four things instead of one.

With FFI, the feature ships with the theme or the plugin. Composer pulls the package, the model folder sits next to the app, and the rollback is the same rollback you already trust.

2. The failure mode is boring

A sidecar fails by timing out, and a timeout tells you nothing about which stage broke. The HTTP layer, the queue, the model load, the tokeniser.

An exception in PHP comes with a stack trace, and with xdebug attached you can step into the call that produced it. That sounds like a small thing until the first production incident, where you have twenty minutes and one log file.

3. Memory behaves like memory

Every PHP-FPM worker loading its own copy of a model is a bad plan, and the library does not pretend otherwise. A bounded LRU pool keeps model instances alive between requests, and when the shared memory extension is available, workers share the weights instead of duplicating them.

The practical result: a warm page answers in milliseconds and a cold start is the only expensive request. That is easier to reason about than a sidecar whose worker count you tune separately from the web tier.

4. Structured output stops being a retry loop

This is the part that decides whether a local model is usable. Passing a JSON schema as a grammar constrains the sampler, so the model cannot emit a reply that fails to parse. No regex repair, no second attempt, no “sometimes it returns markdown” bug report.

For anything that writes to a database or calls an API, that constraint is worth more than a slightly smarter model behind an HTTP wrapper.

5. The retrieval layer stays where the data lives

Embeddings for a knowledge base belong next to the content they describe. With the vector store in the same PostgreSQL instance, a backup covers the vectors, the staging copy behaves like production, and a client’s host does not need a new service just to answer a question.

SQLite with sqlite-vec covers the smaller case: one file, no daemon, honest brute force when the extension is missing.

What LLM in PHP actually costs

Nothing here is free.

FFI has to be enabled, and some shared hosts disable it. Native libraries have to match the architecture, and a missing CUDA DLL produces an error that reads like a PHP problem and is not one. The package is an early release, so the API can change between versions and pinning is not optional.

There is also a ceiling. A 0.6B parameter model running on a CPU core answers slower and reasons worse than a hosted frontier model. If the feature needs careful multi-step reasoning, a local model will disappoint no matter how clean the integration is.

When I still reach for Python

Training, fine-tuning, and batch jobs measured in hours. A nightly job that re-embeds a growing corpus is easier to run from a CLI with a GPU and a retry policy than from a web request.

The line I use: if the work fits in a request and the model is already trained, it belongs in the process serving the page. If it needs a queue, it belongs outside.

That is also how I decide on architecture in general, which I wrote about in six architecture patterns and how to choose. The same instinct applies to reporting: a query that answers the question beats a service that wraps the query, as in one SQL query instead of a backend service.

Background: PHP FFI documentation and llama.cpp. The package is FerryAI on GitHub, MIT licensed.