Sending user messages to an LLM is easy. Sending them without leaking names, ID numbers, IBANs and emails to a third-party API is harder. Plain redaction (“[NAME]”) protects the data but breaks the answer: the model can no longer say “Dear John” or refer back to a specific account.
Laranon, a new Laravel package featured on Laravel News on 20 November 2026, takes a different approach: reversible anonymization. It swaps personal data for stable tokens, sends the tokenised text out, and restores the real values in the response. The token map never leaves your server.
This tutorial shows how to install Laranon, wrap an LLM call, keep tokens consistent across a chat, stream responses safely and scrub logs and outgoing HTTP.
What Laranon does
Given this text:
Our client John Smith, SSN 536-90-4399, requests the transfer to GB29 NWBK 6016 1331 9268 19.
Laranon produces:
Our client «PER_1» «AP_1», SSN «SSN_1», requests the transfer to «IBAN_1».
The LLM works with the tokens, and Laranon puts the original values back afterwards. A few design choices stand out in the README:
- Checksum validation, not just regex. IBANs are checked with mod-97, credit cards with Luhn plus IIN, and Spanish DNI/NIE/CIF numbers with their control letters. Invalid look-alikes are not flagged, which cuts false positives.
- Per-word name tokens. “John Smith” becomes
«PER_1» «AP_1». A later bare “John” reuses«PER_1», and the package never guesses identities. - Stable tokens in a scope. The same value always gets the same token within a conversation.
- Exact restoration. Each token maps back to the literal original text.
- Language packs. English and Spanish packs ship with the package, plus universal detectors for email, IBAN, cards, phones, IPs, URLs and BIC/SWIFT.
Install
Laranon requires PHP 8.2+ and Laravel 12+ (per its composer.json).
composer require edulazaro/laranon
# optional: config file, and the vault migration if you use the database store
php artisan vendor:publish --tag=laranon-config
php artisan vendor:publish --tag=laranon-migrations
The basic round trip
use EduLazaro\Laranon\Laranon;
$result = Laranon::anonymize($request->input('message'));
// $result->text is safe to send to the model
$reply = $llm->send($result->text);
// put the real values back before showing the user
$restored = $result->restore($reply);
There are also helpers if you prefer functions: anon($text, $scope = null) and deanon($text, $map = null, $scope = null).
Sessions: the recommended way for chat
A chat turn usually has several pieces of text: the user message, retrieved context and tool results. You want one map shared by all of them, and you want it to disappear when the request ends. Laranon calls this a session:
use EduLazaro\Laranon\Anonymizer;
$anon = Anonymizer::create(); // the map lives inside $anon
// anonymize the 'content' key of every message
$messages = $anon->anonymize($messages, 'content');
$response = $llm->chat($messages);
// a tool needs real values to run its search
if ($response->toolCall) {
$args = $anon->restore($response->toolCall, 'arguments');
$result = runTool($args);
$messages[] = ['role' => 'tool', 'content' => json_encode($result)];
$messages = $anon->anonymize($messages, 'content'); // same map, new PII covered
$response = $llm->chat($messages);
}
$reply = $anon->restore($response->content);
// $anon goes out of scope here; nothing was persisted
anonymize() accepts a string, a list of strings, or an array of records with a key path. Dot paths (payload.text), several keys, and * wildcards (tool_calls.*.arguments) are supported. Only string values at those paths are changed; roles, IDs and tool names are left alone.
Because you keep chat history in the clear in your own database, each new turn can create a fresh session and re-anonymize the whole prompt. The README notes tokens come out identical in reading order, so the conversation stays consistent without storing a map.
Scopes for queued jobs and longer workflows
If the restore happens later, for example in a queued job, use a named scope. The map is stored encrypted with your app key in a vault: cache (default, with TTL), database (survives days) or array (single request).
// Turn 1
Laranon::scope("case-{$case->id}")->anonymize('SSN 536-90-4399'); // «SSN_1»
// Later, same scope: same token
Laranon::scope("case-{$case->id}")->anonymize('On file: 536-90-4399'); // «SSN_1»
// Restore without carrying the map around
$final = Laranon::scope("case-{$case->id}")->restore($llmResponse);
// Drop the map when you're done
Laranon::scope("case-{$case->id}")->forget();
Calling forget() matters for compliance: once the map is gone, the tokenised copies held by third parties can no longer be linked back to the person.
Choosing a strategy
Laranon::strategy('faker')->anonymize($text); // realistic surrogates, reversible
Laranon::strategy('redact')->anonymize($text); // [DNI], one-way, nothing stored
token(default):«PER_1»-style tokens, best for LLM round trips.faker: realistic fake values in the same format, reversible, for documents that must read naturally.redact: one-way labels, best for logs and outbound data you never need back.
Telling Laranon what you already know
Detection is good, but exact matching is better. Pass values you already hold:
Laranon::withEntities([
['value' => 'Acme Health Ltd', 'type' => 'entity'],
['value' => '[email protected]', 'type' => 'email'],
])->anonymize($text);
Or mark model attributes with the Anonymizable trait:
use EduLazaro\Laranon\Concerns\Anonymizable;
class Client extends Model
{
use Anonymizable;
protected array $anonymizable = ['email', 'phone'];
}
Laranon::withModels($case->clients)->anonymize($text);
You can also limit detectors with Laranon::only(['iban', 'email']) or Laranon::except('url').
Streaming responses safely
With server-sent events, a token like «PER_1» can be split across two chunks (even inside the multibyte «). Laranon’s stream restorer buffers partial tokens:
$stream = Laranon::stream($result);
foreach ($chunks as $chunk) {
echo $stream->push($chunk);
}
echo $stream->flush();
Scrubbing logs and outbound HTTP
Two one-line integrations cover common leaks:
// config/logging.php
'stack' => [
'driver' => 'stack',
'tap' => [\EduLazaro\Laranon\Integrations\ScrubLogsTap::class],
],
Http::scrubPii()->post($webhookUrl, $payload); // one-way redaction of the body
Audit detection before you trust it
Run the scanner over a sample of your real data to see what would be replaced:
php artisan laranon:scan storage/corpus.txt --types=iban,person,email
It prints each match with type, offset and confidence, plus a per-type summary. Use it to check recall on your own data, especially for names, which rely on bundled name dictionaries.
Things to keep in mind
- Detection is locale-based. Only English and Spanish packs ship today; you can add your own packs and recognizers.
- Ambiguous formats such as postal codes and passports are context-gated on purpose, so some will not be caught without surrounding clues. Feed known values with
withEntities()where it matters. - Anonymization reduces risk; it is not a full compliance programme. Keep your data-processing agreements in place.
Takeaway
Laranon lets a Laravel app use LLMs and third-party APIs without handing over raw personal data, while still getting useful, personalised answers back. Start with a per-request Anonymizer::create() session for chat, use scopes for queued work, and run laranon:scan on real samples before going live. Full docs are in the GitHub repository.
