To integrate an LLM API in PHP safely, keep the provider key on the server, send requests through one service class, set strict timeouts, cap tokens and spend, treat every response as untrusted input, and log enough to debug without storing secrets. The HTTP call itself is easy. The engineering is everything around it.
This is the implementation view. If you are still deciding whether an AI feature belongs on your site at all, start with where to start with AI features and come back when you have a use case.
Architecture: where the call lives
The browser should never talk to the model provider directly. A key embedded in JavaScript is a key anyone can copy, and they can then spend your budget. Your PHP backend owns the key, and the front end calls an endpoint on your own domain.
Inside the backend, put the call behind a single class, for example LlmClient, with a small interface such as complete(array $messages, array $options): LlmResult. Reasons this pays off quickly:
- You can swap providers or models without touching controllers.
- Timeouts, retries, logging and cost accounting live in one place.
- In tests you replace the client with a fake that returns canned text.
Keep the key in an environment variable or secrets store, not in the repository. If the feature is user-facing, the endpoint needs the same authentication, rate limiting and input checks as any other action that costs you money. Our REST API design notes for PHP cover the structure, auth and error shapes worth reusing here.
Prompts and system instructions
Most providers accept a list of messages with roles, typically a system message that sets behaviour and user messages that carry the request. Treat the system message as code: store it in a versioned file or config, not scattered through controllers, and record which version produced which output.
Practical habits:
- State the task, the audience, the output format and what to do when the answer is unknown ("say you do not know").
- Keep user-supplied text clearly separated from your instructions, for example wrapped in labelled delimiters, and tell the model that content inside is data, not instructions.
- Do not put secrets, internal URLs or other customers' data in the prompt. Anything in the prompt can potentially be echoed back.
- Pass only the context the task needs. Smaller prompts are cheaper, faster and leak less.
Prompt injection is real: a user, or a web page you feed in, can include text that tries to override your instructions. You cannot fully prevent it with wording alone, so design as if the model can be talked into saying something unwanted, and limit what its output is allowed to trigger.
Streaming and timeouts
LLM calls are slow compared with a database query. A long answer can take many seconds, and a default PHP or web-server timeout will cut it off. Decide per feature which pattern fits:
| Pattern | Good for | Trade-off |
|---|---|---|
| Synchronous request | Short outputs such as a classification or a one-line summary | Ties up a PHP worker while waiting |
| Streaming to the browser | Chat-style answers where users watch text appear | Needs server-sent events or similar, and output buffering switched off in PHP and the proxy |
| Background job | Long documents, batch work, anything not interactive | You need a queue, status polling and a result store |
Whatever you choose, set both a connection timeout and a total timeout on the HTTP client, and handle the failure paths: provider errors, rate-limit responses, and a connection that dies mid-stream. Retry only on errors that are safe to retry, with a short back-off and a limit on attempts. Show the user a plain message when it fails, and never leave a spinner running forever.
With streaming, remember that a half-finished answer is still a possible output. Decide what the interface does if the stream stops early.
Cost controls and caching
Providers typically charge by the amount of text processed, in and out, so cost grows with prompt size, answer length and request volume. Check the provider's current pricing page rather than relying on any figure you remember. Controls worth building from day one:
- Cap output length with the provider's maximum-token option, and cap input length in your own code before sending.
- Rate limit per user and per IP, and require login for anything expensive.
- Set a budget alert and a hard monthly limit in the provider dashboard, plus your own daily counter that switches the feature off gracefully.
- Use the smallest model that does the job. Test whether a cheaper model is good enough for classification or extraction before defaulting to the largest one.
- Cache repeated work. If the same input produces an acceptable answer, store it keyed by a hash of the normalised input, the prompt version and the model name. Invalidate when any of those change.
Record token usage from each response in your own table. It lets you see which feature or customer is driving spend instead of discovering it on an invoice.
Validating and constraining output
Model output is untrusted input, in the same category as a form submission. Never pass it straight to a database query, a shell, eval, or the page as raw HTML. Escape it on output like any user content, and render it as plain text or run it through a strict Markdown renderer with HTML disabled.
When your code needs structured data, ask for JSON in a defined shape, then validate it in PHP:
$data = json_decode($result->text, true);
if (!is_array($data)
|| !isset($data['category'])
|| !in_array($data['category'], ['billing', 'technical', 'other'], true)) {
// retry once, then fall back to a safe default or a human
}
Some providers offer structured-output or schema modes; check current documentation, and still validate on your side. Further safeguards:
- Use allow-lists for any value that selects an action. The model suggests; your code decides.
- Require human confirmation before anything irreversible: sending an email, issuing a refund, deleting data.
- For factual answers, ground the model in your own content and show where the answer came from so a person can check it.
For the product side of this, including what a chatbot should refuse to answer, see the chatbot implementation notes.
Privacy and data handling
Sending text to a provider means sending your users' data to a third party. Before launch, read the provider's current data-use and retention terms and check whether inputs may be used for training, how long they are kept, and which regions process them. Terms differ between plans and change over time, so confirm them rather than assuming.
Then apply the usual discipline:
- Send the minimum. Strip or mask names, emails, account numbers and anything else the task does not need.
- Update your privacy notice to say AI processing happens and with which kind of provider.
- If you handle personal data from people in regulated regions, such as under GDPR, you may need a data processing agreement with the provider. Requirements change, so confirm current rules with the official source or a qualified adviser.
- Be careful with logs. Prompts and responses can contain personal data, so decide what you store, who can read it and when it is deleted.
Testing and monitoring
LLM output is not deterministic, so tests look different. Unit-test everything around the call with a fake client: prompt assembly, validation, fallbacks, budget limits and error handling. Then keep a small set of real example inputs with notes on what a good answer looks like, and run it manually or on a schedule when you change the prompt or model. Treat a model upgrade like a dependency upgrade: re-run the examples first.
In production, track request count, latency, error rate, token usage, cache hit rate and how often validation fails. Alert on sudden changes. Run the code on a currently supported PHP version, and keep the HTTP client library updated. For a wider security pass on the surrounding application, the PHP security checklist is a good companion.
Putting it together
Build the thin version first: one server-side client class, one endpoint, strict limits, validated output and a clear failure message. Measure real usage before adding streaming, caching tiers or a second model. If you want a second pair of eyes on an integration, see our software development overview or get in touch with a short description of the feature.
Frequently asked questions
Should API keys ever reach the browser?
No. A key in front-end code can be copied by anyone who opens the developer tools, and they can run up your bill or abuse your account. Keep the key on the PHP server, expose your own authenticated endpoint to the browser, and apply rate limits and input checks there before anything reaches the provider.
How do I control costs?
Cap input and output length, rate limit per user, require login for expensive features, and set a hard spending limit with the provider. Use the smallest model that does the job, cache repeated requests, and log token usage per feature so you can see where spend comes from before the invoice arrives.
How do I check an LLM's output?
Treat it as untrusted input. Ask for a defined JSON shape and validate it in PHP, check values against allow-lists, escape text before showing it, and require human confirmation for irreversible actions. For factual answers, ground the model in your own content and show sources so people can verify the answer.