Why LLM Guard API memory use and model loading decide container stability
When an existing LLM Guard API container is OOM-killed or takes a long time to report ready, the cause is usually models loaded at startup and duplicated per worker, not traffic volume or a bug in a single request. Each scanner that needs a model loads it when the server starts, and a process model that forks independent workers can multiply that memory footprint. The fix is configuration plus right-sizing the container memory limit and startup budget. It does not require a new rollout, which matters because LLM Guard is archived and no new deployments are recommended.
LLM Guard can be used as a Python library installed with pip or deployed as an API server, and the two modes load models differently. A library embedded in your own application loads only the scanners you instantiate in that process. The API server loads the scanners named in its configuration file for every worker it starts. Before changing anything, record which scanners the container loads, how many workers run, the memory limit set by your orchestrator, and how long the container takes to pass its readiness check. Those four facts show whether the problem is a memory ceiling, a startup delay, or both.
Because the project is archived, keep changes minimal and document each one: the scanner list, the worker count, the memory limit, the preload setting and the image tag or pinned package version. The LLM Guard documentation describes gunicorn preloading, which loads models once in the parent and shares them with workers through copy-on-write memory. Whether that sharing holds in your image depends on how the models and Python objects are touched after load, so confirm the effect with a measurement in your own environment rather than assuming it. Treat the memory and readiness numbers you record now as the baseline for the decommission plan.
Which memory and loading option fits which symptom
This table compares four levers for out-of-memory restarts and slow starts in LLM Guard containers: container sizing, lazy loading, ONNX models from a mounted directory, and gunicorn preloading. Read each row by the symptom you see, then check the trade-off column before changing anything. Configuration keys differ between releases, so confirm each one against the version you deploy.
| Option | What it changes | Trade-off | When it fits |
|---|---|---|---|
| Right-size container memory | Gives the scanner process enough headroom to hold every loaded model at once | Higher hosting cost and no change to per-worker duplication; the 16GB guidance is a starting point to test, not a guarantee | Restarts show exit code 137 during startup or under load and no other fix is in place |
| Lazy loading of scanners | Defers loading each model until a scanner first runs, lowering idle and startup memory | First request that uses a scanner pays the load delay, which can cause timeouts on the first traffic after a restart | Only a subset of scanners is enabled, and slow first responses are acceptable |
| ONNX models from a mounted directory | Reads model files from a volume you control instead of downloading them at start | Adds image and volume management work, and each model update requires copying and checking files again | Starts are slow because of downloads, or hosts have limited or no outbound access |
| Gunicorn preloading | Loads models once in the master process so workers share them through copy-on-write memory | Shared memory is only as good as the pages that stay unchanged; per-worker allocations still duplicate, and preloading delays worker startup errors | Several workers run per container and memory grows roughly with worker count |
| Fewer workers or scanners | Cuts the number of model copies or loaded models in one container | Lower throughput per container and possible queueing under peak load | Traffic is modest or some scanners are not needed for the use case |
How to diagnose and change LLM Guard container memory settings
This sequence confirms what is running, measures where memory goes, and applies one memory change at a time with a restart and readiness check after each, so the team knows which change actually fixed the out-of-memory restarts or slow starts.
Confirm the deployed version and Python runtime
Record the installed LLM Guard version, the base image tag, and the Python interpreter inside the running container. The last LLM Guard release on PyPI is 0.3.16, uploaded on 19 May 2025, and it requires Python 3.10 or newer but below 3.13, so confirm the interpreter falls in that range before changing anything. Check the pip freeze output against the image you intend to keep. This step is done when the version, interpreter and image digest are written into the change record and match what the running container reports.
List enabled scanners and the models they load
Read the scanner configuration the API server loads, including every input and output scanner that is enabled. Note which scanners download or load a model, since each one adds memory at startup. The LLM Guard documentation describes 15 input scanners and 20 output scanners, and the models they use vary by scanner, so confirm the list against your own configuration file rather than the defaults. The step is done when you have a table of scanner name, direction, and model path or Hugging Face source for each one.
Measure container memory at idle and under load
Start the container with no traffic and record resident memory after the models finish loading, then send a representative mix of requests and record peak memory. Use the container runtime statistics or your metrics agent, sampling at a fixed interval. Also record how many gunicorn workers are running, since each worker that loads models adds its own footprint unless models are shared. The step is done when you have an idle figure, a loaded peak, and the restart count from the same run, which becomes your baseline.
Set a container memory limit that fits the baseline
Compare the loaded peak with the container memory limit and the host capacity. If the peak sits close to or above the limit, raise the limit to clear the peak with some headroom, or reduce the number of workers. The LLM Guard guidance for this use case refers to a 16GB container, so treat that as a starting point to verify against your measured peak rather than a sizing rule. Change only this setting, then restart. The step is done when the container stays under its limit through the same load test with no out-of-memory kill recorded.
Enable lazy loading for unused scanners
If some enabled scanners rarely match traffic, configure lazy loading so their models load on first use instead of at startup. This lowers idle memory and start time but moves the cost to the first request that uses each scanner, so measure that first-request latency. Make this the only change in this cycle. Restart the container, then record idle memory, startup time to readiness, and the latency of the first request to each lazy scanner. The step is done when idle memory has dropped without new out-of-memory events under the load test.
Move ONNX models to a mounted directory
Copy the ONNX model files to a host directory, mount that directory read-only into the container, and point each scanner at the mounted path. This removes model downloads from startup and keeps the model files under your change control. Confirm the file names and checksums match what the scanners expect. Make this the only change in the cycle, then restart. The step is done when the container starts without reaching out to the model hub, readiness time is recorded, and each scanner logs that it loaded from the mounted path.
Test gunicorn preloading and re-test after each change
Enable gunicorn preloading so workers share the loaded models in memory instead of each loading a copy. The LLM Guard API deployment documentation says gunicorn preloads models into shared memory among workers. Restart, then repeat the idle and loaded measurements, the restart test, and the readiness timing. Keep the previous configuration file as your rollback point and record which change was in effect for each run. The step is done when the change record shows, for every change, the before and after memory, restart count and readiness time.
How startup, workers and model memory fit together
The diagram starts at the container launch, where the gunicorn master process reads the API configuration and decides whether models load once before the workers fork or separately inside each worker. The memory-heavy step is model loading, so the two branches are shown side by side: a preloaded model shared among workers, and per-worker copies. The mounted model directory feeds both branches, and the readiness probe should only pass after the models are in memory, so check that the probe waits for that point before you tune worker counts.
Startup path where model loading choices drive per-container memory use.
Security and operational risks when changing memory settings
Memory and loading changes look like tuning, but they alter what gets loaded, who runs it and what fails silently. Each card below covers one risk to check before and after a change to an archived LLM Guard API container.
Integrity of models mounted from a directory
Loading ONNX models from a mounted directory means the container trusts whatever files it finds there. Record a checksum for every model file when you copy it, mount the directory read-only, and verify checksums at container start. Because the upstream models on Hugging Face are no longer maintained, a copied model is the only version you can vouch for. Confirm who can write to the host path and who can replace files in it.
Restart loops that hide scanner failures
A memory limit that is too low can make the container restart repeatedly, and an orchestrator may report the service as running between restarts. Scans may then fail open, skip or time out for some requests while health checks look acceptable. Check restart counts, exit codes such as OOM kills, and the scanner error rate after every limit change. Confirm your application fails closed when the guardrail does not answer.
Preloading and the failure blast radius
Gunicorn preloading loads models once in the parent process and shares them with workers, which lowers total memory use. The trade-off is that a fault during model loading or in shared state affects every worker at once, and a crashing parent takes the whole service down. Test a worker crash and a parent restart in staging, and confirm how many requests fail during each.
API deployment docsImage changes and the pinned environment
Changing the base image, Python version or a dependency can shift how models load and how scanners behave, even when the LLM Guard package version stays fixed. The last PyPI release, 0.3.16, requires Python 3.10 or newer but below 3.13. Rebuild from a pinned image digest, record the Python and dependency versions in the change record, and rerun scanner tests against known-good prompts before promoting the image.
Before and after measurements for each change
Without baseline figures, a memory change cannot be shown to help or to cause harm. Capture container memory use at idle and under a fixed request load, model load time at startup, and restart counts, both before and after each change. Keep these in your logging or metrics system with the image tag attached. Ask whether the API exposes the metrics you need, and if not, add them at the reverse proxy or in the container runtime.
How the fix differs by hosting environment
A single Docker Compose host is the most direct environment to fix. The container's memory is bounded by what the host and the Compose file allow, and a restart loop usually shows up in docker stats and in the container's exit status. Start by confirming how much memory the host has free after the operating system and any other containers take their share, because the LLM Guard API process and its scanner models compete for the same pool.
- Memory ceiling: Check whether the service sets a
mem_limitordeploy.resources.limits.memoryvalue. Without one, the container can grow until the host's out-of-memory killer intervenes. - Exit reason: Run
docker inspecton the stopped container and look forOOMKilled. Confirm that the restart is memory-related before changing anything. - Worker count: Confirm the gunicorn worker count in the startup command. Each worker that loads its own copy of the models multiplies the footprint.
- Model cache location: Confirm whether models are downloaded at first start into a named volume, and whether that volume survives container recreation.
- Host sizing: Ask the platform owner for the host's total and available memory, and confirm that no other service shares the same limit.
After each change, recreate the container and watch memory during the first scan requests, since the peak often comes from the first inference rather than from startup.
On Kubernetes, memory behavior is governed by the pod's resource requests and limits, and by the probes that decide whether a pod is restarted. A pod that exceeds its memory limit is terminated by the kubelet, while a slow start can trip a liveness probe before the models finish loading. Both symptoms can look like application crashes, so the fix has to address the container spec and the probe timing together. Confirm the cluster's admission and autoscaling rules before you raise any limit.
- Limits and requests: Check that
resources.limits.memoryis set and that the request is high enough for the scheduler to place the pod on a node with real headroom. - Termination reason: Use
kubectl describe podto confirm whether the last state showsOOMKilledor a failed probe. - Startup probe: Confirm whether a
startupProbeexists. Without one, alivenessProbewith a short initial delay can kill a pod that is still loading models. - Preloading: Confirm whether gunicorn preloading is enabled, since shared model memory across workers changes the per-pod figure.
- Replica sizing: Ask the platform team how many replicas run and whether a horizontal autoscaler reacts to memory.
Changes to limits should be tested in a non-production namespace first, using the same image and model set as production.
On an air-gapped host, the container cannot download models at start, so every model file must already be present on the host and mounted into the container. The loading path becomes the main risk. If a scanner points at a Hugging Face identifier, the start will fail or hang, and an operator may restart the container in a loop that looks like a memory problem. Confirm that the model directory is complete and readable before tuning memory, because a missing file is a common cause of slow or failed starts in isolated networks.
- Local model path: Confirm that each scanner that needs a model is configured with a local path, and that the path exists inside the container, not only on the host.
- Mount permissions: Check that the container user can read the mounted directory and that the files are the versions you tested.
- Network egress: Confirm that the host cannot reach external model hosts, so that a fallback download is not silently attempted.
- Image provenance: Confirm how the container image and model files were transferred, and keep a record of their checksums.
- Memory headroom: Ask whether the host has a documented memory budget, since the air-gapped team usually cannot pull a fix quickly.
For the full procedure on copying ONNX models and setting paths, use the sibling topic on running LLM Guard with local models.
Limits to confirm before you change memory settings
Memory and loading changes only help if they match the exact LLM Guard version, the scanners you run and the runtime around them. The items below are checks to complete in your own environment before you edit any setting, and each one should be confirmed against the documentation and your own measurements.
Exact version and configuration keys
Confirm the installed LLM Guard version and compare it with the configuration keys in the documentation for that release. The last release on PyPI is 0.3.16, but your deployment may pin an earlier build. Keys that appear in newer examples may be silently ignored by an older API server, so a change that looks correct can have no effect. Record the version from the running container, not from the image tag alone.
API deployment docsActual memory footprint of scanners
The memory a container needs depends on which scanners you load and which models each one pulls in. Start the container with your exact scanner set, wait for all models to load, and record resident memory per process. Repeat after a sample of real requests, because some scanners allocate more during inference. Compare that measured figure with any published guidance before you raise limits.
Whether gunicorn is used at all
Preloading only changes memory behavior if your container actually starts the API under gunicorn with multiple workers. Check the entrypoint or start command in your deployment, not the README. If you run a single process, preloading saves nothing, and the sharing behavior described for gunicorn does not apply. Confirm the worker count and preload flag in the running process list.
Python version in the base image
The Python version in your base image determines which dependency wheels install and whether the model runtime loads correctly. LLM Guard requires Python 3.10 or newer but below 3.13. Confirm the interpreter inside the running container with a version command, because a host interpreter can differ from the one in the image. A version mismatch can cause import failures that look like memory problems.
Whether the 16GB guidance fits your scanners
Treat any memory figure in older guidance as a starting point, not a requirement. Whether a 16GB limit suits your deployment depends on your scanner set, concurrency, worker count and request size. Test the container under peak load with a memory limit set to the guidance value, watch for restarts, and note which scanner or worker triggers them. A limit that works for one scanner set may fail for another.
Questions teams ask before changing LLM Guard memory settings
How much effort does a memory fix take, and what drives it?
The effort depends mostly on how many scanners the API loads, how many worker processes run, and how much memory the container gets. A fix that only changes a container memory limit is small. A fix that changes how models load, how workers start, or which scanners run per request takes longer because each change needs tests against real traffic. Other drivers include how many distinct models your scanner configuration pulls in, whether the models are downloaded at start or mounted from disk, and how many environments (development, staging, production) must match. Ask any provider to price the work from a written list of scanners, worker settings and target environments, and to state which changes they will test before and after.
Can our in-house team handle LLM Guard memory tuning?
Often yes, if the team already runs Python services in containers and can read container metrics. The work is mostly configuration, measurement and controlled restarts, not new code. The harder parts are knowing which scanners your application actually calls, reproducing realistic request sizes, and keeping a rollback path. Teams without container operations experience, or without a staging copy, usually take longer and risk longer outages. Check whether your engineers can answer three questions: which scanners run on each request, how much memory a worker uses at peak, and how to roll back a container image within your change process.
What do we need to provide for a memory tuning project?
You need representative test traffic, a staging copy of the container and its configuration, and access to container memory and restart metrics. Representative traffic matters because input and output sizes change memory use, and a short synthetic test can miss the peak. The staging copy should use the same image tag, scanner list and mounted model directory as production. Metrics should cover memory per container, restart count with exit reason, and startup time. You also need a named owner who can approve a production change window and confirm when the fix is accepted.
How long does it take to plan a safe change window?
Planning a safe window depends on how quickly your organization can approve production changes, how many environments must be updated first, and how long the staging tests take to produce usable results. Start by checking your own change calendar and freeze periods, because those usually set the timeline more than the technical work does. The technical plan should state the exact configuration changes, the metrics that prove the change worked, and the rollback steps. Ask whoever runs the work to show their change plan and how they will record results, instead of accepting a fixed duration.
Should we switch to lazy loading or gunicorn preloading first?
Choose based on whether your problem is memory at steady state or slow starts and restarts. Preloading with gunicorn loads models once in the parent process and shares them with workers through shared memory, which can reduce total memory when several workers run. Lazy loading defers each model until a scanner first needs it, which can cut startup time and memory for scanners that some requests never use, but the first request that triggers a load will be slower. Test both on your staging copy with your real scanner list. Do not combine them without measuring, because preloading and lazy loading can interact in ways that are hard to see from logs alone.
When should we stop tuning LLM Guard and plan a replacement?
Stop tuning and start planning a replacement when the memory fix only buys headroom for a short time, when restarts continue after you have tested the documented settings, or when you cannot keep a stable, pinned environment. The archived project no longer receives fixes, so each new failure you find will stay open unless you patch it yourself. DeployOpen does not recommend new LLM Guard rollouts. For existing deployments, the practical next step is a planned move to a maintained guardrail such as NeMo Guardrails, scanner by scanner, with parallel running until the replacement blocks at least what LLM Guard blocked. The LLM Guard documentation is the reference for current scanner behavior during that move.
Does DeployOpen support tuning and migration for existing LLM Guard deployments?
DeployOpen plans, implements, hardens and documents self-hosted open-source platforms inside your own infrastructure, and it runs security testing and compliance readiness work. Memory tuning and a scanner-by-scanner move to a maintained guardrail fall within that scope when agreed in writing. Most engagements are fixed-scope, so the written scope should name the environments, the scanners covered, the traffic used for testing, and the acceptance tests. DeployOpen does not publish a price list, and a quote follows from that written scope. Ask any provider to confirm what they test, what evidence they hand over, and what remains your responsibility after the change.
When tuning memory on LLM Guard stops being the right fix
Tuning memory buys time, but it does not add fixes. LLM Guard was archived by its owner on 9 July 2026, and its README states that the project and its associated Hugging Face models are no longer under active development or maintained. Every change to gunicorn workers, lazy loading or the container memory limit keeps the current release running a little longer. None of those changes will bring in a patched scanner, a corrected model or a new Python version. If an out-of-memory restart returns after a limit increase, or a model load fails after an upstream change you did not make, the honest response is to plan the replacement, not to tune again.
Set a rule for your team: when the same class of problem recurs, or when a fix requires changing the model source, the Python version or the base image, stop tuning and move to the next step. The LLM Guard documentation is the reference for what each scanner needs, and the decommissioning and migration pages in this series cover the inventory of callers, parallel running and scanner-by-scanner replacement. Record each tuning change with its date and the symptom it addressed, so the decommission plan starts from evidence. Ask your platform owner to confirm which scanners your callers depend on before the next memory incident forces an unplanned change.
No new LLM Guard rollouts are recommended. If a team proposes a fresh deployment, a new environment or a copy for another product, direct the request to a maintained guardrail such as NeMo Guardrails, and treat the existing deployment as a containment exercise with a fixed end date. Agree that end date with the risk owner in writing, including who accepts the risk while the service runs and what triggers an earlier shutdown. Keeping a deployment stable is a valid goal for the transition period only.
How to roll back a memory change safely
This sequence lets the team test a memory or model loading change on one deployment, measure its effect and return to the known-good state quickly if the guardrail regresses. The result is a recorded before and after comparison and a clear decision to keep or revert the change.
Record the current known-good state
Before touching anything, the platform engineer records the running image tag, the image digest, the full environment configuration and any scanner settings in the deployment manifest. Store this record with the change ticket so it can be reapplied without relying on memory. The step is done when someone other than the author can read the record and redeploy the previous version from it alone.
Keep the previous image tag available
Confirm that the previous image tag is still present in the registry and that the cluster or host can pull it. Do not overwrite or delete that tag during the change window. Because LLM Guard is archived, the team should also confirm that the prior image still starts on the current host, since no upstream fixes will be issued. The step is done when a test pull of the previous tag succeeds from the target environment.
Capture baseline memory and startup measurements
Measure container memory use at idle and under a representative request load, along with the time from container start until the readiness endpoint responds. Use the orchestrator or container runtime metrics that the team already collects. Record the values alongside the configuration record. The step is done when there are at least a few baseline readings taken under the same traffic pattern, written down with their timestamps.
Change one memory setting per deployment
Apply a single change, such as enabling lazy loading, moving a model to a mounted directory or changing the gunicorn preloading option. Changing several settings together makes it impossible to tell which one caused a result. The platform engineer notes the exact key and value changed in the change record. The step is done when the deployment rolls out and the change record names the only modified setting.
Measure the change under the same load
Repeat the baseline measurements with the same traffic pattern and compare memory peaks, restart count, and time to readiness. Watch the container logs for model loading errors and out-of-memory events. Write the before and after values side by side in the change record. The step is done when the comparison shows whether the change met its goal without introducing new restarts.
Confirm scanner results on sample traffic
Send a fixed set of sample prompts and responses through the changed deployment and compare the scanner verdicts with those from the previous version. Include prompts that the scanners are expected to flag and prompts that should pass. Any difference in verdicts must be reviewed by the security owner before the change is accepted. The step is done when the security owner signs off on the comparison, or the change is reverted.
Revert or close the change
If readiness, memory, or scanner verdicts regress, redeploy the recorded previous image tag and configuration, then repeat the readiness check. If the change is accepted, update the configuration record to the new state and keep the previous record for the rollback window. The step is done when the running deployment matches either the recorded previous state or the newly accepted state, and the change record shows which.
How DeployOpen works on this
DeployOpen approaches LLM Guard API memory use and model loading as a fixed-scope piece of work inside your own infrastructure. We start from how your containers are sized, how scanners load their models and how the API server runs under gunicorn, then change only what the evidence supports and document each change for your team.
How we scope
- The written scope names which containers, scanners and model paths are covered for LLM Guard memory and loading.
- We review current memory limits, restart history, startup logs and the gunicorn worker configuration before any change is proposed.
- Scope records whether models load lazily or at startup, and which scanners load from a mounted directory.
How we build
- We adjust container memory limits, lazy loading and gunicorn preloading settings in a non-production copy first.
- Model files are moved to a mounted directory and referenced by path, so startup no longer depends on downloads at boot.
- Each change is tested with repeated restarts and sustained request load to confirm memory stays within the new limits.
How we hand over
- You receive the final configuration files, container definitions and a record of each memory and startup measurement taken.
- Runbook steps cover restarting containers, rolling back a settings change and checking model files after an update.
- Documentation states the limits that were tested and the conditions under which they should be retested.
How we support
- Ongoing support and retesting after configuration changes are available as a retainer, which we can scope in writing.
- Because LLM Guard is archived, support work focuses on keeping the existing deployment stable until it is replaced.
- Ask any provider to confirm how they will handle updates to your pinned environment and models before work starts.

