Course outline

Generative AI / Intermediate: retrieval and controlled workflows / Lesson 14

Retrieval Authorization and Ranking

Keep access checks and evidence ranking distinct when assembling context for a generated answer.

About 3 min readContent reviewed
On this page

Lesson goals

  • Define the authorized evidence set for a request.
  • Explain why ranking and access checks serve different purposes.
  • Test permission changes and retrieval boundaries.

Define the evidence boundary

A retrieval system should only supply a user with evidence they are authorized to access. Define the principal, resource identity, permission source, and time at which access is checked. Ranking answers which eligible passages seem relevant; it does not grant permission. A similarity score cannot replace an access decision.

Permissions can change after an index is built. Decide how current access state is applied at query time, including deleted or newly restricted documents. Keep the index and authorization system consistent enough for the applicationโ€™s requirements, and fail safely when permission state cannot be established.

Filter and rank a synthetic set

The following toy data contains scores and an explicit permission flag. It first selects allowed records and then ranks them. The private record has the highest score but is excluded. Real systems need authoritative access checks and careful design of their retrieval infrastructure; this list is an illustration rather than a security implementation.

python Rank only allowed toy records
docs = [("public", 0.7, True), ("private", 0.9, False), ("team", 0.8, True)]
allowed = [d for d in docs if d[2]]
print(sorted(allowed, key=lambda d: d[1], reverse=True)[:2])

The team and public records remain eligible; the higher-scored private record is excluded.

Recheck selected candidates

A separate validation step can remove candidates that are no longer authorized before context reaches the model or user. The example filters identifiers against a toy allowlist. An actual application must obtain authorization from its trusted policy system and account for timing, caching, and the possibility that access changes during a request.

python Filter candidate identifiers
allowed_ids = {"public", "team"}
candidate_ids = ["private", "team", "public"]
print([item for item in candidate_ids if item in allowed_ids])

The private identifier is removed; the remaining identifiers preserve their candidate order.

Measure quality within the allowed set

Evaluate retrieval using queries and relevant passages that the test principal may access. If unauthorized results occupy a fixed candidate budget before filtering, relevant allowed passages may be missed; the exact behavior depends on the retrieval design. Test both access isolation and recall after filtering, and avoid displaying restricted snippets or identifiers in logs.

Handle unavailable or conflicting evidence

If no authorized evidence supports an answer, state the limitation or ask for an authorized source rather than filling the gap from restricted content. Test permission revocation, absent permissions, and index refresh alongside ordinary retrieval. Record which policy and index versions were used without retaining unnecessary sensitive text.

Common errors and fixes

A relevant restricted passage appears in an answer.

Why it happens
Relevance ranking was treated as an authorization check.
How to fix it
Enforce access policy before passing content to the model or user.
Prevent it next time
Test retrieval with principals having different permissions.

Authorized results disappear after restricted results are removed.

Why it happens
A fixed candidate budget was consumed by ineligible records.
How to fix it
Review the retrieval design and test recall within the allowed set.
Prevent it next time
Measure both authorization and retrieval quality.

A revoked document remains available through a stale index.

Why it happens
Current permissions were not checked at request time.
How to fix it
Recheck against the trusted policy source and refresh affected state.
Prevent it next time
Test revocation and define cache invalidation behavior.

2 practice exercises

1. A private passage scores 0.9 and an allowed passage scores 0.8. Which may enter the user context?

Show worked solution

Only the allowed passage may enter, assuming the trusted policy confirms access. A higher relevance score does not authorize the private passage.

2. All top-ranked candidates are restricted, while a relevant allowed passage exists lower in the index. What should be tested?

Show worked solution

Test whether the retrieval design can find relevant allowed evidence without exposing restricted content. Measure authorized recall and check current permissions; do not simply increase the context with unauthorized passages.

Key takeaways

  • Authorization determines eligibility; ranking orders eligible evidence.
  • Check current access before exposing retrieved content.
  • Evaluate access isolation and retrieval quality together.

Official references