Application safeguards and usage limits
hard 6 minModel-facing safeguards span inputs, outputs, action permissions and resource limits.
Video lesson · Chapter 1 of 4
The instruction inside a reference note
Theory
Read or listen first. About 2 min.
What it is
Model-facing safeguards span inputs, outputs, action permissions and resource limits. Each protects a different boundary. The application must enforce access and capability checks even when text filters and schema validation succeed.
Why it exists
Test permitted operations alongside denied ones with harmless fixtures. Record whether protected state changed and whether the original task remained useful. A system that rejects every request can look safe on a one-sided test.
A retrieved note requests a write the user never approved
Inside this chapter
Chapter sources- Prompt injection and application trust boundaries
- Prompt injection is a boundary problem when untrusted content is interpreted as an instruction. Treat retrieved text and tool output as evidence, constrain tool privileges, and enforce policy in application code. Test with harmless fixture documents and verify sensitive actions still require an authorized decision.
A language model reads instructions and data through the same channel, so text inside a retrieved page or tool result can look like a command. You cannot reliably filter that out, so design as if the model might follow it: give tools the narrowest permissions, require explicit user approval for writes, and enforce both in application code. Test with harmless fixtures that contain embedded requests, and assert that protected state did not change.
Example. Place an inert out-of-scope request inside a source document. Assert that it is displayed as evidence and that the simulated protected record is unchanged.
Another example. An email assistant reads a message containing a fixture line asking it to forward the inbox. The test passes only if the summary mentions the request as content and no forward call reaches the mail API.
Retrieved text is evidence, never authority
The authorisation check lives in code, outside the model, and uses the user's actual permissions. - Guardrails & Input Filters
- Input filters can flag or reject content before generation, while application policy controls what the service can actually do. Measure false positives on legitimate tasks and inspect missed cases. A filter and a permission check serve different purposes and should each have independent tests.
An input filter is a classifier with a threshold, so every setting trades missed cases for false alarms. Measure both on representative traffic and inspect the misses by hand. A filter decides what text reaches the model; it does not decide what the service is allowed to do. Keep separate tests for the filter and for permission checks, so a filter change cannot quietly weaken access control.
Example. Evaluate a diagnostic filter on both an allowed support request and a prohibited fixture. Measure the legitimate work the filter incorrectly rejects.
Score the filter in both directions
Test set Cases Flagged Rate Reading Legitimate support requests 200 6 3% false positives real users wrongly blocked Prohibited fixtures 50 46 92% caught 4 slipped through Illustrative counts. Tune the threshold while watching both rows, not just catch rate. - Output Moderation
- Output moderation evaluates generated content before release according to a defined policy. Streaming needs a deliberate buffering strategy because already displayed text cannot be recalled. Audit representative decisions, offer appropriate fallback responses and keep access to sensitive moderation logs restricted.
Moderating a finished answer is straightforward; moderating a stream is a design choice, because anything already on screen has been seen. Buffering per sentence or paragraph lets the moderator check a meaningful unit while keeping the experience responsive. Decide what replaces blocked text, log decisions with restricted access, and sample them for review so policy drift is noticed.
Example. A streaming answer requires review before release. Compare buffering a complete sentence with publishing every token immediately under the same moderation policy.
Sentence-level buffering before display
Buffering a sentence adds a short delay but avoids showing text that cannot be taken back. - Rate Limiting & Cost Controls
- Limit concurrent requests, input size, output tokens and tenant spend separately. A request-rate limit alone permits a few unusually expensive generations to consume the budget. Reserve a maximum cost before execution, reconcile actual usage afterward, and expose clear behavior when a limit is reached.
A requests-per-minute limit treats a 50-token question and a 50,000-token document job the same. Separate limits for concurrency, input size, output tokens and spend close that gap. The reservation pattern makes the spend limit safe under concurrency: each request claims its worst-case cost atomically before starting and returns the unused part when it finishes. Tell the caller clearly when a limit is hit, with a retry hint rather than a vague error.
Example. With ten available budget units, two requests each reserving six must not both start unchecked. Reconcile each completed reservation with its actual usage.
Another example. A report generator caps each tenant at 3 concurrent jobs and reserves the maximum output cost per job. A burst of 20 jobs from one tenant queues behind its own limit instead of starving other tenants.
Reserve before running, reconcile after - 1Budget: 10 units freetenant spend limit
- 2Request 1 reserves 64 units remain
- 3Request 2 asks for 6only 4 free, so it waits
- 4Request 1 used 42 unused units released
- 5Request 2 starts6 free, reserves 6
Reserving the maximum stops two expensive requests from both starting on the same budget.
- Token bucket limits
- A token bucket holds up to B units and refills at rate r. A request proceeds only if the bucket holds at least its cost, which allows short bursts while capping the long-run rate.
The bucket only needs the last level and timestamp per key, so it is cheap to store in a shared cache. Pricing each request by its estimated token cost turns it into a spend limiter. Keep the check and the decrement atomic, or two concurrent requests can both see enough balance.
Example. With B = 10 and r = 1 unit per second, a client can spend 10 units at once, then about one unit per second after that.
Refill, then spend
B is the burst capacity and r the sustained rate. Charge cost in tokens or estimated spend, not just requests.
How it works
Step through the idea, or press Play.
Untrusted note. Read-only reference
Understand it
Worked example
A retrieved note requests a write the user never approved
- 1
Read-only reference
The note belongs in the evidence channel. The user requested a summary and has not approved any account change.
- 2
Write request denied
Even a well-formed proposed action must pass authorization and scope checks outside the model. The fixture uses an inert account action.
- 3
Summary only · no state change
Log a redacted decision and verify the protected record is unchanged. Separately evaluate whether normal summaries still work.
Follow the handoffs
Every layer passed except the one that matters
| Layer | What it checked | Result |
|---|---|---|
| Input filter | is the note harmful content? | pass |
| Schema validation | is the tool call valid JSON? | pass |
| Authorisation | did this user approve this write? | deny |
| Protected record | state after the run | unchanged |
Summary
The whole topic in one minute.
A content check and a schema check provide no authority over account state. The operation must be independently permitted.
Test permitted operations alongside denied ones with harmless fixtures. Record whether protected state changed and whether the original task remained useful. A system that rejects every request can look safe on a one-sided test.